Using GD to create a JPG not working ?

trg_dk
trg_dk used Ask the Experts™
on
Hi

I am uisng GD to create a JPG - copied the example code from php.net - i Charles Proxy I can see that something that looks like img is returned... But my browser shows a broken image :(

So I might be tired after 14 hours at the computer today, so I need you Experts help - please...

Here's my code
$im = imagecreatetruecolor(120, 20);
            $text_color = imagecolorallocate($im, 233, 14, 91);
            imagestring($im, 1, 5, 5, "gd test", $text_color);
            
            // Set the content type header - in this case image/jpeg
            header('Content-Type: image/jpeg');
            
            // Output the image
            imagejpeg($im,null);
            
            // Free up memory
            imagedestroy($im);

Here's the output from Charles (screencap)

And the url are http://developer.nemvagt.dk/admin/avatar/gdtest
gd-charles-output.GIF
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
The server is Win2008R2, Apache2, PHP5 - GD is loaded

What's up ??
change the line
imagejpeg($im,null);

Open in new window


to:
imagejpeg($im,NULL);

Open in new window

Did that fix it?

I answered as quickly as possible so you could get the answer you needed, if that fixed it then please accept, otherwise say that it didn't so I can try something else.

Thank you :)

-Robert

Author

Commented:
Robert, thanks for your quick reply - it changed the error to be excatly the same as my "real" gd error loading stuff from the database...

I get this garbage (which it propably isn't) inside Charsel proxy
gderror.GIF
Most Valuable Expert 2011
Top Expert 2016
Commented:
It looks like we are not seeing the entire code set.  Probably there is something else that you did not post here.  For example, we do not see the <?php tag to start the PHP interpreter.  It may be that the headers were already sent, perhaps by spurious browser output.  This script implements the code posted above, unmodified, and works correctly.
http://www.laprbass.com/RAY_temp_trg.php
<?php // RAY_temp_trg.php
error_reporting(E_ALL);

// COPIED FROM THE POST AT EE
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "gd test", $text_color);

// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');

// Output the image
imagejpeg($im,null);

// Free up memory
imagedestroy($im);

Open in new window

Yes according to the output it appears that an image is being generated but the browser doesn't recognize the header.

Like Ray said this is usually because something is causing output before your header tag.

Check the error log, and find out what error (most probably a notice or warning since the code doesn't end) is popping up.

Author

Commented:
Thanks Ray

You got me thinking and this is a problem I could recognize...

So I started the code with a ob_clean(); and my problem was solved.

Thanks for the tip :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial