Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to determine image resolution and file type in PHP or Unix command line?

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

like image 605
Gilles Avatar asked Sep 18 '08 07:09

Gilles


3 Answers

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

like image 142
J D OConal Avatar answered Nov 15 '22 06:11

J D OConal


Have you tried

identify -ping filename.png

?

like image 23
Vinko Vrsalovic Avatar answered Nov 15 '22 06:11

Vinko Vrsalovic


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);
}
like image 39
Steven Noble Avatar answered Nov 15 '22 06:11

Steven Noble