Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

php gd - Fix corrupt png (truncated data) in PHP

I have a png which is supposedly corrupt, but still loads in all desktop image viewers, but not php.

The error when calling imagecreatefrompng is:

Warning: imagecreatefrompng(): gd-png: fatal libpng error: Read Error: truncated data in test.php on line 45

Warning: imagecreatefrompng(): gd-png error: setjmp returns error condition in test.php on line 45

Warning: imagecreatefrompng(): 'test.png' is not a valid PNG file in test.php on line 45

Here is the image in question is there anyway of fixing the image in PHP before further processing it? I can fix it by opening it a desktop image viewer and then re-saving as png, but I need to fix any images server side as they come in with this corrupt format.

pngcheck test.png
test.png  file doesn't end with an IEND chunk
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Section 11.2.1 of the PNG standard under the section critical chunks says

A valid PNG datastream shall begin with a PNG signature, immediately followed by an IHDR chunk, then one or more IDAT chunks, and shall end with an IEND chunk. Only one IHDR chunk and one IEND chunk are allowed in a PNG datastream.

As the error says the image does not have an IEND chunk. It is not a valid PHP image, and PHP has every right to refuse to process the image.

With that said, how are the images coming to you. Are you connecting to another system to retrieve the images, pulling them from a database, or are they being uploaded by the user?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...