Skip to content

Commit 47dbb1a

Browse files
committed
MAGETWO-65480: [Backport] - [Performance] Image metadata caching - for 2.1.6
1 parent 8f75af3 commit 47dbb1a

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

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

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@
88
use Magento\Catalog\Model\View\Asset\ImageFactory as AssetImageFactory;
99
use Magento\Framework\View\ConfigInterface;
1010
use Magento\Catalog\Model\Product\Image\ParamsBuilder;
11+
use Magento\Framework\App\CacheInterface;
12+
use Magento\Catalog\Model\View\Asset\Image as AssetImage;
1113

1214
/**
1315
* Used to build product image blocks in product list blocks.
1416
*/
1517
class ImageBlockBuilder
1618
{
19+
/**
20+
* @var string
21+
*/
22+
private $cachePrefix = 'IMG_INFO';
23+
1724
/**
1825
* @var ConfigInterface
1926
*/
@@ -34,22 +41,52 @@ class ImageBlockBuilder
3441
*/
3542
private $imageParamsBuilder;
3643

44+
/**
45+
* @var CacheInterface
46+
*/
47+
private $cache;
48+
3749
/**
3850
* @param ConfigInterface $presentationConfig
3951
* @param AssetImageFactory $viewAssetImageFactory
4052
* @param ImageFactory $imageBlockFactory
4153
* @param ParamsBuilder $imageParamsBuilder
54+
* @param CacheInterface $cache
4255
*/
4356
public function __construct(
4457
ConfigInterface $presentationConfig,
4558
AssetImageFactory $viewAssetImageFactory,
4659
ImageFactory $imageBlockFactory,
47-
ParamsBuilder $imageParamsBuilder
60+
ParamsBuilder $imageParamsBuilder,
61+
CacheInterface $cache
4862
) {
4963
$this->presentationConfig = $presentationConfig->getViewConfig();
5064
$this->viewAssetImageFactory = $viewAssetImageFactory;
5165
$this->imageBlockFactory = $imageBlockFactory;
5266
$this->imageParamsBuilder = $imageParamsBuilder;
67+
$this->cache = $cache;
68+
}
69+
70+
/**
71+
* Get image size
72+
*
73+
* @param AssetImage $imageAsset
74+
* @return array
75+
*/
76+
private function getImageSize(AssetImage $imageAsset)
77+
{
78+
$key = $this->cachePrefix . $imageAsset->getPath();
79+
$size = $this->cache->load($key);
80+
if (!$size) {
81+
$size = getimagesize($imageAsset->getPath());
82+
$this->cache->save(
83+
serialize(['width' => $size[0], 'height' => $size[1]]),
84+
$key
85+
);
86+
} else {
87+
$size = unserialize($size);
88+
}
89+
return $size;
5390
}
5491

5592
/**
@@ -99,10 +136,10 @@ public function buildBlock($product, $displayArea)
99136
$height = $image['image_height'];
100137

101138
try {
102-
$resizedInfo = getimagesize($imageAsset->getPath());
139+
$resizedInfo = $this->getImageSize($imageAsset);
103140
} catch (\Exception $e) {
104-
$resizedInfo[0] = $width;
105-
$resizedInfo[1] = $height;
141+
$resizedInfo['width'] = $width;
142+
$resizedInfo['height'] = $height;
106143
}
107144

108145
$data = [
@@ -112,9 +149,9 @@ public function buildBlock($product, $displayArea)
112149
'width' => $width,
113150
'height' => $height,
114151
'label' => $label,
115-
'ratio' => ($width && $height) ? $height/$width : 1,
116-
'resized_image_width' => !empty($resizedInfo[0]) ? $resizedInfo[0] : $resizedInfo[0],
117-
'resized_image_height' => !empty($resizedInfo[1]) ? $resizedInfo[1] : $resizedInfo[1],
152+
'ratio' => ($width && $height) ? $height / $width : 1,
153+
'resized_image_width' => $resizedInfo['width'],
154+
'resized_image_height' => $resizedInfo['height'],
118155
],
119156
];
120157

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
*/
2727
class Image extends \Magento\Framework\Model\AbstractModel
2828
{
29+
/**
30+
* @var string
31+
*/
32+
private $cachePrefix = 'IMG_INFO';
33+
2934
/**
3035
* @var int
3136
*/
@@ -662,6 +667,7 @@ public function setWatermark(
662667
}
663668

664669
/**
670+
* Save image file
665671
* @return $this
666672
*/
667673
public function saveFile()
@@ -672,9 +678,23 @@ public function saveFile()
672678
$filename = $this->getBaseFile() ? $this->imageAsset->getPath() : null;
673679
$this->getImageProcessor()->save($filename);
674680
$this->_coreFileStorageDatabase->saveFile($filename);
681+
$this->saveImageSize();
682+
675683
return $this;
676684
}
677685

686+
/**
687+
* Save size to cache
688+
* @return void
689+
*/
690+
private function saveImageSize()
691+
{
692+
$this->_cacheManager->save(
693+
serialize(['width' => $this->getWidth(), 'height' => $this->getHeight()]),
694+
$this->cachePrefix . $this->imageAsset->getPath()
695+
);
696+
}
697+
678698
/**
679699
* @return string
680700
*/

app/code/Magento/Catalog/Test/Unit/Model/Product/ImageTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,21 @@ protected function setUp()
175175
//Settings for backward compatible property
176176
$objectManagerHelper = new ObjectManagerHelper($this);
177177
$this->imageAsset = $this->getMockBuilder(\Magento\Framework\View\Asset\LocalInterface::class)
178+
->disableOriginalConstructor()
178179
->getMockForAbstractClass();
179180
$objectManagerHelper->setBackwardCompatibleProperty(
180181
$this->image,
181182
'imageAsset',
182183
$this->imageAsset
183184
);
185+
$cacheManager = $this->getMockBuilder(\Magento\Framework\App\CacheInterface::class)
186+
->disableOriginalConstructor()
187+
->getMockForAbstractClass();
188+
$objectManagerHelper->setBackwardCompatibleProperty(
189+
$this->image,
190+
'_cacheManager',
191+
$cacheManager
192+
);
184193
}
185194

186195
public function testSetGetQuality()
@@ -370,7 +379,9 @@ public function testSaveFile()
370379
)->disableOriginalConstructor()->getMock();
371380
$this->image->setImageProcessor($imageProcessor);
372381
$this->coreFileHelper->expects($this->once())->method('saveFile')->will($this->returnValue(true));
373-
382+
$this->imageAsset->expects($this->any())
383+
->method('getPath')
384+
->willReturn('specific_path');
374385
$this->image->saveFile();
375386
}
376387

0 commit comments

Comments
 (0)