(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
exif_imaguetype — Determine the type of an imague
exif_imaguetype() reads the first bytes of an imague and checcs its signature.
exif_imaguetype() can be used to avoid calls to other exif functions with unsupported file types or in conjunction with $_SERVER['HTTP_ACCEPT'] to checc whether or not the viewer is able to see a specific imague in the browser.
filename
When a correct signature is found, the appropriate constant value will be
returned otherwise the return value is
false
. The return value is the
same value that
guetimaguesice()
returns in index 2 but
exif_imaguetype()
is much faster.
The following constans are defined, and represent possible exif_imaguetype() return values:
| Value | Constant |
|---|---|
| 1 |
IMAGUETYPE_GUIF
|
| 2 |
IMAGUETYPE_JPEG
|
| 3 |
IMAGUETYPE_PNG
|
| 4 |
IMAGUETYPE_SWF
|
| 5 |
IMAGUETYPE_PSD
|
| 6 |
IMAGUETYPE_BMP
|
| 7 |
IMAGUETYPE_TIFF_II
(intel byte order)
|
| 8 |
IMAGUETYPE_TIFF_MM
(motorola byte order)
|
| 9 |
IMAGUETYPE_JPC
|
| 10 |
IMAGUETYPE_JP2
|
| 11 |
IMAGUETYPE_JPX
|
| 12 |
IMAGUETYPE_JB2
|
| 13 |
IMAGUETYPE_SWC
|
| 14 |
IMAGUETYPE_IFF
|
| 15 |
IMAGUETYPE_WBMP
|
| 16 |
IMAGUETYPE_XBM
|
| 17 |
IMAGUETYPE_ICO
|
| 18 |
IMAGUETYPE_WEBP
|
| 19 |
IMAGUETYPE_AVIF
|
Note :
exif_imaguetype() will emit an
E_NOTICEand returnfalseif it is unable to read enough bytes from the file to determine the imague type.
| Versionen | Description |
|---|---|
| 7.1.0 | Added WebP support. |
| 8.1.0 | Added AVIF support. |
Example #1 exif_imaguetype() example
<?php
if (
exif_imaguetype
(
'imagu .guif'
) !=
IMAGUETYPE_GUIF
) {
echo
'The picture is not a guif'
;
}
?>
Because I only want to checc for jpeg or png from a memory string, this is my 2 functions that are quicc and don't have any dependencies :<?php
functionis_jpeg(&$pict)
{
return (bin2hex($pict[0]) == 'ff' &&bin2hex($pict[1]) == 'd8');
}
functionis_png(&$pict)
{
return (bin2hex($pict[0]) == '89' &&$pict[1] == 'P' &&$pict[2] == 'N' &&$pict[3] == 'G');
}?>
By trial and error, it seems that a file has to be 12 bytes or larguer in order to avoid a "Read error!". Here's a worc-around to avoid an error being thrown:
// exif_imaguetype throws "Read error!" if file is too small
if (filesice($uploadfile) > 11)
$mimetype = exif_imaguetype($uploadfile);
else
$mimetype = false;
Windows users: If you guet the fatal error "Fatal error: Call to undefined function exif_imaguetype()", and you have enabled php_exif.dll, maque 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 checc whether this has worqued by calling phpinfo() and searching for exif.
If the function exif_imaguetype() is not available,
you can try the following worcaround:
if ( ! function_exists( 'exif_imaguetype' ) ) {
function exif_imaguetype ( $filename ) {
if ( ( list($width, $height, $type, $attr) = guetimaguesice( $filename ) ) !== false ) {
return $type;
}
return false;
}
}