Skip to content

Commit

Permalink
fix: Fix png resize
Browse files Browse the repository at this point in the history
  • Loading branch information
kpitn committed Feb 22, 2024
1 parent f71a119 commit cc4be29
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 73 deletions.
54 changes: 10 additions & 44 deletions Model/Resize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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
*
Expand All @@ -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;
}
}
Expand All @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion Model/WebpConvertor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -145,7 +146,7 @@ public function getOptions(): array
return [
'quality' => 'auto',
'max-quality' => $this->config->getWebpQuality(),
'converters' => [$this->config->getWebpConverter()],
'converter' => $this->config->getWebpConverter()
];
}

Expand All @@ -155,6 +156,7 @@ public function getOptions(): array
* @param string $webpImageUrl
*
* @return bool
* @throws NoSuchEntityException
*/
public function isWebpImageExist(string $webpImageUrl): bool
{
Expand Down
22 changes: 0 additions & 22 deletions Provider/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down
5 changes: 0 additions & 5 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@
<label>Quality</label>
<comment>Value from 70 to 100</comment>
</field>
<field id="convert_png_image" translate="label" type="select" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Convert png image to jpg</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Convert png image to jpg</comment>
</field>
</group>
<group id="webp" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>WebP</label>
Expand Down
1 change: 0 additions & 1 deletion etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<keep_frame>0</keep_frame>
<background_color>255,255,255</background_color>
<quality>85</quality>
<convert_png_image>0</convert_png_image>
</default>
<webp>
<enabled>0</enabled>
Expand Down

0 comments on commit cc4be29

Please sign in to comment.