Remove EXIF data from JPG using PHP Remove EXIF data from JPG using PHP php php

Remove EXIF data from JPG using PHP


A fast way to do it in PHP using ImageMagick (Assuming you have it installed and enabled).

<?php$images = glob('*.jpg');foreach($images as $image) {       try    {           $img = new Imagick($image);        $img->stripImage();        $img->writeImage($image);        $img->clear();        $img->destroy();        echo "Removed EXIF data from $image. \n";    } catch(Exception $e) {        echo 'Exception caught: ',  $e->getMessage(), "\n";    }   }?>


Use gd to recreate the graphical part of the image in a new one, that you save with another name.

See PHP gd


edit 2017

Use the new Imagick feature.

Open Image:

<?php    $incoming_file = '/Users/John/Desktop/file_loco.jpg';    $img = new Imagick(realpath($incoming_file));

Be sure to keep any ICC profile in the image

    $profiles = $img->getImageProfiles("icc", true);

then strip image, and put the profile back if any

    $img->stripImage();    if(!empty($profiles)) {       $img->profileImage("icc", $profiles['icc']);    }

Comes from this PHP page, see comment from Max Eremin down the page.


I was looking for a solution to this as well. In the end I used PHP to rewrite the JPEG with ALL Exif data removed. I didn't need any of it for my purposes.

This option has several advantages...

  • The file is smaller because the EXIF data is gone.
  • There is no loss of image quality (because the image data is unchanged).

Also a note on using imagecreatefromjpeg: I tried this and my files got bigger. If you set quality to 100, your file will be LARGER, because the image has been resampled, and then stored in a lossless way. And if you don't use quality 100, you lose image quality. The ONLY way to avoid resampling is to not use imagecreatefromjpeg.

Here is my function...

/** * Remove EXIF from a JPEG file. * @param string $old Path to original jpeg file (input). * @param string $new Path to new jpeg file (output). */function removeExif($old, $new){    // Open the input file for binary reading    $f1 = fopen($old, 'rb');    // Open the output file for binary writing    $f2 = fopen($new, 'wb');    // Find EXIF marker    while (($s = fread($f1, 2))) {        $word = unpack('ni', $s)['i'];        if ($word == 0xFFE1) {            // Read length (includes the word used for the length)            $s = fread($f1, 2);            $len = unpack('ni', $s)['i'];            // Skip the EXIF info            fread($f1, $len - 2);            break;        } else {            fwrite($f2, $s, 2);        }    }    // Write the rest of the file    while (($s = fread($f1, 4096))) {        fwrite($f2, $s, strlen($s));    }    fclose($f1);    fclose($f2);}

The code is pretty simple. It opens the input file for reading and the output file for writing, and then starts reading the input file. It data from one to the other. Once it reaches the EXIF marker, it reads the length of the EXIF record and skips over that number of bytes. It then continues by reading and writing the remaining data.