Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove EXIF data from JPG using PHP

Is there any way to remove the EXIF data from a JPG using PHP? I have heard of PEL, but I'm hoping there's a simpler way. I am uploading images that will be displayed online and would like the EXIF data removed.

Thanks!

EDIT: I don't/can't install ImageMagick.

like image 911
tau Avatar asked Sep 01 '10 03:09

tau


People also ask

Can EXIF data be removed?

Select all the files you want to delete EXIF metadata from. Right-click anywhere within the selected fields and choose “Properties.” Click the “Details” tab. At the bottom of the “Details” tab, you'll see a link titled “Remove Properties and Personal Information.” Click this link.

How do I remove EXIF data from a photo in Photoshop?

Removing Image Metadata with Photoshop To remove image metadata in Photoshop, use the “Save for Web” option and in the drop-down next to “Metadata” select “None.”


4 Answers

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.

like image 77
Déjà vu Avatar answered Oct 21 '22 06:10

Déjà vu


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";
    }   
}
?>
like image 26
Bill H Avatar answered Oct 21 '22 05:10

Bill H


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.

like image 16
xtempore Avatar answered Oct 21 '22 05:10

xtempore


The following will remove all EXIF data of a jpeg file. This will make a copy of original file without EXIF and remove the old file. Use 100 quality not to loose any quality details of picture.

$path = "/image.jpg";

$img = imagecreatefromjpeg ($path);
imagejpeg ($img, $path, 100);
imagedestroy ($img);

(simple approximation to the graph can be found here)

like image 6
Naveen Web Solutions Avatar answered Oct 21 '22 04:10

Naveen Web Solutions