Browse Source

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

remotes/upstream/experimental
Hugo Sales 8 months ago
parent
commit
6a2c3eb711
Signed by: someonewithpc <hugo@hsal.es> GPG Key ID: 7D0C7EAFC9D835A0
1 changed files with 49 additions and 40 deletions
  1. +49
    -40
      plugins/ImageEncoder/ImageEncoder.php

+ 49
- 40
plugins/ImageEncoder/ImageEncoder.php View File

@@ -100,21 +100,25 @@ class ImageEncoder extends Plugin

public function fileMeta(SplFileInfo &$file, ?string &$mimetype, ?int &$width, ?int &$height): bool
{
$original_mimetype = $mimetype;
if (GSFile::mimetypeMajor($original_mimetype) !== 'image') {
// Nothing concerning us
return false;
}

$old_limit = ini_set('memory_limit', Common::config('attachments', 'memory_limit'));
try {
$image = Vips\Image::newFromFile($file->getRealPath(), ['access' => 'sequential']);
} catch (Vips\Exception $e) {
Log::debug("ImageEncoder's Vips couldn't handle the image file, failed with {$e}.");
throw new UnsupportedFileTypeException(_m("Unsupported image file with {$mimetype}.", previous: $e));
}
$width = $image->width;
$height = $image->height;
$original_mimetype = $mimetype;
if (GSFile::mimetypeMajor($original_mimetype) !== 'image') {
// Nothing concerning us
return false;
}

try {
$image = Vips\Image::newFromFile($file->getRealPath(), ['access' => 'sequential']);
} catch (Vips\Exception $e) {
Log::debug("ImageEncoder's Vips couldn't handle the image file, failed with {$e}.");
throw new UnsupportedFileTypeException(_m("Unsupported image file with {$mimetype}.", previous: $e));
}
$width = $image->width;
$height = $image->height;
} finally {
ini_set('memory_limit', $old_limit); // Restore the old memory limit
}
// Only one plugin can handle meta
return true;
}
@@ -138,42 +142,47 @@ class ImageEncoder extends Plugin
*/
public function fileSanitize(SplFileInfo &$file, ?string &$mimetype, ?int &$width, ?int &$height): bool
{
$original_mimetype = $mimetype;
if (GSFile::mimetypeMajor($original_mimetype) !== 'image') {
// Nothing concerning us
return false;
}
$old_limit = ini_set('memory_limit', Common::config('attachments', 'memory_limit'));
try {
$original_mimetype = $mimetype;
if (GSFile::mimetypeMajor($original_mimetype) !== 'image') {
// Nothing concerning us
return false;
}

// Try to maintain original mimetype extension, otherwise default to preferred.
$extension = '.' . Common::config('thumbnail', 'extension');
$extension = GSFile::ensureFilenameWithProperExtension(
// Try to maintain original mimetype extension, otherwise default to preferred.
$extension = '.' . Common::config('thumbnail', 'extension');
$extension = GSFile::ensureFilenameWithProperExtension(
title: $file->getFilename(),
mimetype: $original_mimetype,
ext: $extension,
force: false
) ?? $extension;

// TemporaryFile handles deleting the file if some error occurs
// IMPORTANT: We have to specify the extension for the temporary file
// in order to have a format conversion
$temp = new TemporaryFile(['prefix' => 'image', 'suffix' => $extension]);
// TemporaryFile handles deleting the file if some error occurs
// IMPORTANT: We have to specify the extension for the temporary file
// in order to have a format conversion
$temp = new TemporaryFile(['prefix' => 'image', 'suffix' => $extension]);

try {
$image = Vips\Image::newFromFile($file->getRealPath(), ['access' => 'sequential']);
} catch (Vips\Exception $e) {
Log::debug("ImageEncoder's Vips couldn't handle the image file, failed with {$e}.");
throw new UnsupportedFileTypeException(_m("Unsupported image file with {$mimetype}.", previous: $e));
}
$width = $image->width;
$height = $image->height;
$image = $image->crop(left: 0,
top: 0,
width: $width,
height: $height);
$image->writeToFile($temp->getRealPath());
try {
$image = Vips\Image::newFromFile($file->getRealPath(), ['access' => 'sequential']);
} catch (Vips\Exception $e) {
Log::debug("ImageEncoder's Vips couldn't handle the image file, failed with {$e}.");
throw new UnsupportedFileTypeException(_m("Unsupported image file with {$mimetype}.", previous: $e));
}
$width = $image->width;
$height = $image->height;
$image = $image->crop(left: 0,
top: 0,
width: $width,
height: $height);
$image->writeToFile($temp->getRealPath());

// Replace original file with the sanitized one
$temp->commit($file->getRealPath());
// Replace original file with the sanitized one
$temp->commit($file->getRealPath());
} finally {
ini_set('memory_limit', $old_limit); // Restore the old memory limit
}

// Only one plugin can handle sanitization
return true;


Loading…
Cancel
Save