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.

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

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.

Master Website Creation: Enroll Now!

Transform your digital presence with our comprehensive Website Creation course – start your journey to becoming a web expert today!

Enroll Here

Transform your digital presence with our comprehensive Website Creation course – start your journey to becoming a web expert today!

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

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

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

Frequently Asked Questions

Why can't I display the image created with PHP GD in my browser?
If your PHP GD generated image is not displaying and shows an error in the browser, it might be due to several reasons such as previous output already being sent, genuine errors during image generation, using the wrong image type, or the GD image library not being installed. Ensure no previous output is sent, check for genuine errors, verify the image type and header, and confirm the GD library's installation.
How can developers fix the issue of PHP GD generated images not displaying in Firefox due to errors?
Developers should check the image generation code for errors, ensure the correct content-type header is being sent before any image data, and verify that no PHP errors or warnings are being outputted. Using ob_clean() before outputting the image may also help.

Yoann Bierling
About the author - Yoann Bierling
Yoann Bierling is a Web Publishing & Digital Consulting professional, making a global impact through expertise and innovation in technologies. Passionate about empowering individuals and organizations to thrive in the digital age, he is driven to deliver exceptional results and drive growth through educational content creation.

Master Website Creation: Enroll Now!

Transform your digital presence with our comprehensive Website Creation course – start your journey to becoming a web expert today!

Enroll Here

Transform your digital presence with our comprehensive Website Creation course – start your journey to becoming a web expert today!



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