'Error when uploading certain .png files "Interlace handling should be turned on when using png_read_image"
I have seen some random errors when processing images that were uploaded to my site, where certain .png files will cause an error to be echo'd to the output buffer which will screw up the response back to the server.
The error is:
"Interlace handling should be turned on when using png_read_image"
I have seen this in some cases where i am manually processing uploaded files and now i just started seeing this on my Wordpress install as well. I have not tagged this question as Wordpress, as i doubt it has anything to do with wordpress, rather it must be some issue within PHP.
I originally saw the issue in PHP 5 but i have also seen it since i upgraded to PHP 7. I am now running PHP 7.1.9.
Googling so far has not returned any meaningful results, there do not seem to be any answers which are specific to PHP, most answers say that the user needs to enable interlacing on the source file BEFORE uploading, which would be unacceptable from the web server standpoint because i cannot control what users upload. The images still seem to work fine after upload, i just need a way to suppress this message so it doesn't cause erroneous errors to be shown to the user.
The error message does mention using png_read_image()
, however i am not directly calling this function anywhere. perhaps it is calling it internally somewhere, but that makes it very difficult to debug. Best i can figure is that it is related to either imagepng()
or imagecreatefrompng()
.
For now i am working around this in the front end, where i am stripping off the error text before trying to parse the response.
I am sorry if this is a little lacking in details but i really do not know what else to provide for now.
Solution 1:[1]
I have this message (PHP 7.2.3 + GD) :
libpng warning: Interlace handling should be turned on when using png_read_image
and some others
libpng warning: tRNS: invalid with alpha channel
libpng warning: iCCP: known incorrect sRGB profile
my code is merging png images from different sources (then different qualities) in one image, after working with Photoshop on some pictures, messages disappeared, however it seems there is a workaround, see explanations below:
<?php
// Create an image instance
$im = imagecreatefromgif('php.gif');
// Enable interlancing
imageinterlace($im, true);
// Save the interlaced image
imagegif($im, './php_interlaced.gif');
imagedestroy($im);
?>
https://www.php.net/manual/en/function.imageinterlace.php
imageinterlace() turns the interlace bit on or off.
And for clarification what interlace is:
The imageinterlace() function is an inbuilt function in PHP which is used to enable or disable interlace in an image. Interlacing (also known as interleaving) is a method of encoding a bitmap image such that a person who has partially received it sees a degraded copy of the entire image. One difference between interlaced and non-interlaced images on a website is that the former one is loaded in a low-quality version first and then it’s quality keeps improving as the website loads whereas a non-interlaced image is loaded in a fixed quality line by line from top to bottom when the website loads.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Avatar |