Skip to content

Commit 41b1cd5

Browse files
committed
ECP-261: Offload Catalog Image Resizing from Magento
1 parent 06a0dd0 commit 41b1cd5

File tree

29 files changed

+431
-967
lines changed

29 files changed

+431
-967
lines changed

app/code/Magento/Catalog/Block/Rss/Category.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
use Magento\Framework\Exception\NoSuchEntityException;
1111

1212
/**
13-
* Class Category
14-
*
15-
* @package Magento\Catalog\Block\Rss
13+
* Category feed block
1614
*
1715
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1816
*/

app/code/Magento/Catalog/Block/Rss/Product/NewProducts.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
use Magento\Framework\App\Rss\DataProviderInterface;
99

1010
/**
11-
* Class NewProducts
12-
* @package Magento\Catalog\Block\Rss\Product
11+
* New products feed block
1312
*/
1413
class NewProducts extends \Magento\Framework\View\Element\AbstractBlock implements DataProviderInterface
1514
{
@@ -55,6 +54,8 @@ public function __construct(
5554
}
5655

5756
/**
57+
* Init
58+
*
5859
* @return void
5960
*/
6061
protected function _construct()
@@ -64,15 +65,15 @@ protected function _construct()
6465
}
6566

6667
/**
67-
* {@inheritdoc}
68+
* @inheritdoc
6869
*/
6970
public function isAllowed()
7071
{
7172
return $this->_scopeConfig->isSetFlag('rss/catalog/new', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
7273
}
7374

7475
/**
75-
* {@inheritdoc}
76+
* @inheritdoc
7677
*/
7778
public function getRssData()
7879
{
@@ -97,10 +98,13 @@ public function getRssData()
9798
$item->setAllowedInRss(true);
9899
$item->setAllowedPriceInRss(true);
99100

100-
$this->_eventManager->dispatch('rss_catalog_new_xml_callback', [
101-
'row' => $item->getData(),
102-
'product' => $item
103-
]);
101+
$this->_eventManager->dispatch(
102+
'rss_catalog_new_xml_callback',
103+
[
104+
'row' => $item->getData(),
105+
'product' => $item
106+
]
107+
);
104108

105109
if (!$item->getAllowedInRss()) {
106110
continue;
@@ -132,6 +136,8 @@ public function getRssData()
132136
}
133137

134138
/**
139+
* Get store id
140+
*
135141
* @return int
136142
*/
137143
protected function getStoreId()
@@ -177,14 +183,16 @@ protected function renderPriceHtml(\Magento\Catalog\Model\Product $product)
177183
}
178184

179185
/**
180-
* {@inheritdoc}
186+
* @inheritdoc
181187
*/
182188
public function getCacheLifetime()
183189
{
184190
return 600;
185191
}
186192

187193
/**
194+
* Get feeds
195+
*
188196
* @return array
189197
*/
190198
public function getFeeds()
@@ -199,7 +207,7 @@ public function getFeeds()
199207
}
200208

201209
/**
202-
* {@inheritdoc}
210+
* @inheritdoc
203211
*/
204212
public function isAuthRequired()
205213
{

app/code/Magento/Catalog/Block/Rss/Product/Special.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
use Magento\Framework\App\Rss\DataProviderInterface;
1010

1111
/**
12-
* Class Special
13-
* @package Magento\Catalog\Block\Rss\Product
12+
* Special products feed block
1413
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1514
*/
1615
class Special extends \Magento\Framework\View\Element\AbstractBlock implements DataProviderInterface
@@ -98,6 +97,8 @@ public function __construct(
9897
}
9998

10099
/**
100+
* Init
101+
*
101102
* @return void
102103
*/
103104
protected function _construct()
@@ -107,6 +108,8 @@ protected function _construct()
107108
}
108109

109110
/**
111+
* Get RSS data
112+
*
110113
* @return array
111114
*/
112115
public function getRssData()
@@ -156,6 +159,8 @@ public function getRssData()
156159
}
157160

158161
/**
162+
* Get entry data
163+
*
159164
* @param \Magento\Catalog\Model\Product $item
160165
* @return array
161166
*/
@@ -245,14 +250,16 @@ public function isAllowed()
245250
}
246251

247252
/**
248-
* {@inheritdoc}
253+
* @inheritdoc
249254
*/
250255
public function getCacheLifetime()
251256
{
252257
return 600;
253258
}
254259

255260
/**
261+
* Get feeds
262+
*
256263
* @return array
257264
*/
258265
public function getFeeds()
@@ -266,7 +273,7 @@ public function getFeeds()
266273
}
267274

268275
/**
269-
* {@inheritdoc}
276+
* @inheritdoc
270277
*/
271278
public function isAuthRequired()
272279
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Magento\Framework\Exception\LocalizedException;
1212

1313
/**
14-
* Class Upload
14+
* Upload product image action controller
1515
*/
1616
class Upload extends \Magento\Backend\App\Action implements HttpPostActionInterface
1717
{

app/code/Magento/Catalog/Helper/Image.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
*/
66
namespace Magento\Catalog\Helper;
77

8+
use Magento\Catalog\Model\Config\CatalogMediaConfig;
9+
use Magento\Catalog\Model\View\Asset\PlaceholderFactory;
810
use Magento\Framework\App\Helper\AbstractHelper;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\Exception\LocalizedException;
913
use Magento\Framework\View\Element\Block\ArgumentInterface;
1014

1115
/**
@@ -129,31 +133,39 @@ class Image extends AbstractHelper implements ArgumentInterface
129133
protected $attributes = [];
130134

131135
/**
132-
* @var \Magento\Catalog\Model\View\Asset\PlaceholderFactory
136+
* @var PlaceholderFactory
133137
*/
134138
private $viewAssetPlaceholderFactory;
135139

140+
/**
141+
* @var CatalogMediaConfig
142+
*/
143+
private $mediaConfig;
144+
136145
/**
137146
* @param \Magento\Framework\App\Helper\Context $context
138147
* @param \Magento\Catalog\Model\Product\ImageFactory $productImageFactory
139148
* @param \Magento\Framework\View\Asset\Repository $assetRepo
140149
* @param \Magento\Framework\View\ConfigInterface $viewConfig
141-
* @param \Magento\Catalog\Model\View\Asset\PlaceholderFactory $placeholderFactory
150+
* @param PlaceholderFactory $placeholderFactory
151+
* @param CatalogMediaConfig $mediaConfig
142152
*/
143153
public function __construct(
144154
\Magento\Framework\App\Helper\Context $context,
145155
\Magento\Catalog\Model\Product\ImageFactory $productImageFactory,
146156
\Magento\Framework\View\Asset\Repository $assetRepo,
147157
\Magento\Framework\View\ConfigInterface $viewConfig,
148-
\Magento\Catalog\Model\View\Asset\PlaceholderFactory $placeholderFactory = null
158+
PlaceholderFactory $placeholderFactory = null,
159+
CatalogMediaConfig $mediaConfig = null
149160
) {
150161
$this->_productImageFactory = $productImageFactory;
151162
parent::__construct($context);
152163
$this->_assetRepo = $assetRepo;
153164
$this->viewConfig = $viewConfig;
154165
$this->viewAssetPlaceholderFactory = $placeholderFactory
155-
?: \Magento\Framework\App\ObjectManager::getInstance()
156-
->get(\Magento\Catalog\Model\View\Asset\PlaceholderFactory::class);
166+
?: ObjectManager::getInstance()
167+
->get(PlaceholderFactory::class);
168+
$this->mediaConfig = $mediaConfig ?: ObjectManager::getInstance()->get(CatalogMediaConfig::class);
157169
}
158170

159171
/**
@@ -526,7 +538,16 @@ protected function isScheduledActionsAllowed()
526538
public function getUrl()
527539
{
528540
try {
529-
$this->applyScheduledActions();
541+
switch ($this->mediaConfig->getMediaUrlFormat()) {
542+
case CatalogMediaConfig::IMAGE_OPTIMIZATION_PARAMETERS:
543+
$this->initBaseFile();
544+
break;
545+
case CatalogMediaConfig::HASH:
546+
$this->applyScheduledActions();
547+
break;
548+
default:
549+
throw new LocalizedException(__("The specified Catalog media URL format is not supported."));
550+
}
530551
return $this->_getModel()->getUrl();
531552
} catch (\Exception $e) {
532553
return $this->getDefaultPlaceholderUrl();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model\Config;
7+
8+
use Magento\Framework\App\Config\ScopeConfigInterface;
9+
10+
/**
11+
* Config for catalog media
12+
*/
13+
class CatalogMediaConfig
14+
{
15+
private const XML_PATH_CATALOG_MEDIA_URL_FORMAT = 'web/url/catalog_media_url_format';
16+
17+
const IMAGE_OPTIMIZATION_PARAMETERS = 'image_optimization_parameters';
18+
const HASH = 'hash';
19+
20+
/**
21+
* @var ScopeConfigInterface
22+
*/
23+
private $scopeConfig;
24+
25+
/**
26+
* Constructor
27+
*
28+
* @param ScopeConfigInterface $scopeConfig
29+
*/
30+
public function __construct(ScopeConfigInterface $scopeConfig)
31+
{
32+
$this->scopeConfig = $scopeConfig;
33+
}
34+
35+
/**
36+
* Get media URL format for catalog images
37+
*
38+
* @param string $scopeType
39+
* @param null|int|string $scopeCode
40+
* @return string
41+
*/
42+
public function getMediaUrlFormat($scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null)
43+
{
44+
return $this->scopeConfig->getValue(
45+
CatalogMediaConfig::XML_PATH_CATALOG_MEDIA_URL_FORMAT,
46+
$scopeType,
47+
$scopeCode
48+
);
49+
}
50+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model\Config\Source\Web;
7+
8+
use Magento\Catalog\Model\Config\CatalogMediaConfig;
9+
10+
/**
11+
* Option provider for catalog media URL format system setting.
12+
*/
13+
class CatalogMediaUrlFormat implements \Magento\Framework\Data\OptionSourceInterface
14+
{
15+
/**
16+
* Get a list of supported catalog media URL formats.
17+
*
18+
* @codeCoverageIgnore
19+
*/
20+
public function toOptionArray()
21+
{
22+
return [
23+
[
24+
'value' => CatalogMediaConfig::IMAGE_OPTIMIZATION_PARAMETERS,
25+
'label' => __('Image optimization based on query parameters')
26+
],
27+
['value' => CatalogMediaConfig::HASH, 'label' => __('Unique hash per image variant (Legacy mode)')]
28+
];
29+
}
30+
}

0 commit comments

Comments
 (0)