From cc4be292ae9bf6187d7e394306133f1b0991ea27 Mon Sep 17 00:00:00 2001 From: kpitn Date: Fri, 23 Feb 2024 00:01:16 +0100 Subject: [PATCH] fix: Fix png resize --- Model/Resize.php | 54 ++++++++-------------------------------- Model/WebpConvertor.php | 4 ++- Provider/Config.php | 22 ---------------- etc/adminhtml/system.xml | 5 ---- etc/config.xml | 1 - 5 files changed, 13 insertions(+), 73 deletions(-) diff --git a/Model/Resize.php b/Model/Resize.php index 5d7f185..bbec0cb 100644 --- a/Model/Resize.php +++ b/Model/Resize.php @@ -204,11 +204,11 @@ public function __construct( * Resized image and return url * - Return original image url if no success * - * @param string $imagePath - * @param null|int $width - * @param null|int $height - * @param array $resizeSettings - * @param string $format + * @param string $imagePath + * @param null|int $width + * @param null|int $height + * @param array $resizeSettings + * @param string|null $format * * @return string */ @@ -262,13 +262,6 @@ public function resizeAndGetUrl(string $imagePath, $width, $height, array $resiz $this->logger->error("Web200_ImageResize: could not find image: \n" . $e->getMessage()); } try { - if ($this->config->convertPngImage() && $this->getFormat() !== self::FORMAT_WEBP) { - $this->absolutePathoriginal = $this->getAbsolutePathOriginal(); - if (preg_match('/\.png$/', $this->absolutePathoriginal)) { - $this->relativeFilename = preg_replace('/(\.png$)/', '.jpg', $this->relativeFilename); - } - } - // Check if resized image already exists in cache $resizedUrl = $this->getResizedImageUrl(); if (!$resizedUrl && $this->resizeAndSaveImage()) { @@ -440,27 +433,6 @@ protected function getImageUrl(): string return ''; } - /** - * Convert png to jpg - * - * @return void - */ - protected function convertPngToJpg(): void - { - if (preg_match('/\.png$/', $this->absolutePathoriginal)) { - $newAbsolutePath = preg_replace('/(\.png$)/', '.jpg', $this->absolutePathoriginal); - $imageInput = imagecreatefrompng($this->absolutePathoriginal); - $width = imagesx($imageInput); - $height = imagesy($imageInput); - $imageOutput = imagecreatetruecolor($width, $height); - list($red, $green, $blue) = $this->resizeSettings['backgroundColor']; - imagefilledrectangle($imageOutput, 0, 0, $width, $height, imagecolorallocate(imagecreatetruecolor($width, $height), $red, $green, $blue)); - imagecopy($imageOutput, $imageInput, 0, 0, 0, 0, $width, $height); - imagejpeg($imageOutput, $newAbsolutePath, $this->resizeSettings['quality']); - imagedestroy($imageOutput); - } - } - /** * Resize and save new generated image * @@ -469,13 +441,11 @@ protected function convertPngToJpg(): void */ protected function resizeAndSaveImage(): bool { - if ($this->config->convertPngImage() && $this->getFormat() !== self::FORMAT_WEBP) { - if (!$this->mediaDirectoryRead->isFile($this->absolutePathoriginal)) { - return false; - } - $this->convertPngToJpg(); - } else { - if (!$this->mediaDirectoryRead->isFile($this->relativeFilename)) { + if ($this->getFormat() === self::FORMAT_WEBP) { + try { + $this->webpConvertor->convert($this->getAbsolutePathResized()); + return true; + } catch (Exception $exception) { return false; } } @@ -500,10 +470,6 @@ protected function resizeAndSaveImage(): bool $imageAdapter->resize($this->width, $this->height); $imageAdapter->save($this->getAbsolutePathResized()); - if ($this->getFormat() === self::FORMAT_WEBP) { - $this->webpConvertor->convert($this->getAbsolutePathResized()); - } - return true; } diff --git a/Model/WebpConvertor.php b/Model/WebpConvertor.php index 0ccddb5..d7bfa7e 100644 --- a/Model/WebpConvertor.php +++ b/Model/WebpConvertor.php @@ -5,6 +5,7 @@ namespace Web200\ImageResize\Model; use Magento\Framework\App\Filesystem\DirectoryList; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Filesystem; use Magento\Framework\Filesystem\Directory\WriteInterface; use Magento\Framework\UrlInterface; @@ -145,7 +146,7 @@ public function getOptions(): array return [ 'quality' => 'auto', 'max-quality' => $this->config->getWebpQuality(), - 'converters' => [$this->config->getWebpConverter()], + 'converter' => $this->config->getWebpConverter() ]; } @@ -155,6 +156,7 @@ public function getOptions(): array * @param string $webpImageUrl * * @return bool + * @throws NoSuchEntityException */ public function isWebpImageExist(string $webpImageUrl): bool { diff --git a/Provider/Config.php b/Provider/Config.php index 51d7b8c..1401ac0 100644 --- a/Provider/Config.php +++ b/Provider/Config.php @@ -54,12 +54,6 @@ class Config * @var string QUALITY */ public const QUALITY = 'w200_image_resize/default/quality'; - /** - * Convert png image - * - * @var string CONVERT_PNG_IMAGE - */ - public const CONVERT_PNG_IMAGE = 'w200_image_resize/default/convert_png_image'; /** * Webp enabled * @@ -120,22 +114,6 @@ public function getDefaultConstrainOnly($store = null): bool ); } - /** - * Convert png image - * - * @param mixed $store - * - * @return bool - */ - public function convertPngImage($store = null): bool - { - return (bool)$this->scopeConfig->getValue( - self::CONVERT_PNG_IMAGE, - ScopeInterface::SCOPE_STORES, - $store - ); - } - /** * Get default keep aspect ratio * diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index d5cfe15..3927ddb 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -38,11 +38,6 @@ Value from 70 to 100 - - - Magento\Config\Model\Config\Source\Yesno - Convert png image to jpg - diff --git a/etc/config.xml b/etc/config.xml index 626671e..83c3935 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -9,7 +9,6 @@ 0 255,255,255 85 - 0 0