kylex
kylex

Reputation: 14426

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

Upvotes: 8

Views: 4949

Answers (5)

Mik
Mik

Reputation: 1703

I suggest this way which works fine for me

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

Upvotes: 6

Dzion
Dzion

Reputation: 1080

A good and quick way to validate images is:

$ identify -format %m image.jpg
JPEG

in PHP:

<?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

Upvotes: 1

John Himmelman
John Himmelman

Reputation: 22010

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)

Upvotes: 3

mcgrailm
mcgrailm

Reputation: 17638

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'];

Upvotes: -1

anoopb
anoopb

Reputation: 543

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

http://php.net/manual/en/function.imagick-getimagetype.php

maybe try a vardump or a print_r of $ext?

Upvotes: 0

Related Questions