PHP: How to Change Image Metadata

Updated: January 12, 2024 By: Guest Contributor Post a comment

Introduction

Image metadata, often known as EXIF (Exchangeable Image File Format) data, holds valuable information about an image, such as the camera used to take the photo, the location at which the photo was taken, the date, time, and more. Manipulating this metadata can be really useful, whether it’s for privacy reasons, organizing images, or correcting information.

In this tutorial, you will learn how to handle and modify image metadata using PHP. Whether you are a beginner or an expert in PHP, this guide will help you understand the essentials and some advanced techniques for handling image metadata.

Understanding Image Metadata

Before we delve into the programming part, let’s first understand what metadata is. Image metadata is divided into several standards, with the most common being EXIF, IPTC (International Press Telecommunications Council), and XMP (Extensible Metadata Platform).

EXIF metadata is automatically added to the image by the digital camera and can include a range of information; from the basic like time and date the image was taken, to the more advanced like GPS data. IPTC metadata is used for adding editorial data such as captions, keywords, and copyright details. XMP is an Adobe product and is a standard created for the creation, processing, and interchange of standardized and custom metadata for digital documents.

Prerequisites

To follow along, you will need:

  • PHP installed on your server or local machine
  • A code editor, like Visual Studio Code or Sublime Text
  • Basic understanding of PHP and working with files in PHP

Reading Image Metadata with PHP

First, we will start by reading the image metadata using PHP’s native functions.

<?php

$imagePath = 'path/to/your/image.jpg';
$exif_data = @exif_read_data($imagePath);

if($exif_data) {
    echo '<pre>';
    print_r($exif_data);
    echo '</pre>';
} else {
    echo 'No EXIF data found for this image.';
}

?>

This script uses the exif_read_data() function to read EXIF metadata from an image. Note that error suppression is used here (@ before the function) to handle images without EXIF data.

Modifying Image Metadata

Unfortunately, PHP does not have native functions to write EXIF metadata. However, you can still modify other types of metadata such as IPTC metadata using the iptcembed() PHP function.

Example: Changing IPTC Metadata

The following example will illustrate how you can modify the copyright and keywords of an image’s IPTC data.

<?php

$imagePath = 'path/to/your/image.jpg';
$info = array(
    '2#040' => 'My New Copyright Notice',
    '2#025' => array('keyword1', 'keyword2', 'keyword3'),
);

$iptc = iptc_make_tag (2, '120', implode(';', $info['2#025']));

// Convert the binary IPTC tags to hexadecimal
$hex = bin2hex(iptc_make_tag(2, 2, 'My Copyright Notice')) . bin2hex($iptc);

// Inject the new IPTC data into the image
$content = iptcembed ('\x1C' . $hex, $imagePath);

To add the data back into the image, you’ll need to use the iptcembed() function along with some knowledge of the IPTC format. This low-level manipulation requires a correct implementation of the binary data structure, which can be challenging.

Using PHP Libraries

Due to the complexity of writing metadata, there are libraries and tools that can help simplify this process. One such tool is the PHP library pel (PHP Exif Library). Another is ImageMagick with the PHP extension imagick.

Here’s how you can use ImageMagick to modify and write metadata:

<?php

$imagePath = 'path/to/your/image.jpg';
$imagick = new Imagick($imagePath);

$imagick->setImageProperty('exif:GPSLatitude', 'GPS latitude value');
$imagick->setImageProperty('exif:GPSLongitude', 'GPS longitude value');

$imagick->writeImage($imagePath);
$imagick->destroy();

?>

This snippet demonstrates how you can use the Imagick class to set the properties of an image. Please note that while Imagick supports setting some EXIF properties, it may not support all of them.

Removing Image Metadata

If you want to remove metadata, perhaps for privacy reasons, the process is quite straightforward with PHP.

Here’s a simple way to strip all metadata from an image using Imagick:

<?php

$imagePath = 'path/to/your/image.jpg';
$imagick = new Imagick($imagePath);

// Strip off all EXIF and other metadata:
$imagick->stripImage();

$imagick->writeImage($imagePath);
$imagick->destroy();

?>

Conclusion

PHP offers several ways to interact with image metadata, though some aspects such as writing EXIF metadata are not as straightforward. Libraries like Imagick and Exiftool for PHP can greatly simplify tasks, and could potentially be a better fit depending on the requirements of your project. By following the examples provided, you are now capable of handling various image metadata requirements using PHP.