Jump to content

GD - dynamic image help.....


cwncool

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!
Link to comment
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]
Link to comment
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]
Link to comment
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!
Link to comment
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?
Link to comment
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....
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.