What kind of file types does PHP getimagesize() return?

℡╲_俬逩灬. 提交于 2020-01-01 04:39:07

问题


Does anyone know all the possible results for the 3rd value returned from PHP's getimagesize() function? Example this code below will return:

  • $imageinfo['2'] = 2; for a jpg image,
  • $imageinfo['2'] = 3; for a png image,
  • $imageinfo['2'] = 0; for a gif image.

The numbers might not be correct above but you get the idea.

I can't find on php.net or anywhere else a list of all possible results for the 3rd value.

$imageinfo = getimagesize($imageurl);
$image_type  = $imageinfo['2'];

回答1:


Execute this:

print_r(get_defined_constants());

And then look for constants prefixed with IMAGETYPE_. On my PHP 5.3 installation I got these values:

[IMAGETYPE_GIF] => 1
[IMAGETYPE_JPEG] => 2
[IMAGETYPE_PNG] => 3
[IMAGETYPE_SWF] => 4
[IMAGETYPE_PSD] => 5
[IMAGETYPE_BMP] => 6
[IMAGETYPE_TIFF_II] => 7
[IMAGETYPE_TIFF_MM] => 8
[IMAGETYPE_JPC] => 9
[IMAGETYPE_JP2] => 10
[IMAGETYPE_JPX] => 11
[IMAGETYPE_JB2] => 12
[IMAGETYPE_SWC] => 13
[IMAGETYPE_IFF] => 14
[IMAGETYPE_WBMP] => 15
[IMAGETYPE_JPEG2000] => 9
[IMAGETYPE_XBM] => 16
[IMAGETYPE_ICO] => 17
[IMAGETYPE_UNKNOWN] => 0
[IMAGETYPE_COUNT] => 18

As you can see Flash SWF are considered images, and actually getimagesize() is able to read the width and height of a SWF object. To me it seemed like a curiosity when I first discovered it, that's why mentioned it here.




回答2:


That index contains the value of one of PHP's IMAGETYPE_XXX constants. An entire list of them can be found on that page, towards the bottom. That page doesn't provide the actual INT value of each one but you should be able to print a few to get the values as necessary. You could also do a comparison check if you're looking for a specific one:

if ($imageinfo[2] == IMAGETYPE_IFF) {
  // Code here
}



回答3:


As previously mentioned constants which start with IMAGETYPE in get_defined_constants() list supported image types.

However, if you Just run this and it will tell you which ones are supported:

function imageTypes () {

    $imageTypes = array();

    foreach(get_defined_constants() as $key => $value){
        $keyStart = explode('_', $key);
        if($keyStart[0] == 'IMAGETYPE'){
             array_push($imageTypes, $key);
        }
    }
    return $imageTypes;
}

echo "<pre>";
var_dump(imageTypes());
echo "</pre>";



回答4:


Quoting the manual :

Index 2 is one of the IMAGETYPE_XXX constants indicating the type of the image.

And you can find those constants onh this page, amongst the other defined by GD




回答5:


getimagesize returns a value of one of the following IMAGETYPE_* constants.




回答6:


If you want to convert value returned by getimagesize() as index 2 into something more human-readable, you may consider using a function like this one:

$imageTypeArray = array
(
    0=>'UNKNOWN',
    1=>'GIF',
    2=>'JPEG',
    3=>'PNG',
    4=>'SWF',
    5=>'PSD',
    6=>'BMP',
    7=>'TIFF_II',
    8=>'TIFF_MM',
    9=>'JPC',
    10=>'JP2',
    11=>'JPX',
    12=>'JB2',
    13=>'SWC',
    14=>'IFF',
    15=>'WBMP',
    16=>'XBM',
    17=>'ICO',
    18=>'COUNT'  
);

$size = getimagesize($filename);

$size[2] = $imageTypeArray[$size[2]];

Or something similar.




回答7:


The above referred pages on the image constants only give the image constant names, not the integers stored in the image constants. The scripts above do give the integers, but I found a listing of the integers on an official PHP manual page at http://www.php.net/manual/en/function.exif-imagetype.php



来源:https://stackoverflow.com/questions/1282351/what-kind-of-file-types-does-php-getimagesize-return

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!