Remove EXIF data from JPG using PHP

Remove EXIF data from JPG using PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Stripping EXIF Data from JPGs in PHP Without ImageMagick

You have a JPEG image and you need to strip its metadata before serving it to users. The most common recommendation you'll find is getimagesize() or ImageMagick, but the latter isn't an option here. Fortunately, there is a simpler way that relies entirely on standard PHP functions. You can remove EXIF data using the built-in exif_read_data() and exif_tagname() helpers combined with basic file manipulation, or by leveraging the php-exif extension if it's available in your environment.

The core problem isn't complex; it's about understanding how JPEG files store metadata. EXIF information is embedded within the image file itself, usually in specific segments that are distinct from the pixel data. When you upload an image to a website, leaving this data intact exposes camera settings, GPS coordinates, and even the original filename. Removing it is a straightforward security hygiene step.

If you cannot install ImageMagick or GD library extensions that handle metadata stripping automatically, you can use the exif extension which is often included in standard PHP installations. The approach involves reading the EXIF data, validating it, and then creating a new image file without that metadata attached.

Here is how you can achieve this using the exif extension:

<?php
$imagePath = 'uploads/photo.jpg';
$destinationPath = 'uploads/photo_stripped.jpg';

// Check if the exif extension is loaded
if (!extension_loaded('exif')) {
    throw new RuntimeException('The exif PHP extension is not installed.');
}

// Read the EXIF data
$exifData = @exif_read_data($imagePath);

// If there is EXIF data, we need to strip it. 
// Note: Reading the data doesn't corrupt the file; it just parses the segments.
// To actually remove them, we must rewrite the file or use a specific function.

// Method 1: Using exif_read_data and saving without metadata (requires GD)
// This is the most reliable way to ensure no metadata remains if you have GD enabled.
$imageInfo = getimagesize($imagePath);
if ($imageInfo === false) {
    throw new RuntimeException('Invalid image file.');
}

$sourceImage = imagecreatefromjpeg($imagePath);
if ($sourceImage === false) {
    throw new RuntimeException('Failed to open image.');
}

// Save the image without EXIF data
// The save function for JPEG in PHP does not preserve EXIF by default.
imagejpeg($sourceImage, $destinationPath, 95);

// Free memory
imagedestroy($sourceImage);
?>

This snippet loads the image into memory using imagecreatefromjpeg(), which inherently discards the metadata segments because it only extracts pixel data and color profiles needed for rendering. It then writes a new JPEG file using imagejpeg(). By default, PHP's GD library does not write EXIF tags when saving a JPEG, ensuring the output is clean.

If you are working within a Laravel application or need to handle this at scale, you might consider integrating this logic into your upload controller. However, be aware that simply reading EXIF data with exif_read_data() does not remove it from the file; it only parses it. You must physically rewrite the file to strip the metadata.

Trade-offs and Considerations

The primary trade-off with this method is image quality. When you read a JPEG into memory and write it back out, you are technically re-encoding the image. While high-quality settings (like the 95 parameter in the code above) minimize loss, there is still a negligible amount of recompression happening. For archival purposes or professional photography where bit-perfect fidelity is required, this might be undesirable. In a web context, however, this trade-off is usually acceptable given the security benefit of removing GPS coordinates and camera models.

Another consideration is performance. Reading a large image into memory consumes RAM. If you are processing thousands of images simultaneously, ensure your server has sufficient memory limits configured in php.ini. You can mitigate this by processing images asynchronously or resizing them first if they are unnecessarily large.

Finally, always verify that the exif extension is actually enabled. If it is missing, this approach will fail immediately. While some hosting environments bundle GD with limited metadata support, relying on the explicit removal of EXIF data via re-encoding is the most robust solution when third-party tools like ImageMagick are off the table.

Our network