Page 1 of 1

php imaging - problem with a simple rectangle

Posted: Wed Apr 30, 2008 5:22 am
by konrados
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi,
I've just started learning the image functions in php. I wanted to create a simple php file, which acts as an image ( header("Content-type: image/png"); ) and which draws a "shading" rectangle, that is a filled rect often used on websites as a background of headers etc.

Anyway - as a start I thought I'll create a function which will draw it with random colors - precisely I was going to draw as many lines as needed, with the colors slowly changing, but as a test these colors are random. Here's my test code:

Code: Select all

 
 
$width=512;$height=512;
$im=imagecreate($width,$height);//imagecreatetruecolor   ?
 
//drawing horizontal lines from up to down:
for ($i=0;$i<$height;$i++){
  $col=imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
  imageline($im,0,$i,$width,$i,$col);
  //imagecolordeallocate($im, $col); //everything goes wrong if this is on
};
 
imagepng($im);
imagedestroy($im);
 
And here is the result:
http://www.konradp.com/wsystem/images/p ... w_rect.php

As you can see, the rectangle's size is OK - 512x512 but the lines with random colors end up on line 255, the rest is a solid color.

Can somebody explain me why?

And by the way - does imagecreate function create an image with 24 bits or 8 bits color ? (Although it shouldn't change anything regarding my problem).


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: php imaging - problem with a simple rectangle

Posted: Wed Apr 30, 2008 5:29 am
by konrados
Stop... I thought I'll test imagecreatetruecolor anyway and it worked. So - nevermind.

Re: php imaging - problem with a simple rectangle

Posted: Wed Apr 30, 2008 6:21 am
by onion2k
Just for the record, imagecreate() creates an 8bit indexed color palette image. They're limited to 256 colours so once you've used all 256 any subsequent call to imagecolorallocate() will 'fail'* because there's no more room in the palette.

* I think it fails silently, but it might be catchable using an if block...

Re: php imaging - problem with a simple rectangle

Posted: Wed Apr 30, 2008 6:25 am
by konrados
Thanks.
Btw: is there a way to display errors in a script like the one above? That is a script which type is "image"? When I make a syntax error in a script, the only information I can see is "couldn't display the image" and I have to guess what's wrong. Is there some kind of trick ?

Re: php imaging - problem with a simple rectangle

Posted: Wed Apr 30, 2008 6:32 am
by onion2k
Make sure the content-type header is sent after all the image code. If your PHP install is set up to buffer output so you can send headers at any time you'd need to turn that off too. Eg

Code: Select all

<?php
 
$i = imagecreate(200,200);
$red = imagecolorallocate($i,255,0,0);
imagefil($i,0,0,$red);
 
header("Content-type: image/jpeg");
imagejpeg($i);
 
Obviously there's a problem in that script with imagefill() (it's missing the second 'l'). Because the parse error comes before the header() you should still see the error. You might also see an error saying "headers already sent" though.

If you're using PHP5 you can also query error_get_last() to see if there's been a non-fatal error and only send the header if there hasn't been.

Re: php imaging - problem with a simple rectangle

Posted: Wed Apr 30, 2008 7:05 am
by konrados
Thanks!