Best way to identify valid image file

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
signsrus

Best way to identify valid image file

Post by signsrus »

I need a solid way to identify if an image is a valid, uncorrupted image that is tested in the following formats:

.jpg, .png, .bmp, .tiff' .tif, .jpeg, .gif, .eps, .pdf, .ai, .psd

I have been using this in PHP

Code: Select all

// Make sure image is actually an image
$limit = '-limit memory 16 -limit map 32 -limit area 64 -limit disk 128';
$call = "/usr/bin/identify $limit {$_FILES['Filedata']['tmp_name']}";
exec($call, $id_output, $id_status);
if($id_output[0]) { // Checks for identifying output string
doWhatever();
}
Is there a better, faster or easier way? Without taking a lot of system resources.

Thank you,

~Shawn
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Best way to identify valid image file

Post by anthony »

Under linux system the first step is probbaly to use the 'file' command whcih contains simular magic to IM, but does not read or process the whole file, just the first block. that should let you JUNK or ignore any 'unknown' or 'not supported' image formats.

Image Magick does this itself internally so that it decodes as many image file formats as posible, but it will always attempt to decode the image regardless of if it is a format you plan to 'support' or not. This is often regarded as a good thing as it means you can accept more images formats, but may only be usful if you always process the input images ton another 'standard' image format for saving or using.

So limit the formats or not limit them. It is up to you.


NOTE; look at general operational settings for handling of errors/problems/warnings
http://www.imagemagick.org/Usage/basics ... /#controls
Especially look at -quiet and -regard-warnings
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply