[SOLVED] Putting dynamic text on a static image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

[SOLVED] Putting dynamic text on a static image

Post by evilmonkey »

Hello.

I have a static image with a place on which dynamic text may be placed. I made code to dothis, but I have a problem with this. I want black text on a white background, but I get yellow on white instead, and it looks bad. Here's the code, please help me.

Code: Select all

$origpng= "testpic.png"; 

$imgsize = getimagesize($origpng);

$newwidth = round($imgsize['0']/1);

$newheight = round($imgsize['1']/1);

$imgwidth = $imgsize['0'];

$imgheight = $imgsize['1'];

$image = imagecreatefrompng($origpng);

$newimage = imagecreate($newwidth, $newheight);

//i imagine the problem is here
$bg = imagecolorallocate($image, 0,0,0);
$black = imagecolorallocate($image, 0, 0, 255); //I also tried 0,0,0 and 255, 255, 255 to no avail


imagecopyresized($newimage, $image, 0, 0, 0, 0, $newwidth, $newheight, $imgwidth, $imgheight);

imagedestroy($image);

imagestring($newimage, 2, 20, 20, "some text here", $black);

header("Content-type: image/png");

imagepng($newimage);

imagedestroy($newimage);

?>
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Try allocating color for $newimage instead of $image:

Code: Select all

//...
$bg = imagecolorallocate($newimage, 0,0,0);
$black = imagecolorallocate($newimage, 0, 0, 0);
//...
Scorphus.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Foolish me. Thank you, that worked. Best of luck. :)

~Jack.
Post Reply