Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
PHP – exif_read_data() function
The exif_read_data() function in PHP reads the EXIF (Exchangeable image file format) headers from an image file. This function extracts metadata information such as camera settings, GPS coordinates, timestamps, and technical details from JPEG and TIFF images.
Note: This function requires the EXIF extension to be enabled in PHP. On most systems, it's enabled by default, but you can check with
extension_loaded('exif').
Syntax
array|false exif_read_data( string $file, string $sections = null, bool $arrays = false, bool $thumbnail = false )
Parameters
exif_read_data() accepts the following four parameters −
$file − The path to the image file (JPEG or TIFF format).
$sections − Comma−separated list of sections to include in the result (e.g., "FILE,COMPUTED,IFD0").
$arrays − Whether to present each section as an array. Default is false.
$thumbnail − Whether to read thumbnail data. Default is false.
Return Value
Returns an associative array containing EXIF data on success, or false on failure.
Example 1: Basic EXIF Data Reading
Here's how to read EXIF data from an image file ?
<?php
// Read EXIF data from an image file
$exifData = exif_read_data('/path/to/image.jpg');
if ($exifData !== false) {
echo "File Name: " . $exifData['FileName'] . "<br>";
echo "File Size: " . $exifData['FileSize'] . " bytes<br>";
echo "Image Width: " . $exifData['COMPUTED']['Width'] . "<br>";
echo "Image Height: " . $exifData['COMPUTED']['Height'] . "<br>";
echo "MIME Type: " . $exifData['MimeType'] . "<br>";
} else {
echo "Failed to read EXIF data";
}
?>
Example 2: Reading Specific Sections
You can specify which EXIF sections to read for better performance ?
<?php
// Read only FILE and COMPUTED sections
$exifData = exif_read_data('/path/to/image.jpg', 'FILE,COMPUTED');
if ($exifData) {
print_r($exifData);
} else {
echo "No EXIF data found or file not accessible";
}
?>
Common EXIF Data Fields
| Field | Description | Example Value |
|---|---|---|
| FileName | Name of the image file | image.jpg |
| FileSize | Size of file in bytes | 45686 |
| COMPUTED['Width'] | Image width in pixels | 640 |
| COMPUTED['Height'] | Image height in pixels | 358 |
| MimeType | MIME type of the image | image/jpeg |
Error Handling
Always check if the function returns false to handle cases where EXIF data cannot be read ?
<?php
$imagePath = '/path/to/image.jpg';
if (!file_exists($imagePath)) {
echo "File does not exist";
} else {
$exifData = exif_read_data($imagePath);
if ($exifData === false) {
echo "Unable to read EXIF data from this image";
} else {
echo "EXIF data successfully read";
// Process EXIF data
}
}
?>
Conclusion
The exif_read_data() function is essential for extracting metadata from images in PHP. It's particularly useful for photo management applications, image galleries, and any system that needs to process image metadata efficiently.
