Current Location: Home> Latest Articles> How to Use PHP and Exif Extension to Read Focus Distance from Photos

How to Use PHP and Exif Extension to Read Focus Distance from Photos

gitbox 2025-06-28

Introduction

When taking photos, in addition to basic parameters like focus and exposure, the Exif data of the photo contains other useful information, such as focus distance, ISO sensitivity, shutter speed, etc. This article will explain how to use PHP and the Exif extension to read the focus distance of a photo.

Exif Extension

Exif (Exchangeable Image File Format) is a metadata format used for photos, which stores shooting parameters and other relevant information. PHP provides an Exif extension that allows us to read and process this metadata.

First, we need to ensure that the Exif extension is installed and enabled. You can check your php.ini file for the following line to verify:

extension=exif

If the line is not found, you can manually add it and restart the web server.

Once the Exif extension is enabled, we can use PHP's Exif-related functions to read Exif data from photos.

Reading Focus Distance

Exif data includes various information, including the focus distance. To read the focus distance, we can use the exif_read_data() function, passing the photo's path as a parameter.

Here is a simple example:


$filename = 'path/to/photo.jpg';
$exif = exif_read_data($filename);
if (isset($exif['FocusDistance'])) {
    $focusDistance = $exif['FocusDistance'];
    echo "Focus Distance: " . $focusDistance;
} else {
    echo "Unable to retrieve focus distance information";
}

In this code, we first specify the path to the photo, then use the exif_read_data() function to read the Exif data. By checking the FocusDistance key in the $exif array, we can retrieve the focus distance value.

Complete Example

Here is a complete example demonstrating how to use PHP and the Exif extension to read the focus distance from a photo:


$filename = 'path/to/photo.jpg';
$exif = exif_read_data($filename);
if (isset($exif['FocusDistance'])) {
    $focusDistance = $exif['FocusDistance'];
    echo "Focus Distance: " . $focusDistance;
} else {
    echo "Unable to retrieve focus distance information";
}

In this complete example, we assume the photo path is path/to/photo.jpg. Using the exif_read_data() function, we read the Exif data, and then retrieve the focus distance value from $exif['FocusDistance']. Finally, we output the focus distance information or an error message.

Conclusion

By using PHP and the Exif extension, we can easily read the focus distance of a photo. This opens up more possibilities when processing photos. We hope this article helps you make better use of Exif data to extract valuable information from your images.