Imagick usage for simple image attributes

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
fRAiLtY-
Posts: 8
Joined: 2011-05-18T08:01:54-07:00
Authentication code: 8675308

Imagick usage for simple image attributes

Post by fRAiLtY- »

Hi guys,

I'm trying to return the following image attributes:

Resolution (in dpi): xxx
Colorspace: xxx
Size (mm): xxx

At the moment I can get the resolution by doing the following:

Code: Select all

<?php echo str_replace(' PixelsPerCentimeter','',round(exec("identify -format %x x %y -units PixelsPerInch $_uploadedFilePath"))) ?>
... which is quite long winded, can I get this via Imagick? The documentation I find is very poor!

Also with the colorspace, I can get it to return an integer by the following:

Code: Select all

<?php echo $_image->getImageResolution() ?>
... but it only returns 0 or 1 or whatever, I want to return a string like simply "RGB" or "CMYK", like it does if you do identify -verbose image.png

And lastly I can get the dimensions of the image with the following:

Code: Select all

<?php echo exec("identify -format %wx%h $_uploadedFilePath") ?>
... but don't seem to be able to return it as millimeters or even centimeters as I can do the maths from there!

I've just installed Imagick after working with exec in PHP and am struggling to get my head around the functions at the moment. Any help at all on the above would be great!

Cheers.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Imagick usage for simple image attributes

Post by fmw42 »

For colorspace see string format, identify -format "%[colorspace]" from http://www.imagemagick.org/script/escape.php. You may also get what you want for size by using the size and resolution and convert to mm using fx escapes.

convert yourimage -format "%[fx:w*$xres*25.4]" info:

( or just do the computation in PHP )

where w is image width
$xres is a variable that you create from your %x for resolution in pixelsperinch
25.4 is the conversion from inches to mm

see
http://www.imagemagick.org/Usage/transform/#fx_escapes

I have no idea if Imagick supports string formats and fx escapes.
Last edited by fmw42 on 2012-06-11T14:22:58-07:00, edited 1 time in total.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Imagick usage for simple image attributes

Post by Bonzo »

I have started to try and write an example of all the Imagick functions and it is hard work due to the lack of documentation as you say.

Code: Select all

$im = new Imagick($input);
$resolution = $im->getImageResolution();
print_r($resolution);

$im = new Imagick($input);
$width = $im->getImageWidth();
echo "<br>Image width = $width";

$im = new Imagick($input);
$height = $im->getImageHeight();
echo "<br>Image height = $height";
Colorspace returns a 1 for me as well and needs more research. You can calculate the size in cm or mm knowing the resolution and dimensions. I have just rembered my Imagick version is to old.

I think there is another way to get the width and height in an array but am getting a bit confused with it all at the moment!
DJ Mike
Posts: 33
Joined: 2010-06-29T19:07:53-07:00
Authentication code: 8675308

Re: Imagick usage for simple image attributes

Post by DJ Mike »

Imagick::getImageResolution
http://eclecticdjs.com/mike/tutorials/p ... lution.php

$resolution = $image->getImageResolution();
echo "<b>Resolution:</b> x=$resolution[x], y=$resolution[y]";
DJ Mike's Tutorials: PHP
ImageMagick Functions
http://eclecticdjs.com/mike/tutorials/p ... /index.php
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Imagick usage for simple image attributes

Post by fmw42 »

fRAiLtY-
Posts: 8
Joined: 2011-05-18T08:01:54-07:00
Authentication code: 8675308

Re: Imagick usage for simple image attributes

Post by fRAiLtY- »

Hi,

The problem is getImageColorspace() returns an integer, I guess I could switch/case it but I was hoping for a better solution from the wrapper! Telling me the colorspace is 1 is little help, I wanted to know whether it's RGB or CMYK, but as I say if necessary I'll switch/case it.

As regards the resolution, I don't really want/need a X x Y value, at present I have this:

Code: Select all

<?php echo str_replace(' PixelsPerCentimeter','',round(exec("identify -format %x x %y -units PixelsPerInch $_uploadedFilePath"))) ?>
.. This currently returns the correct value, in the case of my test image, 300dpi. It would be nice if there's a way outside of exec, i.e. through the wrapper?

I'll have a go with the inches to mm thing now!

Cheers
Post Reply