Remove EXIF data from JPG using PHP
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.
How to remove exif from a JPG without losing image quality?
Consider keeping the ICC profile (which causes richer colors) while removing all other EXIF data:
- Extract the ICC profile
- Strip EXIF data and image profile
- Add the ICC profile back
In PHP + imagick:
$profiles = $img->getImageProfiles("icc", true);
$img->stripImage();
if(!empty($profiles))
$img->profileImage("icc", $profiles['icc']);
(Important note: using the ImageMagick 3.1.0 beta, the result I got from getImageProfiles()
was slightly different from the documentation. I'd advise playing around with the parameters until you get an associative array with the actual profile(s).)
For command line ImageMagick:
convert image.jpg profile.icm
convert image.jpg -strip -profile profile.icm output.jpg
Images will get recompressed of course if you use ImageMagick, but at least colors stay intact.
Hope this helps.
Prevent imagejpeg() from saving EXIF data for images (spec. FileDateTime)
Apparently, GD doesn't like when the path to the input / output file is the same, but the credit isn't mine. To fix, use a new (tmp) file to save the newly created image to:
<?php
...
if(!imagecopyresampled($new_cover, $original_cover, 0, 0, 0, 0, $new_width, $new_height, $width, $height)){
return false; // Could not copy image
}
// Create a tmp file.
$cover_new = tempnam('/tmp', 'cover-');
// Use $cover_new instead of $cover
if(!imagejpeg($new_cover, $cover_new, 100)){
return false; // Image could not be saved to tmp file
}
// Use $cover_new instead of $cover
$file_hash = md5_file($cover_new);
$duplicate_book_cover = $this->find_duplicate_book_cover($book, $file_hash);
if($duplicate_book_cover){
return $duplicate_book_cover;
}
// Use $cover_new instead of $cover
$file_id = $c_consummo->upload_file($cover_new, $filename);
...
Related Topics
How to Convert Output of Number_Format Back to Numbers in PHP
What Causes: "Notice: Uninitialized String Offset" to Appear
Unexpected Character in Input: '\' (Ascii=92) State=1
Selecting Multiple Array Elements
Turn Off Warnings and Errors on PHP and MySQL
How to Read a List of Files from a Folder Using PHP
Dom Parser That Allows HTML5-Style </ in <Script> Tag
How to Delete a Folder with Contents Using PHP
Url Rewriting in PHP Without Htaccess
Read Large Data from CSV File in PHP
Leverage Browser Caching for 3Rd Party Js
How to Store Null Values in Datetime Fields in MySQL
Hiding True Database Object Id in Url'S
Regex Backreference to Match Different Values
Why Should I Use Templating System in PHP
How to Write JavaScript Code Inside PHP
How Get Value for Unchecked Checkbox in Checkbox Elements When Form Posted