PHP – exif_imagetype() function



The EXIF (Exchangeable image file format) PHP extension enables to work with the metadata from the images taken by digital devices like digital cameras, cell phones, etc. It depends on the image file format. We can retrieve embedded thumbnails of images.

The exif_imagetype() function in PHP is used to determine the type of an image. This function reads the first bytes of a given image and checks its signature. It can also 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 can see the specific image in the browser.

Syntax

integer exif_imagetype($str filename)

Parameters

exif_imagetype() accepts only a single parameter $filename that is used to hold the image name.

Return Values

When a correct signature is found, then exif_imagetype() returns the appropriate constant value; else it returns False.

List of Imagetype Constants

Value Constant Value Constant
1 IMAGETYPE_GIF 10 IMAGETYPE_JP2
2 IMAGETYPE_JPEG 11 IMAGETYPE_JPX
3 IMAGETYPE_PNG 12 IMAGETYPE_JB2
4 IMAGETYPE_SWF 13 IMAGETYPE_SWC
5 IMAGETYPE_PSD 14 IMAGETYPE_IFF
6 IMAGETYPE_BMP 15 IMAGETYPE_WBMP
7 IMAGETYPE_TIFF_II (intel byte order) 16 IMAGETYPE_XBM
8 IMAGETYPE_TIFF_MM (motorola byte order) 17 IMAGETYPE_ICO
9 IMAGETYPE_JPC 18 IMAGETYPE_WEBP

Example 1

<?php
   // Load an image from local derive
   $filetype = exif_imagetype('C:\xampp\htdocs\test\office.jpg');

   //Output
   echo "The file type is: ", $filetype;
?>

Here we have used the following jpg image −

Output

It will produce the following output −

The file type is: 2

Example 2

Let us now take a "png" image and run the same code −

<?php
   // Load an image from local derive
   $filetype = exif_imagetype('C:\xampp\htdocs\test\img46.png');

   //Output
   echo "The file type is: ", $filetype;
?>

Here we have used the following png image −

Output

It will produce the following output −

The file type is: 3
Updated on: 2021-10-12T06:46:33+05:30

929 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements