Skip to content

Commit

Permalink
Fix file size estimate checks to rely on real file sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
engram-design committed Dec 14, 2023
1 parent 67471d3 commit 427faab
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions src/services/Resize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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 = [
Expand Down

0 comments on commit 427faab

Please sign in to comment.