Jump to content

Archived

This topic is now archived and is closed to further replies.

cwncool

GD - dynamic image help.....

Recommended Posts

I'm trying to make a script to create a new image from "http://www.gigacat.com/kitten.jpeg" (that's my own server) into another image with a string in the textbox of the picture. These are the two codes i've tried.
[code]<?php
$id = imagecreatefromjpeg("kitten.jpeg");
$string = "Meow, meow, meow!";
$black = imagecolorallocate($id, 0, 0, 0);
imagestring($id, "font.ttf", 170, 117, $string, $black);
imagejpeg($id,"newkitten.jpeg");
?>[/code]
on this first one i just end up with a blank, white page.

and this.
[code]<?php
header("Content-type: image/jpeg");
$id = imagecreatefromjpeg("kitten.jpeg");
$string = "Meow, meow, meow!";
$black = imagecolorallocate($id, 0, 0, 0);
imagestring($id, "font.ttf", 170, 117, $string, $black);
imagejpeg($id,"newkitten.jpeg");
?>[/code]
on this one, all I did was add the header, and it creates another blank, white page displaying the script's url on the page. Can someone please help me figure out what's wrong? Thanx!

Share this post


Link to post
Share on other sites
Try to change:
[code]imagejpeg($id,"newkitten.jpeg");[/code]
To
[code]imagejpeg($id);[/code]

Now, you can't use a ttf font with imagestring, you must use imagettftext():

[a href=\"http://www.php.net/imagettftext\" target=\"_blank\"]http://www.php.net/imagettftext[/a]

Share this post


Link to post
Share on other sites
i got it working (thanx), but is there a way to make the font size smaller? Thanx!

Share this post


Link to post
Share on other sites
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]imagettftext ( resource image, [b]float size[/b], float angle, int x, int y, int color, string fontfile, string text )[/quote]

[a href=\"http://www.php.net/imagettftext\" target=\"_blank\"]http://www.php.net/imagettftext[/a]

Share this post


Link to post
Share on other sites
thanx. i tried that, but must have done something wrong! :) thanx for your help!

Share this post


Link to post
Share on other sites
Okay. The script I have working now is....
[code]
<?php
header("Content-type: image/jpeg");
$id = imagecreatefromjpeg("kitten.jpeg");
$string = $_GET['text'];
$black = imagecolorallocate($id, 0, 0, 0);
imagettftext ($id, 7, 0, 160, 120, $black, "font.ttf", $string);
imagejpeg($id);
?>
[/code]
Instead of taking me to a page with the PHP image on it, how can I make it save it as an image on my server and then forward to another page, html, that says "the cat says.." and then shows the image on it or to just refresh the page with instead of the example image, but the generated image. Like [a href=\"http://atom.smasher.org/chinese/\" target=\"_blank\"]here[/a] Thanx!

Share this post


Link to post
Share on other sites
you should be able to call that script as the source in your image tag. for example, if that script was called blah.php you should be able to do like

<img src = 'blah.php' />

Share this post


Link to post
Share on other sites
i know that and that's what I have, but after submit on the page (http://www.gigacat.com/kitten.html (it will be php once i figure this out)) it takes me to the image file because the script has the image header and creates the image file. i want it to refresh the page with instead of blank php image file, with the one with the text so the form is still there... You see?

Share this post


Link to post
Share on other sites
the image file is the action in my form, but when i put the form page as the action in the form it doesn't work. when the form is like this...
[code]
<img src="kitten1.php?text=<?php echo $string ?>">
<form action="kitten.php" method="GET">
<p>What do you want the kitten to say? <input type="text" name="text" maxlength="19" /></p>
<p><input type="submit" value="Talk, Kitty!"></p>
</form>
[/code]
it just refreshes the page with the text entered in the top url of the form page, but does nothing to the image....

Share this post


Link to post
Share on other sites
i don't think you can combine the two scripts. the idea of using the script as a source is that you are creating a virtual link, and i don't think you can create a virtual link from one place to itself like that. you are going to have to seperate the image generation script from the form script. make your form script display the image, with the kitten.php as the source, and the form itself. then do the action to itself. example:

kitten.php
[code]
<?php
if ($_GET['text']) {
   $text = $_GET['text'];
} else {
   $text = 'meow';
}

//build your image using $text
//and end it with the header
?>
[/code]

form.php
[code]
<img src="kitten1.php?text=<?= $_GET['text'] ?>">

<form action="<?= $_SERVER['PHP_SELF']" ?> method="GET">
<p>What do you want the kitten to say? <input type="text" name="text" maxlength="19" /></p>
<p><input type="submit" value="Talk, Kitty!"></p>
</form>
[/code]

Share this post


Link to post
Share on other sites
that worked perfectly! :) :) Thanx Crayon!
[a href=\"http://www.gigacat.com/kitten.php\" target=\"_blank\"]here's the working script![/a]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.