PHP – exif_imagetype() function

PHPServer Side ProgrammingProgramming

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

ValueConstantValueConstant
1IMAGETYPE_GIF10IMAGETYPE_JP2
2IMAGETYPE_JPEG11IMAGETYPE_JPX
3IMAGETYPE_PNG12IMAGETYPE_JB2
4IMAGETYPE_SWF13IMAGETYPE_SWC
5IMAGETYPE_PSD14IMAGETYPE_IFF
6IMAGETYPE_BMP15IMAGETYPE_WBMP
7IMAGETYPE_TIFF_II (intel byte order)16IMAGETYPE_XBM
8IMAGETYPE_TIFF_MM (motorola byte order)17IMAGETYPE_ICO
9IMAGETYPE_JPC18IMAGETYPE_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
raja
Updated on 12-Oct-2021 06:46:33

Advertisements