Home » Php » PHP ImageMagick getImageType returns array… how to associate with type?

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

Posted by: admin July 12, 2020 Leave a comment


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
How to&Answers:

I suggest this way which works fine for me

    $image = new Imagick('image.gif');
    echo $image; // output GIF


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_PALETTE (integer)
imagick::IMGTYPE_PALETTEMATTE (integer)
imagick::IMGTYPE_TRUECOLOR (integer)
imagick::IMGTYPE_OPTIMIZE (integer)


A good and quick way to validate images is:

$ identify -format %m image.jpg

in PHP:

// If its not a valid image, returns an empty string.
$image_type = exec('identify -format %m image.jpg');

More about using -format: imagemagick formats


getImageType() returns an array if it’s successful as per this page.


maybe try a vardump or a print_r of $ext?


this is the best I could do for you this will give you the extension and you can use that first before you load image

$path_parts  = pathinfo("test.png");
echo $path_parts ['extension'];