Page 1 of 1

imagecolorallocate has given up on me

Posted: Thu Sep 01, 2005 11:08 pm
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

Posted: Fri Sep 02, 2005 5:41 am
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.

Posted: Fri Sep 02, 2005 5:16 pm
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

Posted: Fri Sep 02, 2005 6:16 pm
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.

Posted: Sat Sep 03, 2005 12:33 am
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