PNG file validation

2019-02-23 22:11发布

问题:

I have a Flash web app which displays user submitted PNG files. Files are uploaded to the server via some API prior to being displayed. I'd like to make sure no "bad" files are served to Flash, where "bad" is entirely unspecific. Is there a way to validate PNG files against the PNG specs (this would catch corrupted files)? Or any best pratice on dealing with untrusted image files? I only need to handle PNG, so JPG, GIF etc. support is necessary. Language mostly does not matter, though I'd prefer Python solutions. This is on a Unix webserver.

Thanks, Simon

回答1:

I would suggest you use Python and PIL (Python Imaging Library to do this):

from PIL import Image

v_image = Image.open(file)
v_image.verify()

Catch any exceptions...