Image metadata contains additional information about a picture, such as the shooting date, camera settings, and location. This information is important for managing and processing images. This article will introduce how to use PHP's ImageExif extension to read and edit this metadata.
ImageExif is an official PHP extension designed to read and manipulate Exif (Exchangeable Image File Format) data embedded in image files. Exif metadata is commonly embedded in JPEG, TIFF, and other image formats. Using this extension makes it easy to access and modify this information.
First, ensure that the ImageExif extension is installed and enabled. You can check your php.ini configuration file for "exif". If it’s not enabled, you can enable it via your extension manager or recompile PHP.
The following example demonstrates how to read the Exif data from an image:
$imagePath = 'test.jpg';
$exifData = exif_read_data($imagePath, 'EXIF');
echo "Shooting Time: " . $exifData['DateTimeOriginal'] . "\n";
echo "Camera Brand: " . $exifData['Make'] . "\n";
echo "Camera Model: " . $exifData['Model'] . "\n";
echo "Focal Length: " . $exifData['FocalLength'] . "mm\n";
echo "Exposure Time: " . $exifData['ExposureTime'] . " seconds\n";
echo "ISO Speed: " . $exifData['ISOSpeedRatings'] . "\n";
If you need to modify the Exif data, you can first read the existing metadata, update the fields you want to change, and then write the updated data back to the image. Here's an example:
$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: Before modifying the Exif data, you must first read the original data into a variable. After editing, use the appropriate function to write the changes back to the image.
With PHP's ImageExif extension, you can efficiently read and edit Exif metadata embedded in images, which is very useful for image processing and management. We hope this article helps you better understand and work with Exif data. Feel free to leave comments for discussion.