Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Image.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Catalog\Helper;
7 
9 
17 class Image extends AbstractHelper
18 {
22  const MEDIA_TYPE_CONFIG_NODE = 'images';
23 
29  protected $_model;
30 
36  protected $_scheduleResize = true;
37 
43  protected $_scheduleRotate = false;
44 
50  protected $_angle;
51 
57  protected $_watermark;
58 
65 
71  protected $_watermarkSize;
72 
79 
85  protected $_product;
86 
92  protected $_imageFile;
93 
99  protected $_placeholder;
100 
104  protected $_assetRepo;
105 
112 
116  protected $viewConfig;
117 
121  protected $configView;
122 
128  protected $attributes = [];
129 
133  private $viewAssetPlaceholderFactory;
134 
142  public function __construct(
143  \Magento\Framework\App\Helper\Context $context,
144  \Magento\Catalog\Model\Product\ImageFactory $productImageFactory,
145  \Magento\Framework\View\Asset\Repository $assetRepo,
146  \Magento\Framework\View\ConfigInterface $viewConfig,
147  \Magento\Catalog\Model\View\Asset\PlaceholderFactory $placeholderFactory = null
148  ) {
149  $this->_productImageFactory = $productImageFactory;
150  parent::__construct($context);
151  $this->_assetRepo = $assetRepo;
152  $this->viewConfig = $viewConfig;
153  $this->viewAssetPlaceholderFactory = $placeholderFactory
155  ->get(\Magento\Catalog\Model\View\Asset\PlaceholderFactory::class);
156  }
157 
163  protected function _reset()
164  {
165  $this->_model = null;
166  $this->_scheduleRotate = false;
167  $this->_angle = null;
168  $this->_watermark = null;
169  $this->_watermarkPosition = null;
170  $this->_watermarkSize = null;
171  $this->_watermarkImageOpacity = null;
172  $this->_product = null;
173  $this->_imageFile = null;
174  $this->attributes = [];
175  return $this;
176  }
177 
186  public function init($product, $imageId, $attributes = [])
187  {
188  $this->_reset();
189 
190  $this->attributes = array_merge(
191  $this->getConfigView()->getMediaAttributes('Magento_Catalog', self::MEDIA_TYPE_CONFIG_NODE, $imageId),
193  );
194 
195  $this->setProduct($product);
196  $this->setImageProperties();
197  $this->setWatermarkProperties();
198 
199  return $this;
200  }
201 
207  protected function setImageProperties()
208  {
209  $this->_getModel()->setDestinationSubdir($this->getType());
210  $this->_getModel()->setWidth($this->getWidth());
211  $this->_getModel()->setHeight($this->getHeight());
212 
213  // Set 'keep frame' flag
214  $frame = $this->getFrame();
215  if (!empty($frame)) {
216  $this->_getModel()->setKeepFrame($frame);
217  }
218 
219  // Set 'constrain only' flag
220  $constrain = $this->getAttribute('constrain');
221  if (!empty($constrain)) {
222  $this->_getModel()->setConstrainOnly($constrain);
223  }
224 
225  // Set 'keep aspect ratio' flag
226  $aspectRatio = $this->getAttribute('aspect_ratio');
227  if (!empty($aspectRatio)) {
228  $this->_getModel()->setKeepAspectRatio($aspectRatio);
229  }
230 
231  // Set 'transparency' flag
232  $transparency = $this->getAttribute('transparency');
233  if (!empty($transparency)) {
234  $this->_getModel()->setKeepTransparency($transparency);
235  }
236 
237  // Set background color
238  $background = $this->getAttribute('background');
239  if (!empty($background)) {
240  $this->_getModel()->setBackgroundColor($background);
241  }
242 
243  return $this;
244  }
245 
251  protected function setWatermarkProperties()
252  {
253  $this->setWatermark(
254  $this->scopeConfig->getValue(
255  "design/watermark/{$this->getType()}_image",
257  )
258  );
260  $this->scopeConfig->getValue(
261  "design/watermark/{$this->getType()}_imageOpacity",
263  )
264  );
265  $this->setWatermarkPosition(
266  $this->scopeConfig->getValue(
267  "design/watermark/{$this->getType()}_position",
269  )
270  );
271  $this->setWatermarkSize(
272  $this->scopeConfig->getValue(
273  "design/watermark/{$this->getType()}_size",
275  )
276  );
277  return $this;
278  }
279 
289  public function resize($width, $height = null)
290  {
291  $this->_getModel()->setWidth($width)->setHeight($height);
292  $this->_scheduleResize = true;
293  return $this;
294  }
295 
302  public function setQuality($quality)
303  {
304  $this->_getModel()->setQuality($quality);
305  return $this;
306  }
307 
317  public function keepAspectRatio($flag)
318  {
319  $this->_getModel()->setKeepAspectRatio($flag);
320  return $this;
321  }
322 
335  public function keepFrame($flag)
336  {
337  $this->_getModel()->setKeepFrame($flag);
338  return $this;
339  }
340 
353  public function keepTransparency($flag)
354  {
355  $this->_getModel()->setKeepTransparency($flag);
356  return $this;
357  }
358 
367  public function constrainOnly($flag)
368  {
369  $this->_getModel()->setConstrainOnly($flag);
370  return $this;
371  }
372 
383  public function backgroundColor($colorRGB)
384  {
385  // assume that 3 params were given instead of array
386  if (!is_array($colorRGB)) {
387  $colorRGB = func_get_args();
388  }
389  $this->_getModel()->setBackgroundColor($colorRGB);
390  return $this;
391  }
392 
399  public function rotate($angle)
400  {
401  $this->setAngle($angle);
402  $this->_getModel()->setAngle($angle);
403  $this->_scheduleRotate = true;
404  return $this;
405  }
406 
417  public function watermark($fileName, $position, $size = null, $imageOpacity = null)
418  {
419  $this->setWatermark(
420  $fileName
421  )->setWatermarkPosition(
422  $position
423  )->setWatermarkSize(
424  $size
425  )->setWatermarkImageOpacity(
426  $imageOpacity
427  );
428  return $this;
429  }
430 
437  public function placeholder($fileName)
438  {
439  $this->_placeholder = $fileName;
440  }
441 
451  public function getPlaceholder($placeholder = null)
452  {
453  if ($placeholder) {
454  $placeholderFullPath = 'Magento_Catalog::images/product/placeholder/' . $placeholder . '.jpg';
455  } else {
456  $placeholderFullPath = $this->_placeholder
457  ?: 'Magento_Catalog::images/product/placeholder/' . $this->_getModel()->getDestinationSubdir() . '.jpg';
458  }
459  return $placeholderFullPath;
460  }
461 
468  protected function applyScheduledActions()
469  {
470  $this->initBaseFile();
471  if ($this->isScheduledActionsAllowed()) {
472  $model = $this->_getModel();
473  if ($this->_scheduleRotate) {
474  $model->rotate($this->getAngle());
475  }
476  if ($this->_scheduleResize) {
477  $model->resize();
478  }
479  if ($this->getWatermark()) {
480  $model->setWatermark($this->getWatermark());
481  }
482  $model->saveFile();
483  }
484  return $this;
485  }
486 
492  protected function initBaseFile()
493  {
494  $model = $this->_getModel();
495  $baseFile = $model->getBaseFile();
496  if (!$baseFile) {
497  if ($this->getImageFile()) {
498  $model->setBaseFile($this->getImageFile());
499  } else {
500  $model->setBaseFile($this->getProduct()->getData($model->getDestinationSubdir()));
501  }
502  }
503  return $this;
504  }
505 
511  protected function isScheduledActionsAllowed()
512  {
513  $model = $this->_getModel();
514  if ($model->isBaseFilePlaceholder() || $model->isCached()) {
515  return false;
516  }
517  return true;
518  }
519 
525  public function getUrl()
526  {
527  try {
528  $this->applyScheduledActions();
529  return $this->_getModel()->getUrl();
530  } catch (\Exception $e) {
531  return $this->getDefaultPlaceholderUrl();
532  }
533  }
534 
538  public function save()
539  {
540  $this->applyScheduledActions();
541  return $this;
542  }
543 
549  public function getResizedImageInfo()
550  {
551  $this->applyScheduledActions();
552  return $this->_getModel()->getResizedImageInfo();
553  }
554 
559  public function getDefaultPlaceholderUrl($placeholder = null)
560  {
561  try {
562  $imageAsset = $this->viewAssetPlaceholderFactory->create(
563  [
564  'type' => $placeholder ?: $this->_getModel()->getDestinationSubdir(),
565  ]
566  );
567  $url = $imageAsset->getUrl();
568  } catch (\Exception $e) {
569  $this->_logger->critical($e);
570  $url = $this->_urlBuilder->getUrl('', ['_direct' => 'core/index/notFound']);
571  }
572  return $url;
573  }
574 
580  protected function _getModel()
581  {
582  if (!$this->_model) {
583  $this->_model = $this->_productImageFactory->create();
584  }
585  return $this->_model;
586  }
587 
594  protected function setAngle($angle)
595  {
596  $this->_angle = $angle;
597  return $this;
598  }
599 
605  protected function getAngle()
606  {
607  return $this->_angle;
608  }
609 
616  protected function setWatermark($watermark)
617  {
618  $this->_watermark = $watermark;
619  $this->_getModel()->setWatermarkFile($watermark);
620  return $this;
621  }
622 
628  protected function getWatermark()
629  {
630  return $this->_watermark;
631  }
632 
639  protected function setWatermarkPosition($position)
640  {
641  $this->_watermarkPosition = $position;
642  $this->_getModel()->setWatermarkPosition($position);
643  return $this;
644  }
645 
651  protected function getWatermarkPosition()
652  {
654  }
655 
663  public function setWatermarkSize($size)
664  {
665  $this->_watermarkSize = $size;
666  $this->_getModel()->setWatermarkSize($this->parseSize($size));
667  return $this;
668  }
669 
675  protected function getWatermarkSize()
676  {
677  return $this->_watermarkSize;
678  }
679 
686  public function setWatermarkImageOpacity($imageOpacity)
687  {
688  $this->_watermarkImageOpacity = $imageOpacity;
689  $this->_getModel()->setWatermarkImageOpacity($imageOpacity);
690  return $this;
691  }
692 
698  protected function getWatermarkImageOpacity()
699  {
700  if ($this->_watermarkImageOpacity) {
702  }
703 
704  return $this->_getModel()->getWatermarkImageOpacity();
705  }
706 
713  protected function setProduct($product)
714  {
715  $this->_product = $product;
716  return $this;
717  }
718 
724  protected function getProduct()
725  {
726  return $this->_product;
727  }
728 
735  public function setImageFile($file)
736  {
737  $this->_imageFile = $file;
738  return $this;
739  }
740 
746  protected function getImageFile()
747  {
748  return $this->_imageFile;
749  }
750 
757  protected function parseSize($string)
758  {
759  $size = explode('x', strtolower($string));
760  if (sizeof($size) == 2) {
761  return ['width' => $size[0] > 0 ? $size[0] : null, 'height' => $size[1] > 0 ? $size[1] : null];
762  }
763  return false;
764  }
765 
771  public function getOriginalWidth()
772  {
773  return $this->_getModel()->getImageProcessor()->getOriginalWidth();
774  }
775 
781  public function getOriginalHeight()
782  {
783  return $this->_getModel()->getImageProcessor()->getOriginalHeight();
784  }
785 
792  public function getOriginalSizeArray()
793  {
794  return [$this->getOriginalWidth(), $this->getOriginalHeight()];
795  }
796 
802  protected function getConfigView()
803  {
804  if (!$this->configView) {
805  $this->configView = $this->viewConfig->getViewConfig();
806  }
807  return $this->configView;
808  }
809 
815  public function getType()
816  {
817  return $this->getAttribute('type');
818  }
819 
825  public function getWidth()
826  {
827  return $this->getAttribute('width');
828  }
829 
835  public function getHeight()
836  {
837  return $this->getAttribute('height') ?: $this->getAttribute('width');
838  }
839 
845  public function getFrame()
846  {
847  $frame = $this->getAttribute('frame');
848  if ($frame === null) {
849  $frame = $this->getConfigView()->getVarValue('Magento_Catalog', 'product_image_white_borders');
850  }
851  return (bool)$frame;
852  }
853 
860  protected function getAttribute($name)
861  {
862  return $this->attributes[$name] ?? null;
863  }
864 
870  public function getLabel()
871  {
872  $label = $this->_product->getData($this->getType() . '_' . 'label');
873  if (empty($label)) {
874  $label = $this->_product->getName();
875  }
876  return $label;
877  }
878 }
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\Catalog\Model\Product\ImageFactory $productImageFactory, \Magento\Framework\View\Asset\Repository $assetRepo, \Magento\Framework\View\ConfigInterface $viewConfig, \Magento\Catalog\Model\View\Asset\PlaceholderFactory $placeholderFactory=null)
Definition: Image.php:142
setWatermark($watermark)
Definition: Image.php:616
setWatermarkImageOpacity($imageOpacity)
Definition: Image.php:686
$fileName
Definition: translate.phtml:15
$label
Definition: details.phtml:21
resize($width, $height=null)
Definition: Image.php:289
init($product, $imageId, $attributes=[])
Definition: Image.php:186
setWatermarkPosition($position)
Definition: Image.php:639
getDefaultPlaceholderUrl($placeholder=null)
Definition: Image.php:559
getPlaceholder($placeholder=null)
Definition: Image.php:451
watermark($fileName, $position, $size=null, $imageOpacity=null)
Definition: Image.php:417
backgroundColor($colorRGB)
Definition: Image.php:383
if(!isset($_GET['name'])) $name
Definition: log.php:14