imagecolorallocate has given up on me

Need help with Photoshop, the GIMP, Illustrator, or others? Want to show off your work? Looking for advice on your newest Flash stuff?

Moderator: General Moderators

Post Reply
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

imagecolorallocate has given up on me

Post by josh »

Code: Select all

// create a 100*30 image
$im = imagecreatetruecolor(100, 30);

// white background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);

$textcolor = imagecolorallocate($im, 0, 0, 0);

// write the string at the top left
imagestring($im, 5, 0, 0, "Hello world!", $textcolor);

// output the image
header("Content-type: image/jpeg");
imagejpeg($im);
is outputting an all black image, i changed the textcolor to white and it appeared white text on a black background, despite the fact I set the background to white, this happens no matter what I set the background color to, imagecolorallocate is NOT returning -1 so it should be setting the background color right? Also this happens no matter what format image I try to output!

Code: Select all

GD Support 	enabled
GD Version 	bundled (2.0.28 compatible)
FreeType Support 	enabled
FreeType Linkage 	with freetype
GIF Read Support 	enabled
GIF Create Support 	enabled
JPG Support 	enabled
PNG Support 	enabled
WBMP Support 	enabled
XBM Support 	enabled
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

You're not filling the background. When you create a palette image with imagecreate() it fills the background with the first color you allocate as it's the first color in the palette. You actually need to specifically fill the image when you're using imagecreatetruecolor();

Add imagefill($im,0,0,$bg); after the line where you allocate $bg and all will be well.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Thanks, funny thing is I copied that code exactly (well pretty much, I just changed the text and the color) from http://us3.php.net/manual/en/function.imagestring.php
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

jshpro2 wrote:Thanks, funny thing is I copied that code exactly (well pretty much, I just changed the text and the color) from http://us3.php.net/manual/en/function.imagestring.php
Yes, but in that example they use imagecreate(), not imagecreatetruecolor() .. hence the problem.

Besides, any idiot can add comments to the manual. Loads of shoddy code gets in there.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

onion2k wrote: they use imagecreate(), not imagecreatetruecolor() .. hence the problem.
nope they're using imagecreatetruecolor(), at least for me... maybe there's a discrepancy with the mirrors

Regardless, thanks for your help
Post Reply