Page 1 of 1

Best way to identify valid image file

Posted: 2009-04-17T14:12:29-07:00
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

Re: Best way to identify valid image file

Posted: 2009-04-22T21:33:56-07:00
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