Skip to content

Commit 164e946

Browse files
committed
MAGETWO-84480: Add cache for getimagesize() function for product images
1 parent f31754d commit 164e946

File tree

1 file changed

+45
-95
lines changed
  • app/code/Magento/Catalog/Model/Product

1 file changed

+45
-95
lines changed

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

Lines changed: 45 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -370,86 +370,6 @@ public function setSize($size)
370370
return $this;
371371
}
372372

373-
/**
374-
* @param string|null $file
375-
* @return bool
376-
*/
377-
protected function _checkMemory($file = null)
378-
{
379-
return $this->_getMemoryLimit() > $this->_getMemoryUsage() + $this->_getNeedMemoryForFile(
380-
$file
381-
)
382-
|| $this->_getMemoryLimit() == -1;
383-
}
384-
385-
/**
386-
* @return string
387-
*/
388-
protected function _getMemoryLimit()
389-
{
390-
$memoryLimit = trim(strtoupper(ini_get('memory_limit')));
391-
392-
if (!isset($memoryLimit[0])) {
393-
$memoryLimit = "128M";
394-
}
395-
396-
if (substr($memoryLimit, -1) == 'K') {
397-
return substr($memoryLimit, 0, -1) * 1024;
398-
}
399-
if (substr($memoryLimit, -1) == 'M') {
400-
return substr($memoryLimit, 0, -1) * 1024 * 1024;
401-
}
402-
if (substr($memoryLimit, -1) == 'G') {
403-
return substr($memoryLimit, 0, -1) * 1024 * 1024 * 1024;
404-
}
405-
return $memoryLimit;
406-
}
407-
408-
/**
409-
* @return int
410-
*/
411-
protected function _getMemoryUsage()
412-
{
413-
if (function_exists('memory_get_usage')) {
414-
return memory_get_usage();
415-
}
416-
return 0;
417-
}
418-
419-
/**
420-
* @param string|null $file
421-
* @return float|int
422-
* @SuppressWarnings(PHPMD.NPathComplexity)
423-
*/
424-
protected function _getNeedMemoryForFile($file = null)
425-
{
426-
$file = $file === null ? $this->getBaseFile() : $file;
427-
if (!$file) {
428-
return 0;
429-
}
430-
431-
if (!$this->_mediaDirectory->isExist($file)) {
432-
return 0;
433-
}
434-
435-
$imageInfo = $this->getimagesize($this->_mediaDirectory->getAbsolutePath($file));
436-
437-
if (!isset($imageInfo[0]) || !isset($imageInfo[1])) {
438-
return 0;
439-
}
440-
if (!isset($imageInfo['channels'])) {
441-
// if there is no info about this parameter lets set it for maximum
442-
$imageInfo['channels'] = 4;
443-
}
444-
if (!isset($imageInfo['bits'])) {
445-
// if there is no info about this parameter lets set it for maximum
446-
$imageInfo['bits'] = 8;
447-
}
448-
return round(
449-
($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $imageInfo['channels'] / 8 + Pow(2, 16)) * 1.65
450-
);
451-
}
452-
453373
/**
454374
* Convert array of 3 items (decimal r, g, b) to string of their hex values
455375
*
@@ -486,9 +406,7 @@ public function setBaseFile($file)
486406
'filePath' => $file,
487407
]
488408
);
489-
if ($file == 'no_selection' || !$this->_fileExists($this->imageAsset->getSourceFile())
490-
|| !$this->_checkMemory($this->imageAsset->getSourceFile())
491-
) {
409+
if ($file == 'no_selection' || !$this->_fileExists($this->imageAsset->getSourceFile())) {
492410
$this->_isBaseFilePlaceholder = true;
493411
$this->imageAsset = $this->viewAssetPlaceholderFactory->create(
494412
[
@@ -696,11 +614,14 @@ public function getDestinationSubdir()
696614
}
697615

698616
/**
699-
* @return bool|void
617+
* @return bool
700618
*/
701619
public function isCached()
702620
{
703-
return file_exists($this->imageAsset->getPath());
621+
return (
622+
is_array($this->loadImageInfoFromCache($this->imageAsset->getPath())) ||
623+
file_exists($this->imageAsset->getPath())
624+
);
704625
}
705626

706627
/**
@@ -955,17 +876,46 @@ private function getMiscParams()
955876
*/
956877
private function getImageSize($imagePath)
957878
{
958-
$key = $this->cachePrefix . $imagePath;
959-
$size = $this->_cacheManager->load($key);
960-
if (!$size) {
961-
$size = getimagesize($imagePath);
962-
$this->_cacheManager->save(
963-
$this->serializer->serialize($size),
964-
$key
965-
);
879+
$imageInfo = $this->loadImageInfoFromCache($imagePath);
880+
if (!isset($imageInfo['size'])) {
881+
$imageSize = getimagesize($imagePath);
882+
$this->saveImageInfoToCache(['size' => $imageSize], $imagePath);
883+
return $imageSize;
884+
} else {
885+
return $imageInfo['size'];
886+
}
887+
}
888+
889+
/**
890+
* Save image data to cache
891+
*
892+
* @param array $imageInfo
893+
* @param string $imagePath
894+
* @return void
895+
*/
896+
private function saveImageInfoToCache(array $imageInfo, string $imagePath)
897+
{
898+
$imagePath = $this->cachePrefix . $imagePath;
899+
$this->_cacheManager->save(
900+
$this->serializer->serialize($imageInfo),
901+
$imagePath
902+
);
903+
}
904+
905+
/**
906+
* Load image data from cache
907+
*
908+
* @param string $imagePath
909+
* @return array|false
910+
*/
911+
private function loadImageInfoFromCache(string $imagePath)
912+
{
913+
$imagePath = $this->cachePrefix . $imagePath;
914+
$cacheData = $this->_cacheManager->load($imagePath);
915+
if (!$cacheData) {
916+
return false;
966917
} else {
967-
$size = $this->serializer->unserialize($size);
918+
return $this->serializer->unserialize($cacheData);
968919
}
969-
return $size;
970920
}
971921
}

0 commit comments

Comments
 (0)