|
|
@ -57,6 +57,7 @@ class ImageManager { |
|
|
|
private ICacheFactory $cacheFactory, |
|
|
|
private LoggerInterface $logger, |
|
|
|
private ITempManager $tempManager, |
|
|
|
private BackgroundService $backgroundService, |
|
|
|
) { |
|
|
|
} |
|
|
|
|
|
|
@ -228,47 +229,56 @@ class ImageManager { |
|
|
|
throw new \Exception('Unsupported image type: ' . $detectedMimeType); |
|
|
|
} |
|
|
|
|
|
|
|
if ($key === 'background' && $this->shouldOptimizeBackgroundImage($detectedMimeType, filesize($tmpFile))) { |
|
|
|
try { |
|
|
|
// 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)); |
|
|
|
if ($newImage === false) { |
|
|
|
throw new \Exception('Could not read background image, possibly corrupted.'); |
|
|
|
} |
|
|
|
if ($key === 'background') { |
|
|
|
if ($this->shouldOptimizeBackgroundImage($detectedMimeType, filesize($tmpFile))) { |
|
|
|
try { |
|
|
|
// 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)); |
|
|
|
if ($newImage === false) { |
|
|
|
throw new \Exception('Could not read background image, possibly corrupted.'); |
|
|
|
} |
|
|
|
|
|
|
|
// Preserve transparency
|
|
|
|
imagesavealpha($newImage, true); |
|
|
|
imagealphablending($newImage, true); |
|
|
|
// Preserve transparency
|
|
|
|
imagesavealpha($newImage, true); |
|
|
|
imagealphablending($newImage, true); |
|
|
|
|
|
|
|
$newWidth = (imagesx($newImage) < 4096 ? imagesx($newImage) : 4096); |
|
|
|
$newHeight = (int)(imagesy($newImage) / (imagesx($newImage) / $newWidth)); |
|
|
|
$outputImage = imagescale($newImage, $newWidth, $newHeight); |
|
|
|
if ($outputImage === false) { |
|
|
|
throw new \Exception('Could not scale uploaded background image.'); |
|
|
|
} |
|
|
|
$imageWidth = imagesx($newImage); |
|
|
|
$imageHeight = imagesy($newImage); |
|
|
|
|
|
|
|
$newTmpFile = $this->tempManager->getTemporaryFile(); |
|
|
|
imageinterlace($outputImage, true); |
|
|
|
// Keep jpeg images encoded as jpeg
|
|
|
|
if (str_contains($detectedMimeType, 'image/jpeg')) { |
|
|
|
if (!imagejpeg($outputImage, $newTmpFile, 90)) { |
|
|
|
throw new \Exception('Could not recompress background image as JPEG'); |
|
|
|
/** @var int */ |
|
|
|
$newWidth = min(4096, $imageWidth); |
|
|
|
$newHeight = intval($imageHeight / ($imageWidth / $newWidth)); |
|
|
|
$outputImage = imagescale($newImage, $newWidth, $newHeight); |
|
|
|
if ($outputImage === false) { |
|
|
|
throw new \Exception('Could not scale uploaded background image.'); |
|
|
|
} |
|
|
|
} else { |
|
|
|
if (!imagepng($outputImage, $newTmpFile, 8)) { |
|
|
|
throw new \Exception('Could not recompress background image as PNG'); |
|
|
|
|
|
|
|
$newTmpFile = $this->tempManager->getTemporaryFile(); |
|
|
|
imageinterlace($outputImage, true); |
|
|
|
// Keep jpeg images encoded as jpeg
|
|
|
|
if (str_contains($detectedMimeType, 'image/jpeg')) { |
|
|
|
if (!imagejpeg($outputImage, $newTmpFile, 90)) { |
|
|
|
throw new \Exception('Could not recompress background image as JPEG'); |
|
|
|
} |
|
|
|
} else { |
|
|
|
if (!imagepng($outputImage, $newTmpFile, 8)) { |
|
|
|
throw new \Exception('Could not recompress background image as PNG'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
$tmpFile = $newTmpFile; |
|
|
|
imagedestroy($outputImage); |
|
|
|
} catch (\Exception $e) { |
|
|
|
if (is_resource($outputImage) || $outputImage instanceof \GdImage) { |
|
|
|
$tmpFile = $newTmpFile; |
|
|
|
imagedestroy($outputImage); |
|
|
|
} |
|
|
|
} catch (\Exception $e) { |
|
|
|
if (isset($outputImage) && is_resource($outputImage) || $outputImage instanceof \GdImage) { |
|
|
|
imagedestroy($outputImage); |
|
|
|
} |
|
|
|
|
|
|
|
$this->logger->debug($e->getMessage()); |
|
|
|
$this->logger->debug($e->getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// For background images we need to announce it
|
|
|
|
$this->backgroundService->setGlobalBackground($tmpFile); |
|
|
|
} |
|
|
|
|
|
|
|
$target->putContent(file_get_contents($tmpFile)); |
|
|
|