Skip to content

Commit

Permalink
Merge pull request #921 from magento-tsg/2.1.6-develop-pr14
Browse files Browse the repository at this point in the history
[TSG] Backporting for 2.1 (pr14)
  • Loading branch information
Volodymyr Klymenko authored Mar 13, 2017
2 parents 113d4b9 + 614c4a0 commit 506a93f
Show file tree
Hide file tree
Showing 22 changed files with 1,062 additions and 165 deletions.
14 changes: 12 additions & 2 deletions app/code/Magento/Catalog/Block/Product/AbstractProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Catalog\Block\Product;

use Magento\Framework\App\ObjectManager;

/**
* Class AbstractProduct
* @SuppressWarnings(PHPMD.NumberOfChildren)
Expand Down Expand Up @@ -97,9 +99,13 @@ class AbstractProduct extends \Magento\Framework\View\Element\Template
/**
* @param Context $context
* @param array $data
* @param ImageBlockBuilder|null $imageBlockBuilder
*/
public function __construct(\Magento\Catalog\Block\Product\Context $context, array $data = [])
{
public function __construct(
Context $context,
array $data = [],
ImageBlockBuilder $imageBlockBuilder = null
) {
$this->_imageHelper = $context->getImageHelper();
$this->imageBuilder = $context->getImageBuilder();
$this->_compareProduct = $context->getCompareProduct();
Expand All @@ -111,6 +117,10 @@ public function __construct(\Magento\Catalog\Block\Product\Context $context, arr
$this->_mathRandom = $context->getMathRandom();
$this->reviewRenderer = $context->getReviewRenderer();
$this->stockRegistry = $context->getStockRegistry();
$this->assign(
'imageBlockBuilder',
$imageBlockBuilder ?: ObjectManager::getInstance()->get(ImageBlockBuilder::class)
);
parent::__construct($context, $data);
}

Expand Down
156 changes: 156 additions & 0 deletions app/code/Magento/Catalog/Block/Product/ImageBlockBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Block\Product;

use Magento\Catalog\Model\View\Asset\ImageFactory as AssetImageFactory;
use Magento\Framework\View\ConfigInterface;
use Magento\Catalog\Model\Product\Image\ParamsBuilder;
use Magento\Catalog\Model\View\Asset\Image as AssetImage;
use Magento\Catalog\Model\Product\Image\SizeCache;

/**
* Used to build product image blocks in product list blocks.
*/
class ImageBlockBuilder
{
/**
* @var ConfigInterface
*/
private $presentationConfig;

/**
* @var AssetImageFactory
*/
private $viewAssetImageFactory;

/**
* @var ImageFactory
*/
private $imageBlockFactory;

/**
* @var ParamsBuilder
*/
private $imageParamsBuilder;

/**
* @var SizeCache
*/
private $sizeCache;

/**
* @param ConfigInterface $presentationConfig
* @param AssetImageFactory $viewAssetImageFactory
* @param ImageFactory $imageBlockFactory
* @param ParamsBuilder $imageParamsBuilder
* @param SizeCache $sizeCache
*/
public function __construct(
ConfigInterface $presentationConfig,
AssetImageFactory $viewAssetImageFactory,
ImageFactory $imageBlockFactory,
ParamsBuilder $imageParamsBuilder,
SizeCache $sizeCache
) {
$this->presentationConfig = $presentationConfig->getViewConfig();
$this->viewAssetImageFactory = $viewAssetImageFactory;
$this->imageBlockFactory = $imageBlockFactory;
$this->imageParamsBuilder = $imageParamsBuilder;
$this->sizeCache = $sizeCache;
}

/**
* Get image size
*
* @param AssetImage $imageAsset
* @return array
* @throws \Exception
*/
private function getImageSize(AssetImage $imageAsset)
{
$imagePath = $imageAsset->getPath();
$size = $this->sizeCache->load($imagePath);
if (!$size) {
$size = getimagesize($imagePath);
if (!$size) {
throw new \Exception('An error occurred while reading file: ' . $imagePath);
}
$this->sizeCache->save($size[0], $size[1], $imagePath);
$size = ['width' => $size[0], 'height' => $size[1]];
}

return $size;
}

/**
* Build image block for product and for specific display area (product grid, list, etc)
*
* @param \Magento\Catalog\Model\Product $product
* @param string $displayArea
* @return Image
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function buildBlock($product, $displayArea)
{
$imageArguments = $this->presentationConfig->getMediaAttributes(
'Magento_Catalog',
'images',
$displayArea
);

$image = $this->imageParamsBuilder->build($imageArguments);

$type = isset($imageArguments['type']) ? $imageArguments['type'] : null;
$baseFilePath = $product->getData($type);

$imageAsset = $this->viewAssetImageFactory->create(
[
'miscParams' => $image,
'filePath' => $baseFilePath,
]
);

$label = $product->getData($imageArguments['type'] . '_' . 'label');
if (empty($label)) {
$label = $product->getName();
}

$frame = isset($imageArguments['frame']) ? $imageArguments ['frame'] : null;
if (empty($frame)) {
$frame = $this->presentationConfig->getVarValue('Magento_Catalog', 'product_image_white_borders');
}

$template = $frame
? 'Magento_Catalog::product/image.phtml'
: 'Magento_Catalog::product/image_with_borders.phtml';

$width = $image['image_width'];
$height = $image['image_height'];

try {
$resizedInfo = $this->getImageSize($imageAsset);
} catch (\Exception $e) {
$resizedInfo['width'] = $width;
$resizedInfo['height'] = $height;
}

$data = [
'data' => [
'template' => $template,
'image_url' => $imageAsset->getUrl(),
'width' => $width,
'height' => $height,
'label' => $label,
'ratio' => ($width && $height) ? $height / $width : 1,
'resized_image_width' => empty($resizedInfo['width']) ? $width : $resizedInfo['width'],
'resized_image_height' => empty($resizedInfo['height']) ? $height : $resizedInfo['height'],
],
];

return $this->imageBlockFactory->create($data);
}
}
8 changes: 3 additions & 5 deletions app/code/Magento/Catalog/Helper/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,7 @@ protected function setImageProperties()

// Set 'keep frame' flag
$frame = $this->getFrame();
if (!empty($frame)) {
$this->_getModel()->setKeepFrame($frame);
}
$this->_getModel()->setKeepFrame($frame);

// Set 'constrain only' flag
$constrain = $this->getAttribute('constrain');
Expand Down Expand Up @@ -825,10 +823,10 @@ public function getHeight()
public function getFrame()
{
$frame = $this->getAttribute('frame');
if (empty($frame)) {
if ($frame === null) {
$frame = $this->getConfigView()->getVarValue('Magento_Catalog', 'product_image_white_borders');
}
return $frame;
return (bool)$frame;
}

/**
Expand Down
80 changes: 41 additions & 39 deletions app/code/Magento/Catalog/Model/Product/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

/**
* Catalog product link model
*
* @author Magento Core Team <core@magentocommerce.com>
*/
namespace Magento\Catalog\Model\Product;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Image as MagentoImage;
use Magento\Catalog\Model\Product\Image\ParamsBuilder;
use Magento\Catalog\Model\Product\Image\SizeCache;

/**
* @SuppressWarnings(PHPMD.TooManyFields)
Expand Down Expand Up @@ -185,6 +181,16 @@ class Image extends \Magento\Framework\Model\AbstractModel
*/
private $imageAsset;

/**
* @var ParamsBuilder
*/
private $paramsBuilder;

/**
* @var SizeCache
*/
private $sizeCache;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand All @@ -201,6 +207,8 @@ class Image extends \Magento\Framework\Model\AbstractModel
* @param array $data
* @param \Magento\Catalog\Model\View\Asset\ImageFactory $assetImageFactory
* @param \Magento\Catalog\Model\View\Asset\PlaceholderFactory $assetPlaceholderFactory
* @param ParamsBuilder $paramsBuilder
* @param SizeCache $sizeCache
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
Expand All @@ -219,7 +227,9 @@ public function __construct(
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = [],
\Magento\Catalog\Model\View\Asset\ImageFactory $assetImageFactory = null,
\Magento\Catalog\Model\View\Asset\PlaceholderFactory $assetPlaceholderFactory = null
\Magento\Catalog\Model\View\Asset\PlaceholderFactory $assetPlaceholderFactory = null,
ParamsBuilder $paramsBuilder = null,
SizeCache $sizeCache = null
) {
$this->_storeManager = $storeManager;
$this->_catalogProductMediaConfig = $catalogProductMediaConfig;
Expand All @@ -242,6 +252,8 @@ public function __construct(
\Magento\Catalog\Model\View\Asset\PlaceholderFactory::class
);
}
$this->paramsBuilder = $paramsBuilder ?: ObjectManager::getInstance()->get(ParamsBuilder::class);
$this->sizeCache = $sizeCache ?: ObjectManager::getInstance()->get(SizeCache::class);
}

/**
Expand Down Expand Up @@ -463,15 +475,7 @@ protected function _getNeedMemoryForFile($file = null)
*/
protected function _rgbToString($rgbArray)
{
$result = [];
foreach ($rgbArray as $value) {
if (null === $value) {
$result[] = 'null';
} else {
$result[] = sprintf('%02s', dechex($value));
}
}
return implode($result);
return $this->paramsBuilder->rgbToString($rgbArray);
}

/**
Expand Down Expand Up @@ -661,6 +665,7 @@ public function setWatermark(
}

/**
* Save image file
* @return $this
*/
public function saveFile()
Expand All @@ -671,6 +676,12 @@ public function saveFile()
$filename = $this->getBaseFile() ? $this->imageAsset->getPath() : null;
$this->getImageProcessor()->save($filename);
$this->_coreFileStorageDatabase->saveFile($filename);
$this->sizeCache->save(
$this->getWidth(),
$this->getHeight(),
$this->imageAsset->getPath()
);

return $this;
}

Expand Down Expand Up @@ -919,28 +930,19 @@ public function getResizedImageInfo()
*/
private function getMiscParams()
{
$miscParams = [
'image_type' => $this->getDestinationSubdir(),
'image_height' => $this->getHeight(),
'image_width' => $this->getWidth(),
'keep_aspect_ratio' => ($this->_keepAspectRatio ? '' : 'non') . 'proportional',
'keep_frame' => ($this->_keepFrame ? '' : 'no') . 'frame',
'keep_transparency' => ($this->_keepTransparency ? '' : 'no') . 'transparency',
'constrain_only' => ($this->_constrainOnly ? 'do' : 'not') . 'constrainonly',
'background' => $this->_rgbToString($this->_backgroundColor),
'angle' => $this->_angle,
'quality' => $this->_quality,
];

// if has watermark add watermark params to hash
if ($this->getWatermarkFile()) {
$miscParams['watermark_file'] = $this->getWatermarkFile();
$miscParams['watermark_image_opacity'] = $this->getWatermarkImageOpacity();
$miscParams['watermark_position'] = $this->getWatermarkPosition();
$miscParams['watermark_width'] = $this->getWatermarkWidth();
$miscParams['watermark_height'] = $this->getWatermarkHeight();
}

return $miscParams;
return $this->paramsBuilder->build(
[
'type' => $this->getDestinationSubdir(),
'width' => $this->getWidth(),
'height' => $this->getHeight(),
'frame' => $this->_keepFrame,
'constrain' => $this->_constrainOnly,
'aspect_ratio' => $this->_keepAspectRatio,
'transparency' => $this->_keepTransparency,
'background' => $this->_backgroundColor,
'angle' => $this->_angle,
'quality' => $this->_quality
]
);
}
}
Loading

0 comments on commit 506a93f

Please sign in to comment.