简体   繁体   中英

Read image dimensions: exif vs gd vs imagemagick?

Processing already done by imagick.

But we have a bunch of files already processed and in one of method want to extract only their width/height.

As I understand, Imagick to getImageWidth reads whole file and also loads it inside it's internal structures.

I want to know -- is exif or gd has some more lightweight methods to get only widht/height of image.

// It's ok to trust to exif meta

I just did a benchmark on 127 PNG images . It seems GD is the fastest among GD and Imagick. If you only want to use ImageMagick then load the file with pingImage first and retrieve width and height. This is the fastest in IM methods. But still 3 times slower than GD.

+------------------------------------+---------------------------+
| Script/Task name                   | Execution time in seconds |
+------------------------------------+---------------------------+
| GD                                 | 0.010726928710938         |
| IM Multiple Instance               | 1.9311830997467           |
| IM Single Instance                 | 2.0554959774017           |
| IM Single Instance identifyImage   | 1.4270191192627           |
| IM Single Instance pingImage       | 0.036259889602661         |
| imagedimension-exif-gd-imagick.php | 5.467                     |
+------------------------------------+---------------------------+

ImageMagic Multiple Instance means initializing different instance for different image

foreach($pngs as $png){
    $im = new Imagick($png);
    list($width, $height) =array($im->getimagewidth(), $im->getimageheight());
}

ImageMagic Single Instance means using a single iterate-able instance of Imagick

$images =  new \Imagick($pngs);
foreach($images as $im){
    list($width, $height) =array($im->getimagewidth(), $im->getimageheight());
}

ImageMagick Single Instance pingImage means getting dimension after loading it using pingImage

$im = new Imagick();
foreach($pngs as $png){
    $im->pingImage($png);
    list($width, $height) =array($im->getimagewidth(), $im->getimageheight());
}

ImageMagick Single Instance identifyImage means identifying image after loading it with pingImage();

$im = new Imagick();
foreach($pngs as $png){
    $im->pingImage($png);
    $im_info = $im->identifyImage();
    extract($im_info['geometry']);
    //echo $width. "x$height\n";
}

And finaly the GD,

foreach($pngs as $png) {
    list($width, $height) = getimagesize ($png);
}

Note: Exif extension can not get image dimension . This is written in PHP manual,

Height and Width are computed the same way getimagesize() does so their values must not be part of any header returned. Also, html is a height/width text string to be used inside normal HTML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM