Merge pull request #716 from Zankaria/adjust-image-stripping

Enhance image metadata ops
This commit is contained in:
Lorenzo Yario 2024-04-03 14:23:39 -07:00 committed by GitHub
commit 5680d73f08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -154,7 +154,7 @@ function ocr_image(array $config, string $img_path): string {
* @throws RuntimeException Throws on IO errors. * @throws RuntimeException Throws on IO errors.
*/ */
function strip_image_metadata(string $img_path): int { function strip_image_metadata(string $img_path): int {
$err = shell_exec_error('exiftool -overwrite_original -ignoreMinorErrors -q -q -all= ' . escapeshellarg($img_path)); $err = shell_exec_error('exiftool -overwrite_original -ignoreMinorErrors -q -q -all= -Orientation ' . escapeshellarg($img_path));
if ($err === false) { if ($err === false) {
throw new RuntimeException('Could not strip EXIF metadata!'); throw new RuntimeException('Could not strip EXIF metadata!');
} }
@ -1055,12 +1055,14 @@ if (isset($_POST['delete'])) {
if ($size[0] > $config['max_width'] || $size[1] > $config['max_height']) { if ($size[0] > $config['max_width'] || $size[1] > $config['max_height']) {
error($config['error']['maxsize']); error($config['error']['maxsize']);
} }
// If, on the basis of the file extension, the image file has metadata we can operate on.
$file_image_has_operable_metadata = $file['extension'] === 'jpg' || $file['extension'] === 'jpeg' || $file['extension'] === 'webp' || $file['extension'] == 'png';
if ($config['convert_auto_orient'] && ($file['extension'] == 'jpg' || $file['extension'] == 'jpeg')) { if ($file_image_has_operable_metadata && $config['convert_auto_orient']) {
// The following code corrects the image orientation. // The following code corrects the image orientation.
// Currently only works with the 'convert' option selected but it could easily be expanded to work with the rest if you can be bothered. // Currently only works with the 'convert' option selected but it could easily be expanded to work with the rest if you can be bothered.
if (!($config['redraw_image'] || (($config['strip_exif'] && !$config['use_exiftool']) && ($file['extension'] == 'jpg' || $file['extension'] == 'jpeg')))) { if (!($config['redraw_image'] || (($config['strip_exif'] && !$config['use_exiftool'])))) {
if (in_array($config['thumb_method'], array('convert', 'convert+gifsicle', 'gm', 'gm+gifsicle'))) { if (in_array($config['thumb_method'], array('convert', 'convert+gifsicle', 'gm', 'gm+gifsicle'))) {
$exif = @exif_read_data($file['tmp_name']); $exif = @exif_read_data($file['tmp_name']);
$gm = in_array($config['thumb_method'], array('gm', 'gm+gifsicle')); $gm = in_array($config['thumb_method'], array('gm', 'gm+gifsicle'));
@ -1121,7 +1123,7 @@ if (isset($_POST['delete'])) {
$dont_copy_file = false; $dont_copy_file = false;
if ($config['redraw_image'] || (!@$file['exif_stripped'] && $config['strip_exif'] && ($file['extension'] == 'jpg' || $file['extension'] == 'jpeg'))) { if ($config['redraw_image'] || ($file_image_has_operable_metadata && !@$file['exif_stripped'] && $config['strip_exif'])) {
if (!$config['redraw_image'] && $config['use_exiftool']) { if (!$config['redraw_image'] && $config['use_exiftool']) {
try { try {
$file['size'] = strip_image_metadata($file['tmp_name']); $file['size'] = strip_image_metadata($file['tmp_name']);