Question

I'm currently using ImageMagick to determine the size of images uploaded to the website. By calling ImageMagick's "identify" on the command line it takes about 0.42 seconds to determine a 1MB JPEG's dimensions along with the fact that it's a JPEG. I find that a bit slow.

Using the Imagick PHP library is even slower as it attemps to load the whole 1MB in memory before doing any treatment to the image (in this case, simply determining its size and type).

Are there any solutions to speed up this process of determining which file type and which dimensions an arbitrary image file has? I can live with it only supporting JPEG and PNG. It's important to me that the file type is determined by looking at the file's headers and not simply the extension.

Edit: The solution can be a command-line tool UNIX called by PHP, much like the way I'm using ImageMagick at the moment

Was it helpful?

Solution

Sorry I can't add this as a comment to a previous answer but I don't have the rep. Doing some quick and dirty testing I also found that exec("identify -ping... is about 20 times faster than without the -ping. But getimagesize() appears to be about 200 times faster still.

So I would say getimagesize() is the faster method. I only tested on jpg and not on png.

the test is just

$files = array('2819547919_db7466149b_o_d.jpg', 'GP1-green2.jpg', 'aegeri-lake-switzerland.JPG');
foreach($files as $file){
  $size2 = array();
  $size3 = array();
  $time1 = microtime();
  $size = getimagesize($file);
  $time1 = microtime() - $time1;
  print "$time1 \n";
  $time2 = microtime();
  exec("identify -ping $file", $size2);
  $time2 = microtime() - $time2;
  print $time2/$time1 . "\n";
  $time2 = microtime();
  exec("identify $file", $size3);
  $time2 = microtime() - $time2;
  print $time2/$time1 . "\n";
  print_r($size);
  print_r($size2);
  print_r($size3);
}

OTHER TIPS

If you're using PHP with GD support, you can try getimagesize().

Have you tried

identify -ping filename.png

?

It's important to me that the file type is determined by looking at the file's headers and not simply the extension.

For that you can use 'file' unix command (orsome php function that implements the same functionality).

/tmp$ file stackoverflow-logo-250.png
stackoverflow-logo-250.png: PNG image data, 250 x 70, 8-bit colormap, non-interlaced

Actually, to use getimagesize(), you do NOT need to have GD compiled in.

You can also use mime_content_type() to get the MIME type.

exif_imagetype() is faster than getimagesize().

$filename = "somefile";
$data = exif_imagetype($filename);
echo "<PRE>";
print_r($data);
echo "</PRE>";

output:

Array (
        [FileName] => somefile
        [FileDateTime] => 1234895396
        [FileSize] => 15427
        [FileType] => 2
        [MimeType] => image/jpeg
        [SectionsFound] => 
        [COMPUTED] => Array
            (
                [html] => width="229" height="300"
                [Height] => 300
                [Width] => 229
                [IsColor] => 1
        )
)

If you're using PHP I'd suggest using the Imagick library rather than calling exec(). The feature you're looking for is Imagick::pingImage().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top