Skip to content

Commit bd5d0a4

Browse files
authored
Merge pull request #4 from Web200/feat-improve-search
feat: Handle png to jpg cache
2 parents 658b65d + a03d4d6 commit bd5d0a4

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

Model/Resize.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ class Resize
8787
* @var StoreManagerInterface $storeManager
8888
*/
8989
protected $storeManager;
90+
/**
91+
* Absolute original path if png to jpg is use
92+
*
93+
* @var string $absolutePathoriginal
94+
*/
95+
protected $absolutePathoriginal;
9096
/**
9197
* @var array
9298
*
@@ -234,6 +240,13 @@ public function resizeAndGetUrl(string $imagePath, $width, $height, array $resiz
234240
$this->logger->error("Web200_ImageResize: could not find image: \n" . $e->getMessage());
235241
}
236242
try {
243+
if ($this->config->convertPngImage()) {
244+
$this->absolutePathoriginal = $this->getAbsolutePathOriginal();
245+
if (preg_match('/\.png$/', $this->absolutePathoriginal)) {
246+
$this->relativeFilename = preg_replace('/(\.png$)/', '.jpg', $this->relativeFilename);
247+
}
248+
}
249+
237250
// Check if resized image already exists in cache
238251
$resizedUrl = $this->getResizedImageUrl();
239252
if (!$resizedUrl && $this->resizeAndSaveImage()) {
@@ -406,11 +419,9 @@ protected function getImageUrl(): string
406419
*/
407420
protected function convertPngToJpg(): void
408421
{
409-
$absolutePath = $this->getAbsolutePathOriginal();
410-
if (preg_match('/\.png$/', $absolutePath)) {
411-
$newAbsolutePath = preg_replace('/(\.png$)/', '.jpg', $absolutePath);
412-
$this->relativeFilename = preg_replace('/(\.png$)/', '.jpg', $this->relativeFilename);
413-
$imageInput = imagecreatefrompng($absolutePath);
422+
if (preg_match('/\.png$/', $this->absolutePathoriginal)) {
423+
$newAbsolutePath = preg_replace('/(\.png$)/', '.jpg', $this->absolutePathoriginal);
424+
$imageInput = imagecreatefrompng($this->absolutePathoriginal);
414425
$width = imagesx($imageInput);
415426
$height = imagesy($imageInput);
416427
$imageOutput = imagecreatetruecolor($width, $height);
@@ -430,12 +441,15 @@ protected function convertPngToJpg(): void
430441
*/
431442
protected function resizeAndSaveImage(): bool
432443
{
433-
if (!$this->mediaDirectoryRead->isFile($this->relativeFilename)) {
434-
return false;
435-
}
436-
437-
if ($this->config->convertPngImage()){
444+
if ($this->config->convertPngImage()) {
445+
if (!$this->mediaDirectoryRead->isFile($this->absolutePathoriginal)) {
446+
return false;
447+
}
438448
$this->convertPngToJpg();
449+
} else {
450+
if (!$this->mediaDirectoryRead->isFile($this->relativeFilename)) {
451+
return false;
452+
}
439453
}
440454

441455
$imageAdapter = $this->imageAdapterFactory->create();

0 commit comments

Comments
 (0)