Skip to content

Commit a08fd19

Browse files
🔃 [Magento Community Engineering] Community Contributions - 2.4-develop latest changes
Accepted Community Pull Requests: - #29149: Generetae thumbnails only if image have bigger size then defined in configuration, to aviod stretched images (by @Nazar65)
2 parents e0bf5fe + 7be9654 commit a08fd19

File tree

2 files changed

+83
-2
lines changed
  • app/code/Magento/Cms/Model/Wysiwyg/Images
  • dev/tests/integration/testsuite/Magento/Cms/Model/Wysiwyg/Images

2 files changed

+83
-2
lines changed

app/code/Magento/Cms/Model/Wysiwyg/Images/Storage.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,12 @@ public function resizeFile($source, $keepRatio = true)
628628
}
629629
$image = $this->_imageFactory->create();
630630
$image->open($source);
631+
631632
$image->keepAspectRatio($keepRatio);
632-
$image->resize($this->_resizeParameters['width'], $this->_resizeParameters['height']);
633+
634+
list($imageWidth, $imageHeight) = $this->getResizedParams($source);
635+
636+
$image->resize($imageWidth, $imageHeight);
633637
$dest = $targetDir . '/' . $this->ioFile->getPathInfo($source)['basename'];
634638
$image->save($dest);
635639
if ($this->_directory->isFile($this->_directory->getRelativePath($dest))) {
@@ -638,6 +642,29 @@ public function resizeFile($source, $keepRatio = true)
638642
return false;
639643
}
640644

645+
/**
646+
* Return width height for the image resizing.
647+
*
648+
* @param string $source
649+
* @return array
650+
*/
651+
private function getResizedParams(string $source): array
652+
{
653+
$configWidth = $this->_resizeParameters['width'];
654+
$configHeight = $this->_resizeParameters['height'];
655+
656+
//phpcs:ignore Generic.PHP.NoSilencedErrors
657+
list($imageWidth, $imageHeight) = @getimagesize($source);
658+
659+
if ($imageWidth && $imageHeight) {
660+
$imageWidth = $configWidth > $imageWidth ? $imageWidth : $configWidth;
661+
$imageHeight = $configHeight > $imageHeight ? $imageHeight : $configHeight;
662+
663+
return [$imageWidth, $imageHeight];
664+
}
665+
return [$configWidth, $configHeight];
666+
}
667+
641668
/**
642669
* Resize images on the fly in controller action
643670
*

dev/tests/integration/testsuite/Magento/Cms/Model/Wysiwyg/Images/StorageTest.php

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ public function testUploadFile(): void
167167
public function testUploadFileWithExcludedDirPath(): void
168168
{
169169
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
170-
$this->expectExceptionMessage('We can\'t upload the file to current folder right now. Please try another folder.');
170+
$this->expectExceptionMessage(
171+
'We can\'t upload the file to current folder right now. Please try another folder.'
172+
);
171173

172174
$fileName = 'magento_small_image.jpg';
173175
$tmpDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
@@ -295,6 +297,58 @@ public function testGetThumbnailUrl(string $directory, string $filename, string
295297
$this->storage->deleteFile($path);
296298
}
297299

300+
/**
301+
* Verify thumbnail generation for diferent sizes
302+
*
303+
* @param array $sizes
304+
* @param bool $resized
305+
* @dataProvider getThumbnailsSizes
306+
*/
307+
public function testResizeFile(array $sizes, bool $resized): void
308+
{
309+
$root = $this->storage->getCmsWysiwygImages()->getStorageRoot();
310+
$path = $root . '/' . 'testfile.png';
311+
$this->generateImage($path, $sizes['width'], $sizes['height']);
312+
$this->storage->resizeFile($path);
313+
314+
$thumbPath = $this->storage->getThumbnailPath($path);
315+
list($imageWidth, $imageHeight) = getimagesize($thumbPath);
316+
317+
$this->assertEquals(
318+
$resized ? $this->storage->getResizeWidth() : $sizes['width'],
319+
$imageWidth
320+
);
321+
$this->assertLessThanOrEqual(
322+
$resized ? $this->storage->getResizeHeight() : $sizes['height'],
323+
$imageHeight
324+
);
325+
326+
$this->storage->deleteFile($path);
327+
}
328+
329+
/**
330+
* Provide sizes for resizeFile test
331+
*/
332+
public function getThumbnailsSizes(): array
333+
{
334+
return [
335+
[
336+
[
337+
'width' => 1024,
338+
'height' => 768,
339+
],
340+
true
341+
],
342+
[
343+
[
344+
'width' => 20,
345+
'height' => 20,
346+
],
347+
false
348+
]
349+
];
350+
}
351+
298352
/**
299353
* Provide scenarios for testing getThumbnailUrl()
300354
*

0 commit comments

Comments
 (0)