From 427faabd1bc203ee603271a6aee6cc6c832bce72 Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Thu, 14 Dec 2023 14:16:28 +1100 Subject: [PATCH] Fix file size estimate checks to rely on real file sizes --- src/services/Resize.php | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/services/Resize.php b/src/services/Resize.php index 6e27b65..247c53f 100644 --- a/src/services/Resize.php +++ b/src/services/Resize.php @@ -124,21 +124,16 @@ public function resize(Asset $asset, string $filename, string $path, int $width // If we're checking for larger images if ($settings->skipLarger) { - // We need to check if the resulting image has a larger file size. Normally you would create the image file and read that - // but it's unperformant. Instead, generate the image from the resource (GD or Imagick), get the in-memory inline image - // and render it as a JPG, reporting the size of the resulting string representation. We force things to be JPG, because - // dealing with PNGs is very, very slow. - // See https://stackoverflow.com/a/63376880 for converting string to estimated filesize. - $resizedSize = (strlen(rtrim(base64_encode($image->getImagineImage()->get('jpg')), '=')) * 0.75); + // Save this resized image in a temporary location - we need to test filesize difference + $tempPath = AssetsHelper::tempFilePath($filename); + ImageResizer::$plugin->getService()->saveAs($image, $tempPath); - // Lets check to see if this resize resulted in a larger file - revert if so. - if ($resizedSize < filesize($path)) { - ImageResizer::$plugin->getService()->saveAs($image, $path); // It's a smaller file - properly save + clearstatcache(); - // Create remote file - // if (!$volume instanceof LocalVolumeInterface) { - // $this->_createRemoteFile($volume, $filename, $path); - // } + // Lets check to see if this resize resulted in a larger file - revert if so. + if (filesize($tempPath) < filesize($path)) { + // Copy the temp image we create to check filesize + copy($tempPath, $path); clearstatcache(); @@ -152,14 +147,12 @@ public function resize(Asset $asset, string $filename, string $path, int $width } else { ImageResizer::$plugin->getLogs()->resizeLog($taskId, 'skipped-larger-result', $filename); } + + // Delete our temp file we test filesize with + @unlink($tempPath); } else { ImageResizer::$plugin->getService()->saveAs($image, $path); - // Create remote file - // if (!$volume instanceof LocalVolumeInterface) { - // $this->_createRemoteFile($volume, $filename, $path); - // } - clearstatcache(); $newProperties = [