Skip to content

Commit ec376bd

Browse files
authored
Merge 81d30a9 into 2.1-develop
2 parents 413f42f + 81d30a9 commit ec376bd

File tree

30 files changed

+1289
-210
lines changed

30 files changed

+1289
-210
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Catalog\Block\Product;
77

8+
use Magento\Framework\App\ObjectManager;
9+
810
/**
911
* Class AbstractProduct
1012
* @SuppressWarnings(PHPMD.NumberOfChildren)
@@ -97,9 +99,13 @@ class AbstractProduct extends \Magento\Framework\View\Element\Template
9799
/**
98100
* @param Context $context
99101
* @param array $data
102+
* @param ImageBlockBuilder|null $imageBlockBuilder
100103
*/
101-
public function __construct(\Magento\Catalog\Block\Product\Context $context, array $data = [])
102-
{
104+
public function __construct(
105+
Context $context,
106+
array $data = [],
107+
ImageBlockBuilder $imageBlockBuilder = null
108+
) {
103109
$this->_imageHelper = $context->getImageHelper();
104110
$this->imageBuilder = $context->getImageBuilder();
105111
$this->_compareProduct = $context->getCompareProduct();
@@ -111,6 +117,10 @@ public function __construct(\Magento\Catalog\Block\Product\Context $context, arr
111117
$this->_mathRandom = $context->getMathRandom();
112118
$this->reviewRenderer = $context->getReviewRenderer();
113119
$this->stockRegistry = $context->getStockRegistry();
120+
$this->assign(
121+
'imageBlockBuilder',
122+
$imageBlockBuilder ?: ObjectManager::getInstance()->get(ImageBlockBuilder::class)
123+
);
114124
parent::__construct($context, $data);
115125
}
116126

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Block\Product;
7+
8+
use Magento\Catalog\Model\View\Asset\ImageFactory as AssetImageFactory;
9+
use Magento\Framework\View\ConfigInterface;
10+
use Magento\Catalog\Model\Product\Image\ParamsBuilder;
11+
use Magento\Catalog\Model\View\Asset\Image as AssetImage;
12+
use Magento\Catalog\Model\Product\Image\SizeCache;
13+
14+
/**
15+
* Used to build product image blocks in product list blocks.
16+
*/
17+
class ImageBlockBuilder
18+
{
19+
/**
20+
* @var ConfigInterface
21+
*/
22+
private $presentationConfig;
23+
24+
/**
25+
* @var AssetImageFactory
26+
*/
27+
private $viewAssetImageFactory;
28+
29+
/**
30+
* @var ImageFactory
31+
*/
32+
private $imageBlockFactory;
33+
34+
/**
35+
* @var ParamsBuilder
36+
*/
37+
private $imageParamsBuilder;
38+
39+
/**
40+
* @var SizeCache
41+
*/
42+
private $sizeCache;
43+
44+
/**
45+
* @param ConfigInterface $presentationConfig
46+
* @param AssetImageFactory $viewAssetImageFactory
47+
* @param ImageFactory $imageBlockFactory
48+
* @param ParamsBuilder $imageParamsBuilder
49+
* @param SizeCache $sizeCache
50+
*/
51+
public function __construct(
52+
ConfigInterface $presentationConfig,
53+
AssetImageFactory $viewAssetImageFactory,
54+
ImageFactory $imageBlockFactory,
55+
ParamsBuilder $imageParamsBuilder,
56+
SizeCache $sizeCache
57+
) {
58+
$this->presentationConfig = $presentationConfig->getViewConfig();
59+
$this->viewAssetImageFactory = $viewAssetImageFactory;
60+
$this->imageBlockFactory = $imageBlockFactory;
61+
$this->imageParamsBuilder = $imageParamsBuilder;
62+
$this->sizeCache = $sizeCache;
63+
}
64+
65+
/**
66+
* Get image size
67+
*
68+
* @param AssetImage $imageAsset
69+
* @return array
70+
* @throws \Exception
71+
*/
72+
private function getImageSize(AssetImage $imageAsset)
73+
{
74+
$imagePath = $imageAsset->getPath();
75+
$size = $this->sizeCache->load($imagePath);
76+
if (!$size) {
77+
$size = getimagesize($imagePath);
78+
if (!$size) {
79+
throw new \Exception('An error occurred while reading file: ' . $imagePath);
80+
}
81+
$this->sizeCache->save($size[0], $size[1], $imagePath);
82+
$size = ['width' => $size[0], 'height' => $size[1]];
83+
}
84+
85+
return $size;
86+
}
87+
88+
/**
89+
* Build image block for product and for specific display area (product grid, list, etc)
90+
*
91+
* @param \Magento\Catalog\Model\Product $product
92+
* @param string $displayArea
93+
* @return Image
94+
* @SuppressWarnings(PHPMD.NPathComplexity)
95+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
96+
*/
97+
public function buildBlock($product, $displayArea)
98+
{
99+
$imageArguments = $this->presentationConfig->getMediaAttributes(
100+
'Magento_Catalog',
101+
'images',
102+
$displayArea
103+
);
104+
105+
$image = $this->imageParamsBuilder->build($imageArguments);
106+
107+
$type = isset($imageArguments['type']) ? $imageArguments['type'] : null;
108+
$baseFilePath = $product->getData($type);
109+
110+
$imageAsset = $this->viewAssetImageFactory->create(
111+
[
112+
'miscParams' => $image,
113+
'filePath' => $baseFilePath,
114+
]
115+
);
116+
117+
$label = $product->getData($imageArguments['type'] . '_' . 'label');
118+
if (empty($label)) {
119+
$label = $product->getName();
120+
}
121+
122+
$frame = isset($imageArguments['frame']) ? $imageArguments ['frame'] : null;
123+
if (empty($frame)) {
124+
$frame = $this->presentationConfig->getVarValue('Magento_Catalog', 'product_image_white_borders');
125+
}
126+
127+
$template = $frame
128+
? 'Magento_Catalog::product/image.phtml'
129+
: 'Magento_Catalog::product/image_with_borders.phtml';
130+
131+
$width = $image['image_width'];
132+
$height = $image['image_height'];
133+
134+
try {
135+
$resizedInfo = $this->getImageSize($imageAsset);
136+
} catch (\Exception $e) {
137+
$resizedInfo['width'] = $width;
138+
$resizedInfo['height'] = $height;
139+
}
140+
141+
$data = [
142+
'data' => [
143+
'template' => $template,
144+
'image_url' => $imageAsset->getUrl(),
145+
'width' => $width,
146+
'height' => $height,
147+
'label' => $label,
148+
'ratio' => ($width && $height) ? $height / $width : 1,
149+
'resized_image_width' => empty($resizedInfo['width']) ? $width : $resizedInfo['width'],
150+
'resized_image_height' => empty($resizedInfo['height']) ? $height : $resizedInfo['height'],
151+
],
152+
];
153+
154+
return $this->imageBlockFactory->create($data);
155+
}
156+
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,7 @@ protected function setImageProperties()
200200

201201
// Set 'keep frame' flag
202202
$frame = $this->getFrame();
203-
if (!empty($frame)) {
204-
$this->_getModel()->setKeepFrame($frame);
205-
}
203+
$this->_getModel()->setKeepFrame($frame);
206204

207205
// Set 'constrain only' flag
208206
$constrain = $this->getAttribute('constrain');
@@ -825,10 +823,10 @@ public function getHeight()
825823
public function getFrame()
826824
{
827825
$frame = $this->getAttribute('frame');
828-
if (empty($frame)) {
826+
if ($frame === null) {
829827
$frame = $this->getConfigView()->getVarValue('Magento_Catalog', 'product_image_white_borders');
830828
}
831-
return $frame;
829+
return (bool)$frame;
832830
}
833831

834832
/**

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

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@
33
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
7-
/**
8-
* Catalog product link model
9-
*
10-
* @author Magento Core Team <core@magentocommerce.com>
11-
*/
126
namespace Magento\Catalog\Model\Product;
137

148
use Magento\Framework\App\Filesystem\DirectoryList;
159
use Magento\Framework\App\ObjectManager;
1610
use Magento\Framework\Image as MagentoImage;
11+
use Magento\Catalog\Model\Product\Image\ParamsBuilder;
12+
use Magento\Catalog\Model\Product\Image\SizeCache;
1713

1814
/**
1915
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -185,6 +181,16 @@ class Image extends \Magento\Framework\Model\AbstractModel
185181
*/
186182
private $imageAsset;
187183

184+
/**
185+
* @var ParamsBuilder
186+
*/
187+
private $paramsBuilder;
188+
189+
/**
190+
* @var SizeCache
191+
*/
192+
private $sizeCache;
193+
188194
/**
189195
* @param \Magento\Framework\Model\Context $context
190196
* @param \Magento\Framework\Registry $registry
@@ -201,6 +207,8 @@ class Image extends \Magento\Framework\Model\AbstractModel
201207
* @param array $data
202208
* @param \Magento\Catalog\Model\View\Asset\ImageFactory $assetImageFactory
203209
* @param \Magento\Catalog\Model\View\Asset\PlaceholderFactory $assetPlaceholderFactory
210+
* @param ParamsBuilder $paramsBuilder
211+
* @param SizeCache $sizeCache
204212
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
205213
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
206214
*/
@@ -219,7 +227,9 @@ public function __construct(
219227
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
220228
array $data = [],
221229
\Magento\Catalog\Model\View\Asset\ImageFactory $assetImageFactory = null,
222-
\Magento\Catalog\Model\View\Asset\PlaceholderFactory $assetPlaceholderFactory = null
230+
\Magento\Catalog\Model\View\Asset\PlaceholderFactory $assetPlaceholderFactory = null,
231+
ParamsBuilder $paramsBuilder = null,
232+
SizeCache $sizeCache = null
223233
) {
224234
$this->_storeManager = $storeManager;
225235
$this->_catalogProductMediaConfig = $catalogProductMediaConfig;
@@ -242,6 +252,8 @@ public function __construct(
242252
\Magento\Catalog\Model\View\Asset\PlaceholderFactory::class
243253
);
244254
}
255+
$this->paramsBuilder = $paramsBuilder ?: ObjectManager::getInstance()->get(ParamsBuilder::class);
256+
$this->sizeCache = $sizeCache ?: ObjectManager::getInstance()->get(SizeCache::class);
245257
}
246258

247259
/**
@@ -463,15 +475,7 @@ protected function _getNeedMemoryForFile($file = null)
463475
*/
464476
protected function _rgbToString($rgbArray)
465477
{
466-
$result = [];
467-
foreach ($rgbArray as $value) {
468-
if (null === $value) {
469-
$result[] = 'null';
470-
} else {
471-
$result[] = sprintf('%02s', dechex($value));
472-
}
473-
}
474-
return implode($result);
478+
return $this->paramsBuilder->rgbToString($rgbArray);
475479
}
476480

477481
/**
@@ -661,6 +665,7 @@ public function setWatermark(
661665
}
662666

663667
/**
668+
* Save image file
664669
* @return $this
665670
*/
666671
public function saveFile()
@@ -671,6 +676,12 @@ public function saveFile()
671676
$filename = $this->getBaseFile() ? $this->imageAsset->getPath() : null;
672677
$this->getImageProcessor()->save($filename);
673678
$this->_coreFileStorageDatabase->saveFile($filename);
679+
$this->sizeCache->save(
680+
$this->getWidth(),
681+
$this->getHeight(),
682+
$this->imageAsset->getPath()
683+
);
684+
674685
return $this;
675686
}
676687

@@ -919,28 +930,19 @@ public function getResizedImageInfo()
919930
*/
920931
private function getMiscParams()
921932
{
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;
933+
return $this->paramsBuilder->build(
934+
[
935+
'type' => $this->getDestinationSubdir(),
936+
'width' => $this->getWidth(),
937+
'height' => $this->getHeight(),
938+
'frame' => $this->_keepFrame,
939+
'constrain' => $this->_constrainOnly,
940+
'aspect_ratio' => $this->_keepAspectRatio,
941+
'transparency' => $this->_keepTransparency,
942+
'background' => $this->_backgroundColor,
943+
'angle' => $this->_angle,
944+
'quality' => $this->_quality
945+
]
946+
);
945947
}
946948
}

0 commit comments

Comments
 (0)