How can I detect whether an image is a PNG or APNG format? [duplicate]
Possible Duplicate:
Can I programatically determine if a PNG is animated?
APNG is backwards compatible with PNG. I opened up an apng and png file in a hex editor and the first few bytes look identical. So if a user uploads either of these formats, how do I detect what the format really is? I've seen this done on some sites that blo开发者_JS百科ck apng.
I'm guessing the ImageMagick library makes this easy, but what if I were to do the detect without the use of an image processing library (for learning purposes)? Can I look for specific bytes that tell me if the file is apng?
Solutions in any language is welcome.
From http://en.wikipedia.org/wiki/Apng:
Among users and maintainers of the PNG and MNG formats, APNG was not well received. In particular, PNG was conceived to be a single-image format [2]. APNG hides the subsequent frames in PNG ancillary chunks in such a way that APNG-unaware applications would ignore them, but there are otherwise no changes to the format to allow software to distinguish between animated and non-animated images. [italics mine]
[2] "PNG (Portable Network Graphics) Specification, Version 1.1#8.4. Multiple-image extension"
See the solution of Can I programatically determine if a PNG is animated?: you could search for the string "acTL".
APNG and PNG files' first few bytes' control can be bypass, easily . So this method isn't secure. User can change file's hex code and pass your this control.
You can use libmagic which is already installed on most Unix platforms.
精彩评论