Current Location: Home> Latest Articles> Complete Guide to Using PHP ImageExif Extension for Reading and Editing Image Metadata

Complete Guide to Using PHP ImageExif Extension for Reading and Editing Image Metadata

M66 2025-06-10

How to Use PHP ImageExif Extension to Read and Edit Image Metadata

Introduction:
Image metadata contains additional information about a photo, such as the shooting time, camera settings, and location. This information is very important for image management and post-processing. PHP’s ImageExif extension allows us to easily read and edit this metadata.

About the ImageExif Extension

ImageExif is an official PHP extension that supports reading and editing Exif (Exchangeable Image File Format) metadata embedded in JPEG, TIFF, and other image formats. With this extension, you can efficiently access shooting details and camera parameters within image files.

Reading Exif Data from Images

First, ensure the ImageExif extension is enabled in your PHP environment. You can check the “exif” setting in your php.ini file to confirm. If it’s not enabled, activate it via the extension manager or recompile PHP.

The following example shows how to read Exif data from an image:

$imagePath = 'test.jpg';
$exifData = exif_read_data($imagePath, 'EXIF');

echo "Shooting Time: " . $exifData['DateTimeOriginal'] . "<br>";
echo "Camera Make: " . $exifData['Make'] . "<br>";
echo "Camera Model: " . $exifData['Model'] . "<br>";
echo "Focal Length: " . $exifData['FocalLength'] . "mm<br>";
echo "Exposure Time: " . $exifData['ExposureTime'] . " seconds<br>";
echo "ISO Speed Ratings: " . $exifData['ISOSpeedRatings'] . "<br>";

Editing Exif Data in Images

To edit Exif data, first read the existing data, modify the desired fields, and then write it back to the image. The following example demonstrates how to change the Exif metadata:

$imagePath = 'test.jpg';
$exifData = exif_read_data($imagePath, 'EXIF');

$exifData['DateTimeOriginal'] = '2022-01-01 12:00:00';
$exifData['Make'] = 'Canon';
$exifData['Model'] = 'EOS 5D Mark IV';
$exifData['FocalLength'] = '50/1';
$exifData['ExposureTime'] = '1/100';
$exifData['ISOSpeedRatings'] = '400';

exif_write_data($exifData, $imagePath);

Note that before editing, you must ensure the original data is correctly loaded. After modification, calling the write function updates the metadata in the image file.

Conclusion

Using PHP’s ImageExif extension makes reading and modifying image Exif metadata simple and efficient. This greatly facilitates image information management and subsequent processing. We hope this guide helps you better utilize PHP to handle image metadata.