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

Quick links

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.

Similar articles

Comments (6)

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

Leave a comment