Skip to content

Commit 91d2de4

Browse files
Chhandak.BaruaChhandak.Barua
authored andcommitted
ACP2E-3504: Product images not resized when added as configurable product
1 parent 2d62730 commit 91d2de4

File tree

2 files changed

+142
-8
lines changed
  • app/code/Magento/Catalog/Controller/Adminhtml/Product/Gallery
  • dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Product/Gallery

2 files changed

+142
-8
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Gallery/Upload.php

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
use Magento\Framework\App\Filesystem\DirectoryList;
1010
use Magento\Framework\App\ObjectManager;
1111
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Backend\Model\Image\UploadResizeConfigInterface;
13+
use Psr\Log\LoggerInterface;
1214

1315
/**
1416
* The product gallery upload controller
17+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1518
*/
1619
class Upload extends \Magento\Backend\App\Action implements HttpPostActionInterface
1720
{
@@ -52,19 +55,32 @@ class Upload extends \Magento\Backend\App\Action implements HttpPostActionInterf
5255
*/
5356
private $productMediaConfig;
5457

58+
/**
59+
* @var UploadResizeConfigInterface
60+
*/
61+
private $imageUploadConfig;
62+
63+
/**
64+
* @var LoggerInterface
65+
*/
66+
private $_logger;
67+
5568
/**
5669
* @param \Magento\Backend\App\Action\Context $context
5770
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
58-
* @param \Magento\Framework\Image\AdapterFactory $adapterFactory
59-
* @param \Magento\Framework\Filesystem $filesystem
60-
* @param \Magento\Catalog\Model\Product\Media\Config $productMediaConfig
71+
* @param \Magento\Framework\Image\AdapterFactory|null $adapterFactory
72+
* @param \Magento\Framework\Filesystem|null $filesystem
73+
* @param \Magento\Catalog\Model\Product\Media\Config|null $productMediaConfig
74+
* @param UploadResizeConfigInterface|null $imageUploadConfig
75+
* @throws \Magento\Framework\Exception\FileSystemException
6176
*/
6277
public function __construct(
6378
\Magento\Backend\App\Action\Context $context,
6479
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
65-
?\Magento\Framework\Image\AdapterFactory $adapterFactory = null,
66-
?\Magento\Framework\Filesystem $filesystem = null,
67-
?\Magento\Catalog\Model\Product\Media\Config $productMediaConfig = null
80+
\Magento\Framework\Image\AdapterFactory $adapterFactory = null,
81+
\Magento\Framework\Filesystem $filesystem = null,
82+
\Magento\Catalog\Model\Product\Media\Config $productMediaConfig = null,
83+
UploadResizeConfigInterface $imageUploadConfig = null
6884
) {
6985
parent::__construct($context);
7086
$this->resultRawFactory = $resultRawFactory;
@@ -74,6 +90,8 @@ public function __construct(
7490
->get(\Magento\Framework\Filesystem::class);
7591
$this->productMediaConfig = $productMediaConfig ?: ObjectManager::getInstance()
7692
->get(\Magento\Catalog\Model\Product\Media\Config::class);
93+
$this->imageUploadConfig = $imageUploadConfig
94+
?: ObjectManager::getInstance()->get(UploadResizeConfigInterface::class);
7795
}
7896

7997
/**
@@ -97,6 +115,12 @@ public function execute()
97115
$result = $uploader->save(
98116
$mediaDirectory->getAbsolutePath($this->productMediaConfig->getBaseTmpMediaPath())
99117
);
118+
// Resize the image if needed
119+
$this->processImage(
120+
$imageAdapter,
121+
$mediaDirectory->getAbsolutePath($this->productMediaConfig->getBaseTmpMediaPath()),
122+
$result['file']
123+
);
100124
$this->_eventManager->dispatch(
101125
'catalog_product_gallery_upload_image_after',
102126
['result' => $result, 'action' => $this]
@@ -124,6 +148,48 @@ public function execute()
124148
return $response;
125149
}
126150

151+
/**
152+
* Resize the image
153+
*
154+
* @param \Magento\Framework\Image\AdapterFactory $imageAdapter
155+
* @param string $path
156+
* @param string $file
157+
* @return bool
158+
*/
159+
private function processImage($imageAdapter, $path, $file): bool
160+
{
161+
try {
162+
$filePath = $path . DIRECTORY_SEPARATOR . $file;
163+
164+
// Open the image file
165+
$imageAdapter->open($filePath);
166+
167+
// Get current dimensions
168+
$imageWidth = $imageAdapter->getOriginalWidth();
169+
$imageHeight = $imageAdapter->getOriginalHeight();
170+
171+
// Fetch resizing configurations
172+
$maxWidth = $this->imageUploadConfig->getMaxWidth();
173+
$maxHeight = $this->imageUploadConfig->getMaxHeight();
174+
175+
// Check if resizing is necessary
176+
if ($this->imageUploadConfig->isResizeEnabled()
177+
&& ($imageWidth > $maxWidth || $imageHeight > $maxHeight)) {
178+
// Maintain aspect ratio and resize
179+
$imageAdapter->keepAspectRatio(true);
180+
$imageAdapter->resize($maxWidth, $maxHeight);
181+
182+
// Save the resized image
183+
$imageAdapter->save($filePath);
184+
}
185+
186+
return true;
187+
} catch (\Exception $e) {
188+
$this->_logger->error($e->getMessage());
189+
return false;
190+
}
191+
}
192+
127193
/**
128194
* Get the set of allowed file extensions.
129195
*

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Product/Gallery/UploadTest.php

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -15,6 +15,7 @@
1515
use Magento\Framework\Filesystem\DirectoryList;
1616
use Magento\Framework\Serialize\Serializer\Json;
1717
use Magento\TestFramework\TestCase\AbstractBackendController;
18+
use Magento\Framework\App\Config\ScopeConfigInterface;
1819

1920
/**
2021
* Provide tests for admin product upload image action.
@@ -53,6 +54,11 @@ class UploadTest extends AbstractBackendController
5354
*/
5455
private $config;
5556

57+
/**
58+
* @var ScopeConfigInterface
59+
*/
60+
private mixed $scopeConfig;
61+
5662
/**
5763
* @inheritdoc
5864
*/
@@ -64,6 +70,7 @@ protected function setUp(): void
6470
$this->serializer = $this->_objectManager->get(Json::class);
6571
$this->mediaDirectory = $this->filesystem->getDirectoryWrite(AppDirectoryList::MEDIA);
6672
$this->config = $this->_objectManager->get(Config::class);
73+
$this->scopeConfig = $this->_objectManager->get(ScopeConfigInterface::class);
6774
}
6875

6976
/**
@@ -92,6 +99,67 @@ public function testUploadAction(array $file, array $expectation): void
9299
));
93100
}
94101

102+
/**
103+
* Test upload image on admin product page.
104+
*
105+
* @dataProvider uploadActionDataProvider
106+
* @magentoDbIsolation enabled
107+
* @param array $file
108+
* @param array $expectation
109+
* @return void
110+
*/
111+
public function testUploadWithResizeAction(array $file, array $expectation): void
112+
{
113+
$this->copyFileToSysTmpDir($file);
114+
$this->scopeConfig->setValue(
115+
'system/upload_configuration/max_width',
116+
100
117+
);
118+
$this->scopeConfig->setValue(
119+
'system/upload_configuration/max_height',
120+
100
121+
);
122+
$this->getRequest()->setMethod($this->httpMethod);
123+
$this->dispatch($this->uri);
124+
$jsonBody = $this->serializer->unserialize($this->getResponse()->getBody());
125+
$this->assertEquals($jsonBody['name'], $expectation['name']);
126+
$this->assertEquals($jsonBody['type'], $expectation['type']);
127+
$this->assertEquals($jsonBody['file'], $expectation['file']);
128+
$this->assertEquals($jsonBody['url'], $expectation['url']);
129+
$this->assertArrayNotHasKey('error', $jsonBody);
130+
$this->assertArrayNotHasKey('errorcode', $jsonBody);
131+
$this->assertTrue($this->mediaDirectory->isExist(
132+
$this->getFileAbsolutePath($expectation['tmp_media_path'])
133+
));
134+
$dimensions = $this->getImageDimensions($this->getFileAbsolutePath($expectation['tmp_media_path']));
135+
$this->assertLessThanOrEqual(100, $dimensions['width']);
136+
$this->assertLessThanOrEqual(100, $dimensions['height']);
137+
}
138+
139+
/**
140+
* Fetch uploaded image dimension.
141+
*
142+
* @param $imagePath
143+
* @return array|false
144+
*/
145+
private function getImageDimensions($imagePath)
146+
{
147+
if (!file_exists($imagePath)) {
148+
return false;
149+
}
150+
151+
$imageInfo = getimagesize($imagePath);
152+
153+
if ($imageInfo === false) {
154+
return false;
155+
}
156+
157+
return [
158+
'width' => $imageInfo[0],
159+
'height' => $imageInfo[1],
160+
];
161+
}
162+
95163
/**
96164
* @return array
97165
*/

0 commit comments

Comments
 (0)