1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-11-23 23:20:57 +01:00

Merge pull request #698 from Zankaria/calc-file-size-after-strip

Calculate the file size after stripping the metadata off an image.
This commit is contained in:
Lorenzo Yario 2024-03-30 22:15:10 -07:00 committed by GitHub
commit 38b47a0844
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -165,6 +165,26 @@ function ocr_image(array $config, string $img_path): string {
return trim($ret); return trim($ret);
} }
/**
* Trim an image's EXIF metadata
*
* @param string $img_path The file path to the image.
* @return int The size of the stripped file.
* @throws RuntimeException Throws on IO errors.
*/
function strip_image_metadata(string $img_path): int {
$err = shell_exec_error('exiftool -overwrite_original -ignoreMinorErrors -q -q -all= ' . escapeshellarg($img_path));
if ($err === false) {
throw new RuntimeException('Could not strip EXIF metadata!');
}
clearstatcache(true, $img_path);
$ret = filesize($img_path);
if ($ret === false) {
throw new RuntimeException('Could not calculate file size!');
}
return $ret;
}
/** /**
* Method handling functions * Method handling functions
*/ */
@ -1089,9 +1109,15 @@ if (isset($_POST['delete'])) {
if ($config['redraw_image'] || (!@$file['exif_stripped'] && $config['strip_exif'] && ($file['extension'] == 'jpg' || $file['extension'] == 'jpeg'))) { if ($config['redraw_image'] || (!@$file['exif_stripped'] && $config['strip_exif'] && ($file['extension'] == 'jpg' || $file['extension'] == 'jpeg'))) {
if (!$config['redraw_image'] && $config['use_exiftool']) { if (!$config['redraw_image'] && $config['use_exiftool']) {
if($error = shell_exec_error('exiftool -overwrite_original -ignoreMinorErrors -q -q -all= ' . try {
escapeshellarg($file['tmp_name']))) $file['size'] = strip_image_metadata($file['tmp_name']);
error(_('Could not strip EXIF metadata!'), null, $error); } catch (RuntimeException $e) {
if ($config['syslog']) {
_syslog(LOG_ERR, "Could not strip image metadata: {$e->getMessage()}");
// Since EXIF metadata can countain sensible info, fail the request.
error(_('Could not strip EXIF metadata!'), null, $error);
}
}
} else { } else {
$image->to($file['file']); $image->to($file['file']);
$dont_copy_file = true; $dont_copy_file = true;