Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only resize an image if it results in a smaller image size #32787

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions apps/theming/lib/ImageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,29 +232,47 @@ public function updateImage(string $key, string $tmpFile): string {
}

if ($key === 'background' && strpos($detectedMimeType, 'image/svg') === false && strpos($detectedMimeType, 'image/gif') === false) {
$tmpFile = $this->getResizedImagePathIfResizeIsSmaller($tmpFile);
}

$target->putContent(file_get_contents($tmpFile));
return $detectedMimeType;
}

/**
* Returns the file path of the file stored as a png and resized to be
* a maximum of 4096 pixels wide (preserving aspect ratio), but only if the resizing
* results in an image that has a smaller file size than the uploaded file.
*
* @param string $originalTmpFile The tmpFile(path) as uploaded by the user
* @return string Location of the resized file, or the original
*/
private function getResizedImagePathIfResizeIsSmaller(string $originalTmpFile): string
{
// Optimize the image since some people may upload images that will be
// either to big or are not progressive rendering.
$newImage = @imagecreatefromstring(file_get_contents($tmpFile));
$newImage = @imagecreatefromstring(file_get_contents($originalTmpFile));

// Preserve transparency
imagesavealpha($newImage, true);
imagealphablending($newImage, true);
imagesavealpha($newImage, true);
imagealphablending($newImage, true);

$tmpFile = $this->tempManager->getTemporaryFile();
$newWidth = (int)(imagesx($newImage) < 4096 ? imagesx($newImage) : 4096);
$newHeight = (int)(imagesy($newImage) / (imagesx($newImage) / $newWidth));
$outputImage = imagescale($newImage, $newWidth, $newHeight);
$newImageTmpFile = $this->tempManager->getTemporaryFile();
$newWidth = (int)(imagesx($newImage) < 4096 ? imagesx($newImage) : 4096);
$newHeight = (int)(imagesy($newImage) / (imagesx($newImage) / $newWidth));
$outputImage = imagescale($newImage, $newWidth, $newHeight);

imageinterlace($outputImage, 1);
imagepng($outputImage, $tmpFile, 8);
imagedestroy($outputImage);
imageinterlace($outputImage, 1);
imagepng($outputImage, $newImageTmpFile, 8);
imagedestroy($outputImage);

$target->putContent(file_get_contents($tmpFile));
// only actually use the image if it is an improvement
$newImageIsSmaller = filesize($newImageTmpFile) <= filesize($originalTmpFile);
if ($newImageIsSmaller) {
return $newImageTmpFile;
} else {
$target->putContent(file_get_contents($tmpFile));
return $originalTmpFile;
}

return $detectedMimeType;
}

/**
Expand Down