Skip to content

Commit 4988790

Browse files
authored
Merge pull request #2478 from magento-performance/MAGETWO-89051
[performance] MAGETWO-89051: Absorb image optimization from 2.1 into 2.3
2 parents 07c2590 + 4b98b0a commit 4988790

File tree

73 files changed

+1944
-1767
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1944
-1767
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,6 @@ protected function getDetailsRendererList()
510510
*/
511511
public function getImage($product, $imageId, $attributes = [])
512512
{
513-
return $this->imageBuilder->setProduct($product)
514-
->setImageId($imageId)
515-
->setAttributes($attributes)
516-
->create();
513+
return $this->imageBuilder->create($product, $imageId, $attributes);
517514
}
518515
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
* @method string getWidth()
1212
* @method string getHeight()
1313
* @method string getLabel()
14-
* @method mixed getResizedImageWidth()
15-
* @method mixed getResizedImageHeight()
1614
* @method float getRatio()
1715
* @method string getCustomAttributes()
1816
* @since 100.0.2

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

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Catalog\Block\Product;
79

810
use Magento\Catalog\Helper\ImageFactory as HelperFactory;
911
use Magento\Catalog\Model\Product;
10-
use Magento\Catalog\Model\Product\Image\NotLoadInfoImageException;
1112

13+
/**
14+
* @deprecated
15+
* @see ImageFactory
16+
*/
1217
class ImageBuilder
1318
{
1419
/**
@@ -117,39 +122,16 @@ protected function getRatio(\Magento\Catalog\Helper\Image $helper)
117122
/**
118123
* Create image block
119124
*
120-
* @return \Magento\Catalog\Block\Product\Image
125+
* @param Product|null $product
126+
* @param string|null $imageId
127+
* @param array|null $attributes
128+
* @return Image
121129
*/
122-
public function create()
130+
public function create(Product $product = null, string $imageId = null, array $attributes = null)
123131
{
124-
/** @var \Magento\Catalog\Helper\Image $helper */
125-
$helper = $this->helperFactory->create()
126-
->init($this->product, $this->imageId);
127-
128-
$template = $helper->getFrame()
129-
? 'Magento_Catalog::product/image.phtml'
130-
: 'Magento_Catalog::product/image_with_borders.phtml';
131-
132-
try {
133-
$imagesize = $helper->getResizedImageInfo();
134-
} catch (NotLoadInfoImageException $exception) {
135-
$imagesize = [$helper->getWidth(), $helper->getHeight()];
136-
}
137-
138-
$data = [
139-
'data' => [
140-
'template' => $template,
141-
'image_url' => $helper->getUrl(),
142-
'width' => $helper->getWidth(),
143-
'height' => $helper->getHeight(),
144-
'label' => $helper->getLabel(),
145-
'ratio' => $this->getRatio($helper),
146-
'custom_attributes' => $this->getCustomAttributes(),
147-
'resized_image_width' => $imagesize[0],
148-
'resized_image_height' => $imagesize[1],
149-
'product_id' => $this->product->getId()
150-
],
151-
];
152-
153-
return $this->imageFactory->create($data);
132+
$product = $product ?? $this->product;
133+
$imageId = $imageId ?? $this->imageId;
134+
$attributes = $attributes ?? $this->attributes;
135+
return $this->imageFactory->create($product, $imageId, $attributes);
154136
}
155137
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Block\Product;
9+
10+
use Magento\Catalog\Block\Product\Image as ImageBlock;
11+
use Magento\Catalog\Model\View\Asset\ImageFactory as AssetImageFactory;
12+
use Magento\Catalog\Model\Product;
13+
use Magento\Catalog\Model\Product\Image\ParamsBuilder;
14+
use Magento\Catalog\Model\View\Asset\PlaceholderFactory;
15+
use Magento\Framework\ObjectManagerInterface;
16+
use Magento\Framework\View\ConfigInterface;
17+
use Magento\Catalog\Helper\Image as ImageHelper;
18+
19+
/**
20+
* Create imageBlock from product and view.xml
21+
*/
22+
class ImageFactory
23+
{
24+
/**
25+
* @var ConfigInterface
26+
*/
27+
private $presentationConfig;
28+
29+
/**
30+
* @var AssetImageFactory
31+
*/
32+
private $viewAssetImageFactory;
33+
34+
/**
35+
* @var ParamsBuilder
36+
*/
37+
private $imageParamsBuilder;
38+
39+
/**
40+
* @var ObjectManagerInterface
41+
*/
42+
private $objectManager;
43+
44+
/**
45+
* @var PlaceholderFactory
46+
*/
47+
private $viewAssetPlaceholderFactory;
48+
49+
/**
50+
* @param ObjectManagerInterface $objectManager
51+
* @param ConfigInterface $presentationConfig
52+
* @param AssetImageFactory $viewAssetImageFactory
53+
* @param PlaceholderFactory $viewAssetPlaceholderFactory
54+
* @param ParamsBuilder $imageParamsBuilder
55+
*/
56+
public function __construct(
57+
ObjectManagerInterface $objectManager,
58+
ConfigInterface $presentationConfig,
59+
AssetImageFactory $viewAssetImageFactory,
60+
PlaceholderFactory $viewAssetPlaceholderFactory,
61+
ParamsBuilder $imageParamsBuilder
62+
) {
63+
$this->objectManager = $objectManager;
64+
$this->presentationConfig = $presentationConfig;
65+
$this->viewAssetPlaceholderFactory = $viewAssetPlaceholderFactory;
66+
$this->viewAssetImageFactory = $viewAssetImageFactory;
67+
$this->imageParamsBuilder = $imageParamsBuilder;
68+
}
69+
70+
/**
71+
* Retrieve image custom attributes for HTML element
72+
*
73+
* @param array $attributes
74+
* @return string
75+
*/
76+
private function getStringCustomAttributes(array $attributes): string
77+
{
78+
$result = [];
79+
foreach ($attributes as $name => $value) {
80+
$result[] = $name . '="' . $value . '"';
81+
}
82+
return !empty($result) ? implode(' ', $result) : '';
83+
}
84+
85+
/**
86+
* Calculate image ratio
87+
*
88+
* @param $width
89+
* @param $height
90+
* @return float
91+
*/
92+
private function getRatio(int $width, int $height): float
93+
{
94+
if ($width && $height) {
95+
return $height / $width;
96+
}
97+
return 1.0;
98+
}
99+
100+
/**
101+
* @param Product $product
102+
*
103+
* @param string $imageType
104+
* @return string
105+
*/
106+
private function getLabel(Product $product, string $imageType): string
107+
{
108+
$label = $product->getData($imageType . '_' . 'label');
109+
if (empty($label)) {
110+
$label = $product->getName();
111+
}
112+
return (string) $label;
113+
}
114+
115+
/**
116+
* Create image block from product
117+
* @param Product $product
118+
* @param string $imageId
119+
* @param array|null $attributes
120+
* @return ImageBlock
121+
*/
122+
public function create(Product $product, string $imageId, array $attributes = null): ImageBlock
123+
{
124+
$viewImageConfig = $this->presentationConfig->getViewConfig()->getMediaAttributes(
125+
'Magento_Catalog',
126+
ImageHelper::MEDIA_TYPE_CONFIG_NODE,
127+
$imageId
128+
);
129+
130+
$imageMiscParams = $this->imageParamsBuilder->build($viewImageConfig);
131+
$originalFilePath = $product->getData($imageMiscParams['image_type']);
132+
133+
if ($originalFilePath === null || $originalFilePath === 'no_selection') {
134+
$imageAsset = $this->viewAssetPlaceholderFactory->create(
135+
[
136+
'type' => $imageMiscParams['image_type']
137+
]
138+
);
139+
} else {
140+
$imageAsset = $this->viewAssetImageFactory->create(
141+
[
142+
'miscParams' => $imageMiscParams,
143+
'filePath' => $originalFilePath,
144+
]
145+
);
146+
}
147+
148+
$data = [
149+
'data' => [
150+
'template' => 'Magento_Catalog::product/image_with_borders.phtml',
151+
'image_url' => $imageAsset->getUrl(),
152+
'width' => $imageMiscParams['image_width'],
153+
'height' => $imageMiscParams['image_height'],
154+
'label' => $this->getLabel($product, $imageMiscParams['image_type']),
155+
'ratio' => $this->getRatio($imageMiscParams['image_width'], $imageMiscParams['image_height']),
156+
'custom_attributes' => $this->getStringCustomAttributes($attributes),
157+
'product_id' => $product->getId()
158+
],
159+
];
160+
161+
return $this->objectManager->create(ImageBlock::class, $data);
162+
}
163+
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Catalog\Helper\Image;
1616
use Magento\Catalog\Model\Product;
1717
use Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface;
18+
use Magento\Catalog\Model\Product\Image\UrlBuilder;
1819
use Magento\Framework\Data\Collection;
1920
use Magento\Framework\DataObject;
2021
use Magento\Framework\App\ObjectManager;
@@ -47,27 +48,35 @@ class Gallery extends AbstractView
4748
*/
4849
private $galleryImagesConfigFactory;
4950

51+
/**
52+
* @var UrlBuilder
53+
*/
54+
private $imageUrlBuilder;
55+
5056
/**
5157
* @param Context $context
5258
* @param ArrayUtils $arrayUtils
5359
* @param EncoderInterface $jsonEncoder
5460
* @param array $data
55-
* @param ImagesConfigFactoryInterface $imagesConfigFactory
61+
* @param ImagesConfigFactoryInterface|null $imagesConfigFactory
5662
* @param array $galleryImagesConfig
63+
* @param UrlBuilder|null $urlBuilder
5764
*/
5865
public function __construct(
5966
Context $context,
6067
ArrayUtils $arrayUtils,
6168
EncoderInterface $jsonEncoder,
6269
array $data = [],
6370
ImagesConfigFactoryInterface $imagesConfigFactory = null,
64-
array $galleryImagesConfig = []
71+
array $galleryImagesConfig = [],
72+
UrlBuilder $urlBuilder = null
6573
) {
6674
parent::__construct($context, $arrayUtils, $data);
6775
$this->jsonEncoder = $jsonEncoder;
6876
$this->galleryImagesConfigFactory = $imagesConfigFactory ?: ObjectManager::getInstance()
6977
->get(ImagesConfigFactoryInterface::class);
7078
$this->galleryImagesConfig = $galleryImagesConfig;
79+
$this->imageUrlBuilder = $urlBuilder ?? ObjectManager::getInstance()->get(UrlBuilder::class);
7180
}
7281

7382
/**
@@ -88,9 +97,7 @@ public function getGalleryImages()
8897
foreach ($galleryImagesConfig as $imageConfig) {
8998
$image->setData(
9099
$imageConfig->getData('data_object_key'),
91-
$this->_imageHelper->init($product, $imageConfig['image_id'])
92-
->setImageFile($image->getData('file'))
93-
->getUrl()
100+
$this->imageUrlBuilder->getUrl($image->getFile(), $imageConfig['image_id'])
94101
);
95102
}
96103
}

0 commit comments

Comments
 (0)