Page 1 of 1

Imagick usage for simple image attributes

Posted: 2012-06-11T11:38:40-07:00
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.

Re: Imagick usage for simple image attributes

Posted: 2012-06-11T11:52:20-07:00
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.

Re: Imagick usage for simple image attributes

Posted: 2012-06-11T12:34:10-07:00
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!

Re: Imagick usage for simple image attributes

Posted: 2012-06-11T14:00:23-07:00
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]";

Re: Imagick usage for simple image attributes

Posted: 2012-06-11T14:17:27-07:00
by fmw42

Re: Imagick usage for simple image attributes

Posted: 2012-06-12T00:24:55-07:00
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