Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP ImageMagick getImageType returns array... how to associate with type?

The PHP ImageMagick function getImageType returns an array number, but I can't find any documentation as to how to actually associate this with a type.

I need to know if it's a png, jpg, gif, etc...

$image = new Imagick("test.png");
$ext = $image->getImageType();

// prints 6
echo $ext
like image 368
kylex Avatar asked Apr 11 '11 21:04

kylex


2 Answers

I suggest this way which works fine for me

    $image = new Imagick('image.gif');
    $image->getImageFormat();
    echo $image; // output GIF
like image 186
Mik Avatar answered Oct 05 '22 01:10

Mik


You could use getimagesizeto get the image size and type. Image type is return at index pos 2.

1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(orden de bytes intel), 8 = TIFF(orden de bytes motorola), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM.

Php doc for getimagesize() - http://php.net/manual/en/function.getimagesize.php

Example usage:

list($width, $height, $type) = getimagesize('herp-derp.png');
echo $type;

Edit: Useful comment from phpdoc notes....

In addition to thomporter's quick-reference of the output array, here's what PHP 4.4.0 does:

Array[0] = Width 
Array[1] = Height
Array[2] = Image Type Flag
Array[3] = width="xxx" height="xxx"
Array[bits] = bits
Array[channels] = channels
Array[mime] = mime-type

Edit: getImageType returns one of IMAGICK_TYPE_* constants (http://www.phpf1.com/manual/imagick-getimagetype.html). You can find the list of constants here - http://php.net/manual/en/imagick.constants.php .

IMGTYPE constants

imagick::IMGTYPE_UNDEFINED (integer)
imagick::IMGTYPE_BILEVEL (integer)
imagick::IMGTYPE_GRAYSCALE (integer)
imagick::IMGTYPE_GRAYSCALEMATTE (integer)
imagick::IMGTYPE_PALETTE (integer)
imagick::IMGTYPE_PALETTEMATTE (integer)
imagick::IMGTYPE_TRUECOLOR (integer)
imagick::IMGTYPE_TRUECOLORMATTE (integer)
imagick::IMGTYPE_COLORSEPARATION (integer)
imagick::IMGTYPE_COLORSEPARATIONMATTE (integer)
imagick::IMGTYPE_OPTIMIZE (integer)

like image 37
John Himmelman Avatar answered Oct 05 '22 02:10

John Himmelman