Skip to content

Commit f97c312

Browse files
author
Volodymyr Klymenko
authored
Merge pull request #899 from magento-tsg/pr-2.1.6
[TSG] Bugfixes - 2.1.6
2 parents 54c2a0e + ef9bf83 commit f97c312

File tree

38 files changed

+2352
-346
lines changed

38 files changed

+2352
-346
lines changed

app/code/Magento/Catalog/Block/Product/ListProduct.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,14 @@ public function getProductPrice(\Magento\Catalog\Model\Product $product)
373373
}
374374

375375
/**
376+
* Specifies that price rendering should be done for the list of products
377+
* i.e. rendering happens in the scope of product list, but not single product
378+
*
376379
* @return \Magento\Framework\Pricing\Render
377380
*/
378381
protected function getPriceRender()
379382
{
380-
return $this->getLayout()->getBlock('product.price.render.default');
383+
return $this->getLayout()->getBlock('product.price.render.default')
384+
->setData('is_product_list', true);
381385
}
382386
}

app/code/Magento/Catalog/Block/Product/View/Gallery.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,13 @@ public function getGalleryImages()
6363
);
6464
$image->setData(
6565
'medium_image_url',
66-
$this->_imageHelper->init($product, 'product_page_image_medium')
67-
->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)
66+
$this->_imageHelper->init($product, 'product_page_image_medium_no_frame')
6867
->setImageFile($image->getFile())
6968
->getUrl()
7069
);
7170
$image->setData(
7271
'large_image_url',
73-
$this->_imageHelper->init($product, 'product_page_image_large')
74-
->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)
72+
$this->_imageHelper->init($product, 'product_page_image_large_no_frame')
7573
->setImageFile($image->getFile())
7674
->getUrl()
7775
);

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

+5-9
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ public function init($product, $imageId, $attributes = [])
195195
protected function setImageProperties()
196196
{
197197
$this->_getModel()->setDestinationSubdir($this->getType());
198-
199198
$this->_getModel()->setWidth($this->getWidth());
200199
$this->_getModel()->setHeight($this->getHeight());
201200

@@ -241,25 +240,25 @@ protected function setWatermarkProperties()
241240
{
242241
$this->setWatermark(
243242
$this->scopeConfig->getValue(
244-
"design/watermark/{$this->_getModel()->getDestinationSubdir()}_image",
243+
"design/watermark/{$this->getType()}_image",
245244
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
246245
)
247246
);
248247
$this->setWatermarkImageOpacity(
249248
$this->scopeConfig->getValue(
250-
"design/watermark/{$this->_getModel()->getDestinationSubdir()}_imageOpacity",
249+
"design/watermark/{$this->getType()}_imageOpacity",
251250
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
252251
)
253252
);
254253
$this->setWatermarkPosition(
255254
$this->scopeConfig->getValue(
256-
"design/watermark/{$this->_getModel()->getDestinationSubdir()}_position",
255+
"design/watermark/{$this->getType()}_position",
257256
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
258257
)
259258
);
260259
$this->setWatermarkSize(
261260
$this->scopeConfig->getValue(
262-
"design/watermark/{$this->_getModel()->getDestinationSubdir()}_size",
261+
"design/watermark/{$this->getType()}_size",
263262
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
264263
)
265264
);
@@ -497,10 +496,7 @@ protected function initBaseFile()
497496
protected function isScheduledActionsAllowed()
498497
{
499498
$model = $this->_getModel();
500-
if ($model->isBaseFilePlaceholder()
501-
&& $model->getNewFile() === true
502-
|| $model->isCached()
503-
) {
499+
if ($model->isBaseFilePlaceholder() || $model->isCached()) {
504500
return false;
505501
}
506502
return true;

app/code/Magento/Catalog/Model/Product/Image.php

+90-100
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
namespace Magento\Catalog\Model\Product;
1313

1414
use Magento\Framework\App\Filesystem\DirectoryList;
15+
use Magento\Framework\App\ObjectManager;
1516
use Magento\Framework\Image as MagentoImage;
16-
use Magento\Store\Model\Store;
1717

1818
/**
1919
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -170,6 +170,21 @@ class Image extends \Magento\Framework\Model\AbstractModel
170170
*/
171171
protected $_storeManager;
172172

173+
/**
174+
* @var \Magento\Catalog\Model\View\Asset\ImageFactory
175+
*/
176+
private $viewAssetImageFactory;
177+
178+
/**
179+
* @var \Magento\Catalog\Model\View\Asset\PlaceholderFactory
180+
*/
181+
private $viewAssetPlaceholderFactory;
182+
183+
/**
184+
* @var \Magento\Framework\View\Asset\LocalInterface
185+
*/
186+
private $imageAsset;
187+
173188
/**
174189
* @param \Magento\Framework\Model\Context $context
175190
* @param \Magento\Framework\Registry $registry
@@ -184,6 +199,8 @@ class Image extends \Magento\Framework\Model\AbstractModel
184199
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
185200
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
186201
* @param array $data
202+
* @param \Magento\Catalog\Model\View\Asset\ImageFactory $assetImageFactory
203+
* @param \Magento\Catalog\Model\View\Asset\PlaceholderFactory $assetPlaceholderFactory
187204
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
188205
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
189206
*/
@@ -200,18 +217,31 @@ public function __construct(
200217
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
201218
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
202219
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
203-
array $data = []
220+
array $data = [],
221+
\Magento\Catalog\Model\View\Asset\ImageFactory $assetImageFactory = null,
222+
\Magento\Catalog\Model\View\Asset\PlaceholderFactory $assetPlaceholderFactory = null
204223
) {
205224
$this->_storeManager = $storeManager;
206225
$this->_catalogProductMediaConfig = $catalogProductMediaConfig;
207226
$this->_coreFileStorageDatabase = $coreFileStorageDatabase;
208227
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
209228
$this->_mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
210-
$result = $this->_mediaDirectory->create($this->_catalogProductMediaConfig->getBaseMediaPath());
211229
$this->_imageFactory = $imageFactory;
212230
$this->_assetRepo = $assetRepo;
213231
$this->_viewFileSystem = $viewFileSystem;
214232
$this->_scopeConfig = $scopeConfig;
233+
$this->viewAssetImageFactory = $assetImageFactory;
234+
if ($this->viewAssetImageFactory == null) {
235+
$this->viewAssetImageFactory = ObjectManager::getInstance()->get(
236+
\Magento\Catalog\Model\View\Asset\ImageFactory::class
237+
);
238+
}
239+
$this->viewAssetPlaceholderFactory = $assetPlaceholderFactory;
240+
if ($this->viewAssetPlaceholderFactory == null) {
241+
$this->viewAssetPlaceholderFactory = ObjectManager::getInstance()->get(
242+
\Magento\Catalog\Model\View\Asset\PlaceholderFactory::class
243+
);
244+
}
215245
}
216246

217247
/**
@@ -450,85 +480,29 @@ protected function _rgbToString($rgbArray)
450480
* @param string $file
451481
* @return $this
452482
* @throws \Exception
453-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
454-
* @SuppressWarnings(PHPMD.NPathComplexity)
455483
*/
456484
public function setBaseFile($file)
457485
{
458486
$this->_isBaseFilePlaceholder = false;
459487

460-
if ($file && 0 !== strpos($file, '/', 0)) {
461-
$file = '/' . $file;
462-
}
463-
$baseDir = $this->_catalogProductMediaConfig->getBaseMediaPath();
464-
465-
if ('/no_selection' == $file) {
466-
$file = null;
467-
}
468-
if ($file) {
469-
if (!$this->_fileExists($baseDir . $file) || !$this->_checkMemory($baseDir . $file)) {
470-
$file = null;
471-
}
472-
}
473-
if (!$file) {
488+
$this->imageAsset = $this->viewAssetImageFactory->create(
489+
[
490+
'miscParams' => $this->getMiscParams(),
491+
'filePath' => $file,
492+
]
493+
);
494+
if ($file == 'no_selection' || !$this->_fileExists($this->imageAsset->getSourceFile())
495+
|| !$this->_checkMemory($this->imageAsset->getSourceFile())
496+
) {
474497
$this->_isBaseFilePlaceholder = true;
475-
// check if placeholder defined in config
476-
$isConfigPlaceholder = $this->_scopeConfig->getValue(
477-
"catalog/placeholder/{$this->getDestinationSubdir()}_placeholder",
478-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
498+
$this->imageAsset = $this->viewAssetPlaceholderFactory->create(
499+
[
500+
'type' => $this->getDestinationSubdir(),
501+
]
479502
);
480-
$configPlaceholder = '/placeholder/' . $isConfigPlaceholder;
481-
if (!empty($isConfigPlaceholder) && $this->_fileExists($baseDir . $configPlaceholder)) {
482-
$file = $configPlaceholder;
483-
} else {
484-
$this->_newFile = true;
485-
return $this;
486-
}
487-
}
488-
489-
$baseFile = $baseDir . $file;
490-
491-
if (!$file || !$this->_mediaDirectory->isFile($baseFile)) {
492-
throw new \Exception(__('We can\'t find the image file.'));
493-
}
494-
495-
$this->_baseFile = $baseFile;
496-
497-
// build new filename (most important params)
498-
$path = [
499-
$this->_catalogProductMediaConfig->getBaseMediaPath(),
500-
'cache',
501-
$this->getDestinationSubdir(),
502-
];
503-
if (!empty($this->_width) || !empty($this->_height)) {
504-
$path[] = "{$this->_width}x{$this->_height}";
505503
}
506504

507-
// add misk params as a hash
508-
$miscParams = [
509-
($this->_keepAspectRatio ? '' : 'non') . 'proportional',
510-
($this->_keepFrame ? '' : 'no') . 'frame',
511-
($this->_keepTransparency ? '' : 'no') . 'transparency',
512-
($this->_constrainOnly ? 'do' : 'not') . 'constrainonly',
513-
$this->_rgbToString($this->_backgroundColor),
514-
'angle' . $this->_angle,
515-
'quality' . $this->_quality,
516-
];
517-
518-
// if has watermark add watermark params to hash
519-
if ($this->getWatermarkFile()) {
520-
$miscParams[] = $this->getWatermarkFile();
521-
$miscParams[] = $this->getWatermarkImageOpacity();
522-
$miscParams[] = $this->getWatermarkPosition();
523-
$miscParams[] = $this->getWatermarkWidth();
524-
$miscParams[] = $this->getWatermarkHeight();
525-
}
526-
527-
$path[] = md5(implode('_', $miscParams));
528-
529-
// append prepared filename
530-
$this->_newFile = implode('/', $path) . $file;
531-
// the $file contains heading slash
505+
$this->_baseFile = $this->imageAsset->getSourceFile();
532506

533507
return $this;
534508
}
@@ -542,6 +516,7 @@ public function getBaseFile()
542516
}
543517

544518
/**
519+
* @deprecated
545520
* @return bool|string
546521
*/
547522
public function getNewFile()
@@ -690,10 +665,10 @@ public function setWatermark(
690665
*/
691666
public function saveFile()
692667
{
693-
if ($this->_isBaseFilePlaceholder && $this->_newFile === true) {
668+
if ($this->_isBaseFilePlaceholder) {
694669
return $this;
695670
}
696-
$filename = $this->_mediaDirectory->getAbsolutePath($this->getNewFile());
671+
$filename = $this->getBaseFile() ? $this->imageAsset->getPath() : null;
697672
$this->getImageProcessor()->save($filename);
698673
$this->_coreFileStorageDatabase->saveFile($filename);
699674
return $this;
@@ -704,17 +679,7 @@ public function saveFile()
704679
*/
705680
public function getUrl()
706681
{
707-
if ($this->_newFile === true) {
708-
$url = $this->_assetRepo->getUrl(
709-
"Magento_Catalog::images/product/placeholder/{$this->getDestinationSubdir()}.jpg"
710-
);
711-
} else {
712-
$url = $this->_storeManager->getStore()->getBaseUrl(
713-
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
714-
) . $this->_newFile;
715-
}
716-
717-
return $url;
682+
return $this->imageAsset->getUrl();
718683
}
719684

720685
/**
@@ -740,9 +705,7 @@ public function getDestinationSubdir()
740705
*/
741706
public function isCached()
742707
{
743-
if (is_string($this->_newFile)) {
744-
return $this->_fileExists($this->_newFile);
745-
}
708+
return file_exists($this->imageAsset->getPath());
746709
}
747710

748711
/**
@@ -939,18 +902,45 @@ protected function _fileExists($filename)
939902
*/
940903
public function getResizedImageInfo()
941904
{
942-
$fileInfo = null;
943-
if ($this->_newFile === true) {
944-
$asset = $this->_assetRepo->createAsset(
945-
"Magento_Catalog::images/product/placeholder/{$this->getDestinationSubdir()}.jpg"
946-
);
947-
$img = $asset->getSourceFile();
948-
$fileInfo = getimagesize($img);
905+
if ($this->isBaseFilePlaceholder() == true) {
906+
$image = $this->imageAsset->getSourceFile();
949907
} else {
950-
if ($this->_mediaDirectory->isFile($this->_mediaDirectory->getAbsolutePath($this->_newFile))) {
951-
$fileInfo = getimagesize($this->_mediaDirectory->getAbsolutePath($this->_newFile));
952-
}
908+
$image = $this->imageAsset->getPath();
953909
}
954-
return $fileInfo;
910+
911+
return getimagesize($image);
912+
}
913+
914+
/**
915+
* Retrieve misc params based on all image attributes
916+
*
917+
* @return array
918+
* @SuppressWarnings(PHPMD.NPathComplexity)
919+
*/
920+
private function getMiscParams()
921+
{
922+
$miscParams = [
923+
'image_type' => $this->getDestinationSubdir(),
924+
'image_height' => $this->getHeight(),
925+
'image_width' => $this->getWidth(),
926+
'keep_aspect_ratio' => ($this->_keepAspectRatio ? '' : 'non') . 'proportional',
927+
'keep_frame' => ($this->_keepFrame ? '' : 'no') . 'frame',
928+
'keep_transparency' => ($this->_keepTransparency ? '' : 'no') . 'transparency',
929+
'constrain_only' => ($this->_constrainOnly ? 'do' : 'not') . 'constrainonly',
930+
'background' => $this->_rgbToString($this->_backgroundColor),
931+
'angle' => $this->_angle,
932+
'quality' => $this->_quality,
933+
];
934+
935+
// if has watermark add watermark params to hash
936+
if ($this->getWatermarkFile()) {
937+
$miscParams['watermark_file'] = $this->getWatermarkFile();
938+
$miscParams['watermark_image_opacity'] = $this->getWatermarkImageOpacity();
939+
$miscParams['watermark_position'] = $this->getWatermarkPosition();
940+
$miscParams['watermark_width'] = $this->getWatermarkWidth();
941+
$miscParams['watermark_height'] = $this->getWatermarkHeight();
942+
}
943+
944+
return $miscParams;
955945
}
956946
}

0 commit comments

Comments
 (0)