[ImageEncoder] Ensure proper memory limits are used when loading images from disk

This commit is contained in:
Hugo Sales 2021-09-23 14:50:53 +01:00
parent 1c1bef76ef
commit 6a2c3eb711
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0

View File

@ -100,6 +100,8 @@ class ImageEncoder extends Plugin
public function fileMeta(SplFileInfo &$file, ?string &$mimetype, ?int &$width, ?int &$height): bool public function fileMeta(SplFileInfo &$file, ?string &$mimetype, ?int &$width, ?int &$height): bool
{ {
$old_limit = ini_set('memory_limit', Common::config('attachments', 'memory_limit'));
try {
$original_mimetype = $mimetype; $original_mimetype = $mimetype;
if (GSFile::mimetypeMajor($original_mimetype) !== 'image') { if (GSFile::mimetypeMajor($original_mimetype) !== 'image') {
// Nothing concerning us // Nothing concerning us
@ -114,7 +116,9 @@ class ImageEncoder extends Plugin
} }
$width = $image->width; $width = $image->width;
$height = $image->height; $height = $image->height;
} finally {
ini_set('memory_limit', $old_limit); // Restore the old memory limit
}
// Only one plugin can handle meta // Only one plugin can handle meta
return true; return true;
} }
@ -138,6 +142,8 @@ class ImageEncoder extends Plugin
*/ */
public function fileSanitize(SplFileInfo &$file, ?string &$mimetype, ?int &$width, ?int &$height): bool public function fileSanitize(SplFileInfo &$file, ?string &$mimetype, ?int &$width, ?int &$height): bool
{ {
$old_limit = ini_set('memory_limit', Common::config('attachments', 'memory_limit'));
try {
$original_mimetype = $mimetype; $original_mimetype = $mimetype;
if (GSFile::mimetypeMajor($original_mimetype) !== 'image') { if (GSFile::mimetypeMajor($original_mimetype) !== 'image') {
// Nothing concerning us // Nothing concerning us
@ -174,6 +180,9 @@ class ImageEncoder extends Plugin
// Replace original file with the sanitized one // Replace original file with the sanitized one
$temp->commit($file->getRealPath()); $temp->commit($file->getRealPath());
} finally {
ini_set('memory_limit', $old_limit); // Restore the old memory limit
}
// Only one plugin can handle sanitization // Only one plugin can handle sanitization
return true; return true;