Jump to content

GD imagejpeg and PHP5.4


rwhite35
Go to solution Solved by trq,

Recommended Posts

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 by rwhite35
Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.