Detecting file format?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
webmaster33

Detecting file format?

Post by webmaster33 »

Is it possible to detect the format of a file using ImageMagick?


I would need to detect at least the following formats:
- common image formats (jpg, gif, bmp, tif, tga, etc...)
- common video formats (avi, mpg, etc...)
- common sound formats (wav, mp3, etc...)
- common text/office formats (txt, html, word, excel, access, powerpoint, etc...)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

Sounds more like the UNIX "file" command to me :-)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
webmaster33

Post by webmaster33 »

anthony wrote: Sounds more like the UNIX "file" command to me :-)


Well, I know about the "file" command, but would be needed a platform independent solution, as I develop my website under Windows. The "file" command uses a magic file, where the file footprints are listed. This could be also used by ImageMagick, too.

ImageMagick is platform independent, and such feature would make image detection much easier. And if images are detected, why not detect other file formats, too, so I can tell the visitor, what is wrong with his uploaded file?

I have a website, where users are uploading images. Sometimes they are also uploading files, which are not images at all or they are not supported by ImageMagick. This is why I would welcome a feature, which detects the format of the file. This feature should be also implemented into Perl API, because I use Perl language.


Developers?
Would you consider implementing such feature?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

Unix uses such a file too, with the linux version much more advanced in entries.
However this "/etc/magic" is commonly published, so I am supprised a windows version whcih can use the UNIX 'magic' is not generally available.

For fast file type determination it is a lot faster than IM, as Im is restricted to just formats it knows about and often needs to read in to identify. Mind you for images IM identify can get you a lot more info, like Exif Metadata, but of course the price is speed.

Your choice.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
webmaster33

Post by webmaster33 »

anthony wrote: For fast file type determination it is a lot faster than IM, as Im is restricted to just formats it knows about and often needs to read in to identify. Mind you for images IM identify can get you a lot more info, like Exif Metadata, but of course the price is speed.


Not faster, as Perl needs to use `backtick` operator to spawn perl interpreter, start the external command "file", then get its output. That's why using the ImageMagick Perl API is faster than using the backtick Perl operator.

IMHO, would be logical feature, to add a command which just quickly identify file type, using a short magic string.

Also note, ImageMagick has also the word magic in his name so this also adds a point to implement this feature :)
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Post by magick »

The Wizards Toolkit has a mime-detection method but ImageMagick does not. See http://www.wizards-toolkit.org/
webmaster33

Post by webmaster33 »

magick wrote: The Wizards Toolkit has a mime-detection method but ImageMagick does not. See http://www.wizards-toolkit.org/

Yeah, thanks!
However it seems the Wizard's Toolkit doesn't have a Perl interface.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Post by magick »

Use CPAN. I'm sure there is a mime detection method available for Perl.
webmaster33

Post by webmaster33 »

Thanks, will try it.
Post Reply