exif_imagetype

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

exif_imagetype判断一个图像的类型

说明

exif_imagetype(string $filename): int|false

exif_imagetype() 读取一个图像的第一个字节并检查其签名。

本函数可用来避免调用其它 exif 函数用到了不支持的文件类型上或和 $_SERVER['HTTP_ACCEPT'] 结合使用来检查浏览器是否可以显示某个指定的图像。

参数

filename
被检查的图像文件名。

返回值

如果发现了恰当的签名则返回一个对应的常量,否则返回 false。返回值和 getimagesize() 返回的数组中的索引 2 的值是一样的,但本函数快得多。

定义有以下常量,并代表了 exif_imagetype() 可能的返回值:

图像类型常量
常量
1 IMAGETYPE_GIF
2 IMAGETYPE_JPEG
3 IMAGETYPE_PNG
4 IMAGETYPE_SWF
5 IMAGETYPE_PSD
6 IMAGETYPE_BMP
7 IMAGETYPE_TIFF_II(小端字节顺序)
8 IMAGETYPE_TIFF_MM(大端字节顺序)
9 IMAGETYPE_JPC
10 IMAGETYPE_JP2
11 IMAGETYPE_JPX
12 IMAGETYPE_JB2
13 IMAGETYPE_SWC
14 IMAGETYPE_IFF
15 IMAGETYPE_WBMP
16 IMAGETYPE_XBM
17 IMAGETYPE_ICO
18 IMAGETYPE_WEBP
19 IMAGETYPE_AVIF

注意:

如果无法从文件中读取足够的字节来确定图像类型,exif_imagetype() 将发出 E_NOTICE 并返回 false

更新日志

版本 说明
7.1.0 新增 WebP 支持。
8.1.0 新增 AVIF 支持。

示例

示例 #1 exif_imagetype() 示例

<?php

if (exif_imagetype("image.gif") != IMAGETYPE_GIF) {
echo
"The picture is not a gif";
}

?>

参见

add a note add a note

User Contributed Notes 7 notes

up
22
Tim
17 years ago
By trial and error, it seems that a file has to be 12 bytes or larger in order to avoid a "Read error!".  Here's a work-around to avoid an error being thrown:

// exif_imagetype throws "Read error!" if file is too small
if (filesize($uploadfile) > 11)
    $mimetype = exif_imagetype($uploadfile);
else
    $mimetype = false;
up
24
christophe dot tournayre at univ*bpclermont dot Fr
11 years ago
Because I only want to check for jpeg or png from a memory string, this is my 2 functions that are quick and don't have any dependencies :

<?php
 
function is_jpeg(&$pict)
  {
    return (
bin2hex($pict[0]) == 'ff' && bin2hex($pict[1]) == 'd8');
  }

  function
is_png(&$pict)
  {
    return (
bin2hex($pict[0]) == '89' && $pict[1] == 'P' && $pict[2] == 'N' && $pict[3] == 'G');
  }
?>
up
15
admin at leonard !spam challis dot com
14 years ago
Windows users: If you get the fatal error "Fatal error:  Call to undefined function exif_imagetype()", and you have enabled php_exif.dll, make sure you enable php_mbstring.dll. You must put mbstring before exif in the php.ini, i.e.:

extension=php_mbstring.dll
extension=php_exif.dll

You can check whether this has worked by calling phpinfo() and searching for exif.
up
11
tom dot ghyselinck at telenet dot be
16 years ago
If the function exif_imagetype() is not available,
you can try the following workaround:

if ( ! function_exists( 'exif_imagetype' ) ) {
    function exif_imagetype ( $filename ) {
        if ( ( list($width, $height, $type, $attr) = getimagesize( $filename ) ) !== false ) {
            return $type;
        }
    return false;
    }
}
up
-16
vuatintac at yahoo dot com
9 years ago
to checking file is image, I used this:

function is_image($path)
{
    $a = getimagesize($path);
    $image_type = $a[2];

    if(in_array($image_type , array(IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP)))
    {
        return true;
    }
    return false;
}
up
-19
Anonymous
18 years ago
Seems to give a 'Read error' warning if the size of the file is very small (2 bytes). I think this is because it needs a min 3 bytes to determine the file type
up
-23
tom at tomvergote dot be
20 years ago
libexif can also be used to parse image info out of id3 tags:

exif_read_data("mp3_with_2.4ID3TAGS, '', true, false);
To Top