From 38d1556b06a32623a3ea7bedbc676e4a732fdc0c Mon Sep 17 00:00:00 2001 From: Pieter Hoste Date: Mon, 10 Feb 2020 21:25:12 +0100 Subject: [PATCH 1/3] Prevent resizing an image if it was already resized before. --- .../MediaStorage/Service/ImageResize.php | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/MediaStorage/Service/ImageResize.php b/app/code/Magento/MediaStorage/Service/ImageResize.php index d061ddbd3dc46..461d3df47523f 100644 --- a/app/code/Magento/MediaStorage/Service/ImageResize.php +++ b/app/code/Magento/MediaStorage/Service/ImageResize.php @@ -294,7 +294,7 @@ private function makeImage(string $originalImagePath, array $imageParams): Image } /** - * Resize image. + * Resize image if not already resized before * * @param array $imageParams * @param string $originalImagePath @@ -303,13 +303,48 @@ private function makeImage(string $originalImagePath, array $imageParams): Image private function resize(array $imageParams, string $originalImagePath, string $originalImageName) { unset($imageParams['id']); - $image = $this->makeImage($originalImagePath, $imageParams); $imageAsset = $this->assertImageFactory->create( [ 'miscParams' => $imageParams, 'filePath' => $originalImageName, ] ); + $imageAssetPath = $imageAsset->getPath(); + $usingDbAsStorage = $this->fileStorageDatabase->checkDbUsage(); + $mediaStorageFilename = $this->mediaDirectory->getRelativePath($imageAssetPath); + + $alreadyResized = $usingDbAsStorage ? + $this->fileStorageDatabase->fileExists($mediaStorageFilename) : + $this->mediaDirectory->isFile($imageAssetPath); + + if (!$alreadyResized) { + $this->generateResizedImage( + $imageParams, + $originalImagePath, + $imageAssetPath, + $usingDbAsStorage, + $mediaStorageFilename + ); + } + } + + /** + * Generate resized image + * + * @param array $imageParams + * @param string $originalImagePath + * @param string $imageAssetPath + * @param bool $usingDbAsStorage + * @param string $mediaStorageFilename + */ + private function generateResizedImage( + array $imageParams, + string $originalImagePath, + string $imageAssetPath, + bool $usingDbAsStorage, + string $mediaStorageFilename + ) { + $image = $this->makeImage($originalImagePath, $imageParams); if ($imageParams['image_width'] !== null && $imageParams['image_height'] !== null) { $image->resize($imageParams['image_width'], $imageParams['image_height']); @@ -335,11 +370,10 @@ private function resize(array $imageParams, string $originalImagePath, string $o $image->watermark($this->getWatermarkFilePath($imageParams['watermark_file'])); } - $image->save($imageAsset->getPath()); + $image->save($imageAssetPath); - if ($this->fileStorageDatabase->checkDbUsage()) { - $mediastoragefilename = $this->mediaDirectory->getRelativePath($imageAsset->getPath()); - $this->fileStorageDatabase->saveFile($mediastoragefilename); + if ($usingDbAsStorage) { + $this->fileStorageDatabase->saveFile($mediaStorageFilename); } } From 801b4b3147afe1e2b5f2ae30c2bf585c144829c7 Mon Sep 17 00:00:00 2001 From: Pieter Hoste Date: Mon, 10 Feb 2020 21:45:26 +0100 Subject: [PATCH 2/3] Cleanup. --- .../MediaStorage/Service/ImageResize.php | 30 ++++++++----------- .../Test/Unit/Service/ImageResizeTest.php | 2 -- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/app/code/Magento/MediaStorage/Service/ImageResize.php b/app/code/Magento/MediaStorage/Service/ImageResize.php index 461d3df47523f..d5ce1a7e20f98 100644 --- a/app/code/Magento/MediaStorage/Service/ImageResize.php +++ b/app/code/Magento/MediaStorage/Service/ImageResize.php @@ -15,17 +15,18 @@ use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\NotFoundException; use Magento\Framework\Filesystem; +use Magento\Framework\Filesystem\Directory\WriteInterface; use Magento\Framework\Image; use Magento\Framework\Image\Factory as ImageFactory; use Magento\Catalog\Model\Product\Media\ConfigInterface as MediaConfig; use Magento\Framework\App\State; use Magento\Framework\View\ConfigInterface as ViewConfig; -use \Magento\Catalog\Model\ResourceModel\Product\Image as ProductImage; +use Magento\Catalog\Model\ResourceModel\Product\Image as ProductImage; use Magento\Store\Model\StoreManagerInterface; use Magento\Theme\Model\Config\Customization as ThemeCustomizationConfig; -use Magento\Theme\Model\ResourceModel\Theme\Collection; +use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeCollection; use Magento\Framework\App\Filesystem\DirectoryList; -use Magento\MediaStorage\Helper\File\Storage\Database; +use Magento\MediaStorage\Helper\File\Storage\Database as FileStorageDatabase; use Magento\Theme\Model\Theme; /** @@ -76,24 +77,20 @@ class ImageResize private $themeCustomizationConfig; /** - * @var Collection + * @var ThemeCollection */ private $themeCollection; /** - * @var Filesystem + * @var WriteInterface */ private $mediaDirectory; /** - * @var Filesystem - */ - private $filesystem; - - /** - * @var Database + * @var FileStorageDatabase */ private $fileStorageDatabase; + /** * @var StoreManagerInterface */ @@ -108,9 +105,9 @@ class ImageResize * @param ViewConfig $viewConfig * @param AssertImageFactory $assertImageFactory * @param ThemeCustomizationConfig $themeCustomizationConfig - * @param Collection $themeCollection + * @param ThemeCollection $themeCollection * @param Filesystem $filesystem - * @param Database $fileStorageDatabase + * @param FileStorageDatabase $fileStorageDatabase * @param StoreManagerInterface $storeManager * @throws \Magento\Framework\Exception\FileSystemException * @internal param ProductImage $gallery @@ -125,9 +122,9 @@ public function __construct( ViewConfig $viewConfig, AssertImageFactory $assertImageFactory, ThemeCustomizationConfig $themeCustomizationConfig, - Collection $themeCollection, + ThemeCollection $themeCollection, Filesystem $filesystem, - Database $fileStorageDatabase = null, + FileStorageDatabase $fileStorageDatabase = null, StoreManagerInterface $storeManager = null ) { $this->appState = $appState; @@ -140,9 +137,8 @@ public function __construct( $this->themeCustomizationConfig = $themeCustomizationConfig; $this->themeCollection = $themeCollection; $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA); - $this->filesystem = $filesystem; $this->fileStorageDatabase = $fileStorageDatabase ?: - ObjectManager::getInstance()->get(Database::class); + ObjectManager::getInstance()->get(FileStorageDatabase::class); $this->storeManager = $storeManager ?? ObjectManager::getInstance()->get(StoreManagerInterface::class); } diff --git a/app/code/Magento/MediaStorage/Test/Unit/Service/ImageResizeTest.php b/app/code/Magento/MediaStorage/Test/Unit/Service/ImageResizeTest.php index f0e1efa7806e4..4a17245396240 100644 --- a/app/code/Magento/MediaStorage/Test/Unit/Service/ImageResizeTest.php +++ b/app/code/Magento/MediaStorage/Test/Unit/Service/ImageResizeTest.php @@ -24,8 +24,6 @@ use Magento\Framework\App\Filesystem\DirectoryList; /** - * Class ImageResizeTest - * * @SuppressWarnings(PHPMD.TooManyFields) * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ From 2d1a3c1777a0fd7f4e2e8b7289701c1e7106594d Mon Sep 17 00:00:00 2001 From: Pieter Hoste Date: Tue, 11 Feb 2020 20:37:01 +0100 Subject: [PATCH 3/3] Added unit tests. --- .../Test/Unit/Service/ImageResizeTest.php | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/app/code/Magento/MediaStorage/Test/Unit/Service/ImageResizeTest.php b/app/code/Magento/MediaStorage/Test/Unit/Service/ImageResizeTest.php index 4a17245396240..29ab475948221 100644 --- a/app/code/Magento/MediaStorage/Test/Unit/Service/ImageResizeTest.php +++ b/app/code/Magento/MediaStorage/Test/Unit/Service/ImageResizeTest.php @@ -247,6 +247,9 @@ public function testResizeFromThemesMediaStorageDatabase() $this->databaseMock->expects($this->any()) ->method('checkDbUsage') ->will($this->returnValue(true)); + $this->databaseMock->expects($this->any()) + ->method('fileExists') + ->will($this->returnValue(false)); $this->productImageMock->expects($this->any()) ->method('getCountUsedProductImages') @@ -287,6 +290,9 @@ public function testResizeFromImageNameMediaStorageDatabase() $this->databaseMock->expects($this->any()) ->method('checkDbUsage') ->will($this->returnValue(true)); + $this->databaseMock->expects($this->any()) + ->method('fileExists') + ->will($this->returnValue(false)); $this->mediaDirectoryMock->expects($this->any()) ->method('isFile') @@ -316,4 +322,65 @@ public function testResizeFromImageNameMediaStorageDatabase() $this->service->resizeFromImageName($this->testfilename); } + + public function testSkipResizingAlreadyResizedImageOnDisk() + { + $this->databaseMock->expects($this->any()) + ->method('checkDbUsage') + ->will($this->returnValue(false)); + + $this->mediaDirectoryMock->expects($this->any()) + ->method('isFile') + ->will($this->returnValue(true)); + + $this->themeCollectionMock->expects($this->any()) + ->method('loadRegisteredThemes') + ->willReturn( + [ new DataObject(['id' => '0']) ] + ); + $this->themeCustomizationConfigMock->expects($this->any()) + ->method('getStoresByThemes') + ->willReturn( + ['0' => []] + ); + + $this->imageFactoryMock->expects($this->never()) + ->method('create'); + + $this->service->resizeFromImageName($this->testfilename); + } + + public function testSkipResizingAlreadyResizedImageInDatabase() + { + $this->databaseMock->expects($this->any()) + ->method('checkDbUsage') + ->will($this->returnValue(true)); + $this->databaseMock->expects($this->any()) + ->method('fileExists') + ->will($this->returnValue(true)); + + $this->mediaDirectoryMock->expects($this->any()) + ->method('isFile') + ->with($this->testfilepath) + ->willReturnOnConsecutiveCalls( + $this->returnValue(false), + $this->returnValue(true) + ); + + $this->themeCollectionMock->expects($this->any()) + ->method('loadRegisteredThemes') + ->willReturn( + [ new DataObject(['id' => '0']) ] + ); + $this->themeCustomizationConfigMock->expects($this->any()) + ->method('getStoresByThemes') + ->willReturn( + ['0' => []] + ); + + $this->databaseMock->expects($this->never()) + ->method('saveFile'); + + $this->service->resizeFromImageName($this->testfilename); + } }