PHP GD generated image The image cannot be displayed because it contains errors in Firefox

When generating an image with PHP, for example adding a watermark to a server stored picture, using below code, or similar one with JPEG instead of PNG, image might not be generated at all and throw an error, directly visible on Firefox, but not on Chrome.

This kind of code, despite working on some servers, might now work on some other, sometimes for obscure reasons. In my case, it worked perfectly fine locally, but produced below error on internet server, the image cannot be displayed because it contains errors.

The issue can come from several reasons, here are some of them :

An output has already been set previously.

A genuine error from image generation.

The wrong image type is used.

Image library is not installed.

$stamp = imagecreatefrompng('watermark.png');
if(!$im = imagecreatefrompng('image.png')) die('Error generating image');
$marge_right = $marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

$copyimage = imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

header('Content-type: image/png');
if(!imagepng($im)) die('Error generating image');
if(!imagedestroy($im)) die('Error generating image');
if(!imagedestroy($stamp)) die('Error generating image');

An output has already been set previously

Even if not intentionally, it might be the case for example that other previous scripts were containing spaces at the end - to avoid this error, make sure, as PHP recommends, not to close scripts with PHP end tag ?>

Then, before using the header function, use the following code to make sure any previously set header is discarded

foreach (getallheaders() as $name => $value) {

And also this function to make sure no output has been generated before the header and image


This solved the issue in my case, and the image was correctly displayed again.

A genuine error from image generation

In case a real error was thrown during image generation, make sure PHP is set to display all possible kind of errors, and check the corresponding logs, in order to be able to track it down and solve it.

Following code will make PHP reports all errors in the log :

ini_set('display_errors', '1');

You might then find out that some errors were reported, even if they did not appear in the output html, due to server configuration.

The wrong image type is used

Make sure the image types you are using are correct, for example using this code to distinguish between png and jpeg.

Also make sure the header is correct according to the output used, depending on the image generation function used

//to generate png image
header('Content-type: image/png');
if(!imagepng($im)) die('Error generating image');
//to generate jpeg image
header('Content-type: image/jpeg');
if(!imagejpeg($im)) die('Error generating image');

Image library is not installed

To make sure the image libraries are installed. To do so, run the function phpinfo(); in a script, and check the output for the following value related to PHP GD, the graphics library that is used by functions like imagepng();

In case packages are missing, the way to solve it depends on your local configuration, either by adding the package using PEAR commands, or using the web host built in solution for package installation.

What to do when imagejpeg or imagepng is not working

In NotePad++, open the PHP file that calls the imagepng function, and save it after going to menu Encoding > Convert to UTF-8 without BOM.

In DreamWeaver, go to Page Properties > Title/Encoding > uncheck Include Unicode Signature (BOM).

When PHP imagejpeg or imagepng is not working, it is most likely due to the fact that the PHP file has been encoded with a BOM, a Bill Of Material, and therefore extra information is sent with the image before the image data.

The PHP imagejpeg and PHP imagepng functions are function used to send a picture code directly in the server response.

PHP - imagepng not working properly
PHP – imagepng function
PHP – imagejpeg function

International Business Consulting
About the author - International Business Consulting
Yoann is an International Business Consultant, facilitator and coordinator. He worked more than 10 years in global business, supply chain, financial, SAP ERP projects, Web and SEO optimization for various industries, such as logistics, cosmetics, consumer goods, beauty care, or fashion, in more than 30 countries around the globe.

Comments (6)

 2018-08-19  Irvin Vasquez
Simple, clear and well explained, many thanks
 2018-08-19  Joyce Lawrence
 2018-08-19  Catherine Roberts
Dokładnie tego, czego szukałem, idealnie
 2018-08-19  Headlinext
जानकारी का महान टुकड़ा, साझा करने के लिए धन्यवाद
 2018-08-19  Marshel
विश्वास नहीं कर सकता कि मुझे अंत में समाधान मिला, यह लंबे समय तक एक दुःस्वप्न था, अब हल हो गया
 2018-08-19  porcateraC
بالضبط ما كنت أبحث عنه ، مثالي

Leave a comment