PNG文件验证文件、PNG

2023-09-08 13:01:36 作者:南筏

我有一个Flash网页应用程序,它会显示用户提交的PNG文件。文件上传到通过一些API服务器显示之前。我想,以确保没有坏的文件投放到Flash中,这里的坏是完全不确定的。有没有一种方法来验证PNG文件对PNG规范(这将赶上损坏的文件)?或者在处理不受信任的图像文件,任何最好的初步实践?我只需要处理PNG,所以JPG,GIF等的支持是必要的。语言大多没有关系,但我想preFER Python的解决方案。这是一个Unix网络服务器。

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.

谢谢,西门

推荐答案

我会建议你使用Python和PIL(Python图像库这样做):

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...