Search code examples
phppngimagecreatefrompng

PHP increase chunk data size for an image


I have an PNG image stored inside a variable. The file is not corrupted, but big (26,7mb / 2908x3444 pixels). I want to load the image and have already used imagecreatefromstring on many other images. But on this image it fails with "zTXt: chunk data is too large". I thought it could be caused by imagecreatefromstring, so I installed Imageick and tried readImageBlob, but I get the same error.

How can I load images with bis filesize?

Testscript:

<?php

$picture = file_get_contents('https://live.staticflickr.com/65535/49177974597_69c2581172_o_d.png');
$image=imagecreatefromstring($picture);

header('Content-type:image/png');
header("Content-Disposition: inline; filename=picture.png");
echo imagepng($image);

?>

EDIT: Álvaro González answer is correct, the image is damaged. I would have never ever expected that the image is corrupted, because it is displayed in most of all image viewers (including the Windows Picture Viewer). Gimp was the right tool to analyze which picture damaged and which not.

In my specific case this .png image, which is downloaded inside my PHP script, was generated by a previous script of mine.

This script has downloaded the original image (which was a .tiff file), removes the alpha and converted it to png.

My mistake was, that I done the following with Imagick: 1. Download file 2. Remove alpha 3. Flatten 4. Remove alpha 5. Convert to png 6. Save

Now I do the following: 1. Download file 2. Convert to png 3. Remove alpha 4. Save

The image is now visible in all image Viewers (including Gimp and Windows Picture Viewer), and is correct displayed when I want to output it in PHP.


Solution

  • "zTXt: chunk data is too large" means that the file is corrupted. To confirm that you can open it with e.g. GIMP:

    GIMP: error while reading

    It tends to happen that tools from major vendors (e.g. Chrome or Firefox) are more resilient and can often recover corrupted data and display it normally, but smaller open source tools (e.g. GIMP or PHP) just clash.