Skip to content

Instantly share code, notes, and snippets.

@joshuafredrickson
Last active December 21, 2023 10:10
Show Gist options
  • Save joshuafredrickson/cd5ab346992f533ecbe976d365fa36dd to your computer and use it in GitHub Desktop.
Save joshuafredrickson/cd5ab346992f533ecbe976d365fa36dd to your computer and use it in GitHub Desktop.
WordPress Plugin: Automatically remove EXIF data from .jpgs when uploaded
<?php
/**
* Plugin Name: Remove EXIF data
* Plugin URI: https://gist.github.com/joshuafredrickson/cd5ab346992f533ecbe976d365fa36dd
* Version: 1.0.0
* Description: Remove EXIF data from .jpg when uploaded.
* Author: Joshua Fredrickson
* Author URI: https://joshuafredrickson.com
* License: GNU General Public License v2
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
add_action('wp_handle_upload', function ($upload) {
if ($upload['type'] == 'image/jpeg' || $upload['type'] == 'image/jpg') {
$filename = $upload['file'];
// Attempt Imagick first; fallback to gd
if (class_exists('Imagick')) {
$im = new Imagick($filename);
if (!$im->valid()) {
return $upload;
}
try {
$im->stripImage();
$im->writeImage($filename);
$im->clear();
$im->destroy();
} catch (Exception $e) {
error_log('Unable to strip EXIF data: ' . $filename);
}
} elseif (function_exists('imagecreatefromjpeg')) {
$image = imagecreatefromjpeg($filename);
if ($image) {
imagejpeg($image, $filename, '100');
imagedestroy($image);
}
}
}
return $upload;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment