exif_imagetype 


php apg

READS the first bytes of an image and checks its signature.





This function can be used to avoid calls to other exif functions with unsupported file types or in conjunction with $_SERVER['HTTP_ACCEPT'] to check whether or not the viewer is able to see a specific image in the browser.

If it is unable to read enough bytes from the file to determine the image type, this function will emit an E_NOTICE and return FALSE.


<?php

int
|false exif_imagetype string $filename )


where,

$filename The image file to be checked

?>

  $filename   


The image file to be checked.



<?php

string image_type_to_mime_type 
int $image_type )


where,

$image_type One the IMAGETYPE_XXX constants
                      
(SEE the below TABLE)

?>

  $image_type   


One the IMAGETYPE_XXX constants.



<?php

array get_defined_constantsbool $categorize false )

where,

$categorize 

?>


  $categorize   


False or true to control and categorize.





<?php

// PHP version dependent display

echo 'PHP ' PHP_VERSION '<br><br>';

$arr get_defined_constants(true);

/*
echo '<pre>';
print_r($arr['standard']);
echo '</pre>';
*/

foreach($arr['standard'] as $k => $v)
{
if(
strchr($k'IMAGETYPE_'))
echo 
"$k => $v => " image_type_to_mime_type($v) . '<br>';
}

?>


IMAGETYPE_XXX_
PHP 8.2.18
VALUECONSTANTTYPE
1IMAGETYPE_GIFimage/gif
2IMAGETYPE_JPEGimage/jpeg
3IMAGETYPE_PNGimage/png
4IMAGETYPE_SWFapplication/x-shockwave-flash
5IMAGETYPE_PSDimage/psd
6IMAGETYPE_BMPimage/bmp
7IMAGETYPE_TIFF_IIimage/tiff
8IMAGETYPE_TIFF_MMimage/tiff
9IMAGETYPE_JPCapplication/octet-stream
10IMAGETYPE_JP2image/jp2
11IMAGETYPE_JPXapplication/octet-stream
12IMAGETYPE_JB2application/octet-stream
13IMAGETYPE_SWCapplication/x-shockwave-flash
14IMAGETYPE_IFFimage/iff
15IMAGETYPE_WBMPimage/vnd.wap.wbmp
9IMAGETYPE_JPEG2000application/octet-stream
16IMAGETYPE_XBMimage/xbm
17IMAGETYPE_ICOimage/vnd.microsoft.icon
18IMAGETYPE_WEBPimage/webp
19IMAGETYPE_AVIFimage/avif
0IMAGETYPE_UNKNOWNapplication/octet-stream
20IMAGETYPE_COUNTapplication/octet-stream
ed48

  1 EXERCISE   

<?php

$arr_img01 
= [ 'E 01 01.gif'
                        
'E 01 02.jpg'
                        
'E 01 03.png'
                        
'E 01 06.bmp'
                        
'E 01 17.ico'
                        
'E 01 18.webp' ];

foreach(
$arr_img01 as $img01)
{
$img_type exif_imagetype('img/' $img01);
echo 
$img01 '<br><br>exif_imagetype = '.  
$img_type '<br><img src="' 'img/' 
$img01 '" alt="' $img01 
'" width="90%" title="' $img01 '"><br>';
$mime_type image_type_to_mime_type($img_type);
echo 
$mime_type '<br><br><br>';
}

?>

 RESULT   

E 01 01.gif

exif_imagetype= 1 => image/gif

apr E 01 01.gif



E 01 02.jpg

exif_imagetype= 2 => image/jpeg

apr E 01 02.jpg



E 01 03.png

exif_imagetype= 3 => image/png

apr E 01 03.png



E 01 06.bmp

exif_imagetype= 6 => image/bmp

apr E 01 06.bmp



E 01 17.ico

exif_imagetype= 17 => image/vnd.microsoft.icon

apr E 01 17.ico



E 01 18.webp

exif_imagetype= 18 => image/webp

apr E 01 18.webp

Artistic design by Auschwitz concentration camp,
(Konzentrationslager Auschwitz).

  2 EXERCISE   

<?php

$arr_img02 
= [ 'E 02 01.klm'
                        
'E 02 02.xyz'
                        
'E 02 02.THM'
                        
'E 02 03.stu' ];

foreach(
$arr_img02 as $img02)
{
$img_type exif_imagetype('img/' $img02);
echo 
$img02 '<br><br>exif_imagetype = '.  
$img_type '<br><img src="' 'img/' 
$img02 '" alt="' $img02 
'" width="90%" title="' $img02 '"><br>';
$mime_type image_type_to_mime_type($img_type);
echo 
$mime_type '<br><br><br>';
}

echo 
'The images used in this exercise have 
unofficial type names.<br><br>
This supports the claim that this function 
reads the first bytes of an image file and 
cheks its signature.'
;

?>

 RESULT   

E 02 01.klm

exif_imagetype = 1 => image/gif

apr E 02 01.klm



E 02 02.xyz

exif_imagetype = 2 => image/jpeg

apr E 02 02.xyz



E 02 02.THM

exif_imagetype = 2 => image/jpeg

apr E 02 02.THM



E 02 03.stu

exif_imagetype = 3 => image/png

apr E 02 03.stu


The images used in this exercise have unofficial type names.

This supports the claim that this function reads the first bytes of an image file and cheks its signature.