rwhite35 Posted July 9, 2013 Share Posted July 9, 2013 (edited) Spent the day trying to figure out why GD library 2.0 imagejpeg($resource_id,NULL,$quality); wouldn't output my images to the browser after updating PHP from 5.3 to 5.4. Allow me to hopefully save you the trouble. If you've enabled GD library and using an external script to generate image files, NO OTHER OUTPUT from the external script is permitted, except the header('Content-Type: image/jpeg'); and the image stream. This goes for any sub include scripts (like a database connection). Error reporting for the executable script should be set to 0 with error_reporting(0); OR you should absolutely make sure none of the scripts are generating any errors or any other output. Here some code: initial_script.php <!DOCTYPE html> <html lang="en"> <head><title>First in stack</title> <meta charset="UTF-8" /> </head> <body> <img src="<?php print createimage.php?imgid=$img_id&uid=$user_id; ?>" alt="red dot" > </body> </html> Next create the image createimage.php <?php include dbconnection.php; //assume db connection and data pull returns image name $image = $row['img_name']; //image create resources etc.. $file="/path/to/".$image; $file_resid = imagecreatefromjpeg($file); // first send the header header('Content-Type: image/jpeg'); // display the image to the browser $quality=100; imagejpeg( $file_resid, NULL, $quality) or die("error displaying jpg"); imagedestroy($image_t); ?> In my case dbconnection.php was spawning a low level notice which was being sent before the header and image stream. This was causing the process to return a broken image icon. Also, imagejpeg now requires NULL if you are displaying the image to the browser. Before (5.3) this parameter could be " ". Firebug was throwing this error - Image corrupt or truncated: Edited July 9, 2013 by rwhite35 Quote Link to comment Share on other sites More sharing options...
Solution trq Posted July 9, 2013 Solution Share Posted July 9, 2013 This has always been the case, nothing new in php5.4. Whatever you where running before must have simply been hiding errors. Also, imagejpeg now requires NULL if you are displaying the image to the browser. Before (5.3) this parameter could be " " That sounds like a bug that has been fixed. Quote Link to comment Share on other sites More sharing options...
rwhite35 Posted July 9, 2013 Author Share Posted July 9, 2013 Thanks TRQ, That may be true, however strict enforcement may be the difference between 5.3 and 5.4. Same scripts in 5.3 work but didn't in 5.4. It should be documented because the answer was not readily available on the net. Regards, Quote Link to comment Share on other sites More sharing options...
kicken Posted July 9, 2013 Share Posted July 9, 2013 It should be documented because the answer was not readily available on the net. It's not documented because it's not something that changed. It's a matter of not setting up the configuration the same after the upgrade. The sample INI configurations for PHP have included E_NOTICE in the error reporting for quite some time. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.