image: prevent quality loss during commit

Signed-off-by: Varun Patil <radialapps@gmail.com>
pull/788/head
Varun Patil 2023-08-22 10:46:53 -07:00
parent aecc53841c
commit 1426e947ba
1 changed files with 13 additions and 1 deletions

View File

@ -339,11 +339,23 @@ class ImageController extends GenericApiController
$image = new \Imagick(); $image = new \Imagick();
$image->readImageBlob($file->getContent()); $image->readImageBlob($file->getContent());
// Due to a bug in filerobot, the provided width and height may be swapped
// 1. If the user does not rotate the image, we're fine
// 2. If image is rotated and user doesn't change the save resolution,
// the wxh corresponds to the original image, not the rotated one
// 3. If image is rotated and user changes the save resolution,
// the wxh corresponds to the rotated image.
$iw = $image->getImageWidth();
$ih = $image->getImageHeight();
$shouldResize = $width !== $iw || $height !== $ih;
// Apply the edits // Apply the edits
(new Service\FileRobotMagick($image, $state))->apply(); (new Service\FileRobotMagick($image, $state))->apply();
// Resize the image // Resize the image
if ($width > 0 && $height > 0 && ($width !== $image->getImageWidth() || $height !== $image->getImageHeight())) { $iw = $image->getImageWidth();
$ih = $image->getImageHeight();
if ($shouldResize && $width && $height && ($iw !== $width || $ih !== $height)) {
$image->resizeImage($width, $height, \Imagick::FILTER_LANCZOS, 1, true); $image->resizeImage($width, $height, \Imagick::FILTER_LANCZOS, 1, true);
} }