Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Image Class Reference
Inheritance diagram for Image:
AbstractHelper

Public Member Functions

 __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)
 
 init ($product, $imageId, $attributes=[])
 
 resize ($width, $height=null)
 
 setQuality ($quality)
 
 keepAspectRatio ($flag)
 
 keepFrame ($flag)
 
 keepTransparency ($flag)
 
 constrainOnly ($flag)
 
 backgroundColor ($colorRGB)
 
 rotate ($angle)
 
 watermark ($fileName, $position, $size=null, $imageOpacity=null)
 
 placeholder ($fileName)
 
 getPlaceholder ($placeholder=null)
 
 getUrl ()
 
 save ()
 
 getResizedImageInfo ()
 
 getDefaultPlaceholderUrl ($placeholder=null)
 
 setWatermarkSize ($size)
 
 setWatermarkImageOpacity ($imageOpacity)
 
 setImageFile ($file)
 
 getOriginalWidth ()
 
 getOriginalHeight ()
 
 getOriginalSizeArray ()
 
 getType ()
 
 getWidth ()
 
 getHeight ()
 
 getFrame ()
 
 getLabel ()
 
- Public Member Functions inherited from AbstractHelper
 __construct (Context $context)
 
 isModuleOutputEnabled ($moduleName=null)
 

Data Fields

const MEDIA_TYPE_CONFIG_NODE = 'images'
 

Protected Member Functions

 _reset ()
 
 setImageProperties ()
 
 setWatermarkProperties ()
 
 applyScheduledActions ()
 
 initBaseFile ()
 
 isScheduledActionsAllowed ()
 
 _getModel ()
 
 setAngle ($angle)
 
 getAngle ()
 
 setWatermark ($watermark)
 
 getWatermark ()
 
 setWatermarkPosition ($position)
 
 getWatermarkPosition ()
 
 getWatermarkSize ()
 
 getWatermarkImageOpacity ()
 
 setProduct ($product)
 
 getProduct ()
 
 getImageFile ()
 
 parseSize ($string)
 
 getConfigView ()
 
 getAttribute ($name)
 
- Protected Member Functions inherited from AbstractHelper
 _getRequest ()
 
 _getModuleName ()
 
 _getUrl ($route, $params=[])
 

Protected Attributes

 $_model
 
 $_scheduleResize = true
 
 $_scheduleRotate = false
 
 $_angle
 
 $_watermark
 
 $_watermarkPosition
 
 $_watermarkSize
 
 $_watermarkImageOpacity
 
 $_product
 
 $_imageFile
 
 $_placeholder
 
 $_assetRepo
 
 $_productImageFactory
 
 $viewConfig
 
 $configView
 
 $attributes = []
 
- Protected Attributes inherited from AbstractHelper
 $_moduleName
 
 $_request
 
 $_moduleManager
 
 $_logger
 
 $_urlBuilder
 
 $_httpHeader
 
 $_eventManager
 
 $_remoteAddress
 
 $urlEncoder
 
 $urlDecoder
 
 $scopeConfig
 
 $_cacheConfig
 

Detailed Description

Catalog image helper

@api @SuppressWarnings(PHPMD.TooManyFields)

Since
100.0.2

Definition at line 17 of file Image.php.

Constructor & Destructor Documentation

◆ __construct()

__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 
)
Parameters
\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

Definition at line 142 of file Image.php.

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  }

Member Function Documentation

◆ _getModel()

_getModel ( )
protected

Get current Image model

Returns
\Magento\Catalog\Model\Product\Image

Definition at line 580 of file Image.php.

581  {
582  if (!$this->_model) {
583  $this->_model = $this->_productImageFactory->create();
584  }
585  return $this->_model;
586  }

◆ _reset()

_reset ( )
protected

Reset all previous data

Returns
$this

Definition at line 163 of file Image.php.

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  }

◆ applyScheduledActions()

applyScheduledActions ( )
protected

Apply scheduled actions

Returns
$this
Exceptions

Definition at line 468 of file Image.php.

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  }

◆ backgroundColor()

backgroundColor (   $colorRGB)

Set color to fill image frame with. Applicable before calling resize() The keepTransparency(true) overrides this (if image has transparent color) It is white by default.

See also
\Magento\Catalog\Model\Product\Image
Parameters
array$colorRGB
Returns
$this

Definition at line 383 of file Image.php.

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  }

◆ constrainOnly()

constrainOnly (   $flag)

Guarantee, that image picture will not be bigger, than it was. Applicable before calling resize() It is false by default

Parameters
bool$flag
Returns
$this

Definition at line 367 of file Image.php.

368  {
369  $this->_getModel()->setConstrainOnly($flag);
370  return $this;
371  }

◆ getAngle()

getAngle ( )
protected

Get Rotation Angle

Returns
int

Definition at line 605 of file Image.php.

606  {
607  return $this->_angle;
608  }

◆ getAttribute()

getAttribute (   $name)
protected

Retrieve image attribute

Parameters
string$name
Returns
string

Definition at line 860 of file Image.php.

861  {
862  return $this->attributes[$name] ?? null;
863  }
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ getConfigView()

getConfigView ( )
protected

Retrieve config view

Returns
\Magento\Framework\Config\View

Definition at line 802 of file Image.php.

803  {
804  if (!$this->configView) {
805  $this->configView = $this->viewConfig->getViewConfig();
806  }
807  return $this->configView;
808  }

◆ getDefaultPlaceholderUrl()

getDefaultPlaceholderUrl (   $placeholder = null)
Parameters
null | string$placeholder
Returns
string

Definition at line 559 of file Image.php.

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  }

◆ getFrame()

getFrame ( )

Retrieve image frame flag

Returns
false|string

Definition at line 845 of file Image.php.

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  }

◆ getHeight()

getHeight ( )

Retrieve image height

Returns
string

Definition at line 835 of file Image.php.

836  {
837  return $this->getAttribute('height') ?: $this->getAttribute('width');
838  }

◆ getImageFile()

getImageFile ( )
protected

Get Image file

Returns
string

Definition at line 746 of file Image.php.

747  {
748  return $this->_imageFile;
749  }

◆ getLabel()

getLabel ( )

Return image label

Returns
string

Definition at line 870 of file Image.php.

871  {
872  $label = $this->_product->getData($this->getType() . '_' . 'label');
873  if (empty($label)) {
874  $label = $this->_product->getName();
875  }
876  return $label;
877  }
$label
Definition: details.phtml:21

◆ getOriginalHeight()

getOriginalHeight ( )

Retrieve original image height

Returns
int|null

Definition at line 781 of file Image.php.

782  {
783  return $this->_getModel()->getImageProcessor()->getOriginalHeight();
784  }

◆ getOriginalSizeArray()

getOriginalSizeArray ( )

Retrieve Original image size as array 0 - width, 1 - height

Returns
int[]

Definition at line 792 of file Image.php.

793  {
794  return [$this->getOriginalWidth(), $this->getOriginalHeight()];
795  }

◆ getOriginalWidth()

getOriginalWidth ( )

Retrieve original image width

Returns
int|null

Definition at line 771 of file Image.php.

772  {
773  return $this->_getModel()->getImageProcessor()->getOriginalWidth();
774  }

◆ getPlaceholder()

getPlaceholder (   $placeholder = null)

Get Placeholder

Parameters
null | string$placeholder
Returns
string
Deprecated:
102.0.0 Returns only default placeholder. Does not take into account custom placeholders set in Configuration.

Definition at line 451 of file Image.php.

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  }

◆ getProduct()

getProduct ( )
protected

Get current Product

Returns
\Magento\Catalog\Model\Product

Definition at line 724 of file Image.php.

725  {
726  return $this->_product;
727  }

◆ getResizedImageInfo()

getResizedImageInfo ( )

Return resized product image information

Returns
array

Definition at line 549 of file Image.php.

550  {
551  $this->applyScheduledActions();
552  return $this->_getModel()->getResizedImageInfo();
553  }

◆ getType()

getType ( )

Retrieve image type

Returns
string

Definition at line 815 of file Image.php.

816  {
817  return $this->getAttribute('type');
818  }

◆ getUrl()

getUrl ( )

Retrieve image URL

Returns
string

Definition at line 525 of file Image.php.

526  {
527  try {
528  $this->applyScheduledActions();
529  return $this->_getModel()->getUrl();
530  } catch (\Exception $e) {
531  return $this->getDefaultPlaceholderUrl();
532  }
533  }
getDefaultPlaceholderUrl($placeholder=null)
Definition: Image.php:559

◆ getWatermark()

getWatermark ( )
protected

Get watermark file name

Returns
string

Definition at line 628 of file Image.php.

629  {
630  return $this->_watermark;
631  }

◆ getWatermarkImageOpacity()

getWatermarkImageOpacity ( )
protected

Get watermark image opacity

Returns
int

Definition at line 698 of file Image.php.

699  {
700  if ($this->_watermarkImageOpacity) {
702  }
703 
704  return $this->_getModel()->getWatermarkImageOpacity();
705  }

◆ getWatermarkPosition()

getWatermarkPosition ( )
protected

Get watermark position

Returns
string

Definition at line 651 of file Image.php.

652  {
654  }

◆ getWatermarkSize()

getWatermarkSize ( )
protected

Get watermark size

Returns
string

Definition at line 675 of file Image.php.

676  {
677  return $this->_watermarkSize;
678  }

◆ getWidth()

getWidth ( )

Retrieve image width

Returns
string

Definition at line 825 of file Image.php.

826  {
827  return $this->getAttribute('width');
828  }

◆ init()

init (   $product,
  $imageId,
  $attributes = [] 
)

Initialize Helper to work with Image

Parameters
\Magento\Catalog\Model\Product$product
string$imageId
array$attributes
Returns
$this

Definition at line 186 of file Image.php.

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  }

◆ initBaseFile()

initBaseFile ( )
protected

Initialize base image file

Returns
$this

Definition at line 492 of file Image.php.

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  }

◆ isScheduledActionsAllowed()

isScheduledActionsAllowed ( )
protected

Check if scheduled actions is allowed

Returns
bool

Definition at line 511 of file Image.php.

512  {
513  $model = $this->_getModel();
514  if ($model->isBaseFilePlaceholder() || $model->isCached()) {
515  return false;
516  }
517  return true;
518  }

◆ keepAspectRatio()

keepAspectRatio (   $flag)

Guarantee, that image picture width/height will not be distorted. Applicable before calling resize() It is true by default.

See also
\Magento\Catalog\Model\Product\Image
Parameters
bool$flag
Returns
$this

Definition at line 317 of file Image.php.

318  {
319  $this->_getModel()->setKeepAspectRatio($flag);
320  return $this;
321  }

◆ keepFrame()

keepFrame (   $flag)

Guarantee, that image will have dimensions, set in $width/$height Applicable before calling resize() Not applicable, if keepAspectRatio(false)

$position - TODO, not used for now - picture position inside the frame.

See also
\Magento\Catalog\Model\Product\Image
Parameters
bool$flag
Returns
$this @SuppressWarnings(PHPMD.UnusedFormalParameter)

Definition at line 335 of file Image.php.

336  {
337  $this->_getModel()->setKeepFrame($flag);
338  return $this;
339  }

◆ keepTransparency()

keepTransparency (   $flag)

Guarantee, that image will not lose transparency if any. Applicable before calling resize() It is true by default.

$alphaOpacity - TODO, not used for now

See also
\Magento\Catalog\Model\Product\Image
Parameters
bool$flag
Returns
$this @SuppressWarnings(PHPMD.UnusedFormalParameter)

Definition at line 353 of file Image.php.

354  {
355  $this->_getModel()->setKeepTransparency($flag);
356  return $this;
357  }

◆ parseSize()

parseSize (   $string)
protected

Retrieve size from string

Parameters
string$string
Returns
array|bool

Definition at line 757 of file Image.php.

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  }

◆ placeholder()

placeholder (   $fileName)

Set placeholder

Parameters
string$fileName
Returns
void

Definition at line 437 of file Image.php.

438  {
439  $this->_placeholder = $fileName;
440  }
$fileName
Definition: translate.phtml:15

◆ resize()

resize (   $width,
  $height = null 
)

Schedule resize of the image $width or $height can be null - in this case, lacking dimension will be calculated.

See also
\Magento\Catalog\Model\Product\Image
Parameters
int$width
int$height
Returns
$this

Definition at line 289 of file Image.php.

290  {
291  $this->_getModel()->setWidth($width)->setHeight($height);
292  $this->_scheduleResize = true;
293  return $this;
294  }

◆ rotate()

rotate (   $angle)

Rotate image into specified angle

Parameters
int$angle
Returns
$this

Definition at line 399 of file Image.php.

400  {
401  $this->setAngle($angle);
402  $this->_getModel()->setAngle($angle);
403  $this->_scheduleRotate = true;
404  return $this;
405  }

◆ save()

save ( )
Returns
$this

Definition at line 538 of file Image.php.

539  {
540  $this->applyScheduledActions();
541  return $this;
542  }

◆ setAngle()

setAngle (   $angle)
protected

Set Rotation Angle

Parameters
int$angle
Returns
$this

Definition at line 594 of file Image.php.

595  {
596  $this->_angle = $angle;
597  return $this;
598  }

◆ setImageFile()

setImageFile (   $file)

Set Image file

Parameters
string$file
Returns
$this

Definition at line 735 of file Image.php.

736  {
737  $this->_imageFile = $file;
738  return $this;
739  }

◆ setImageProperties()

setImageProperties ( )
protected

Set image properties

Returns
$this

Definition at line 207 of file Image.php.

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  }

◆ setProduct()

setProduct (   $product)
protected

Set current Product

Parameters
\Magento\Catalog\Model\Product$product
Returns
$this

Definition at line 713 of file Image.php.

714  {
715  $this->_product = $product;
716  return $this;
717  }

◆ setQuality()

setQuality (   $quality)

Set image quality, values in percentage from 0 to 100

Parameters
int$quality
Returns
$this

Definition at line 302 of file Image.php.

303  {
304  $this->_getModel()->setQuality($quality);
305  return $this;
306  }

◆ setWatermark()

setWatermark (   $watermark)
protected

Set watermark file name

Parameters
string$watermark
Returns
$this

Definition at line 616 of file Image.php.

617  {
618  $this->_watermark = $watermark;
619  $this->_getModel()->setWatermarkFile($watermark);
620  return $this;
621  }

◆ setWatermarkImageOpacity()

setWatermarkImageOpacity (   $imageOpacity)

Set watermark image opacity

Parameters
int$imageOpacity
Returns
$this

Definition at line 686 of file Image.php.

687  {
688  $this->_watermarkImageOpacity = $imageOpacity;
689  $this->_getModel()->setWatermarkImageOpacity($imageOpacity);
690  return $this;
691  }

◆ setWatermarkPosition()

setWatermarkPosition (   $position)
protected

Set watermark position

Parameters
string$position
Returns
$this

Definition at line 639 of file Image.php.

640  {
641  $this->_watermarkPosition = $position;
642  $this->_getModel()->setWatermarkPosition($position);
643  return $this;
644  }

◆ setWatermarkProperties()

setWatermarkProperties ( )
protected

Set watermark properties

Returns
$this

Definition at line 251 of file Image.php.

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  }
setWatermark($watermark)
Definition: Image.php:616
setWatermarkImageOpacity($imageOpacity)
Definition: Image.php:686
setWatermarkPosition($position)
Definition: Image.php:639

◆ setWatermarkSize()

setWatermarkSize (   $size)

Set watermark size param size in format 100x200

Parameters
string$size
Returns
$this

Definition at line 663 of file Image.php.

664  {
665  $this->_watermarkSize = $size;
666  $this->_getModel()->setWatermarkSize($this->parseSize($size));
667  return $this;
668  }

◆ watermark()

watermark (   $fileName,
  $position,
  $size = null,
  $imageOpacity = null 
)

Add watermark to image size param in format 100x200

Parameters
string$fileName
string$position
string$size
int$imageOpacity
Returns
$this

Definition at line 417 of file Image.php.

418  {
419  $this->setWatermark(
420  $fileName
421  )->setWatermarkPosition(
422  $position
423  )->setWatermarkSize(
424  $size
425  )->setWatermarkImageOpacity(
426  $imageOpacity
427  );
428  return $this;
429  }
setWatermark($watermark)
Definition: Image.php:616
$fileName
Definition: translate.phtml:15

Field Documentation

◆ $_angle

$_angle
protected

Definition at line 50 of file Image.php.

◆ $_assetRepo

$_assetRepo
protected

Definition at line 104 of file Image.php.

◆ $_imageFile

$_imageFile
protected

Definition at line 92 of file Image.php.

◆ $_model

$_model
protected

Definition at line 29 of file Image.php.

◆ $_placeholder

$_placeholder
protected

Definition at line 99 of file Image.php.

◆ $_product

$_product
protected

Definition at line 85 of file Image.php.

◆ $_productImageFactory

$_productImageFactory
protected

Definition at line 111 of file Image.php.

◆ $_scheduleResize

$_scheduleResize = true
protected

Definition at line 36 of file Image.php.

◆ $_scheduleRotate

$_scheduleRotate = false
protected

Definition at line 43 of file Image.php.

◆ $_watermark

$_watermark
protected

Definition at line 57 of file Image.php.

◆ $_watermarkImageOpacity

$_watermarkImageOpacity
protected

Definition at line 78 of file Image.php.

◆ $_watermarkPosition

$_watermarkPosition
protected

Definition at line 64 of file Image.php.

◆ $_watermarkSize

$_watermarkSize
protected

Definition at line 71 of file Image.php.

◆ $attributes

$attributes = []
protected

Definition at line 128 of file Image.php.

◆ $configView

$configView
protected

Definition at line 121 of file Image.php.

◆ $viewConfig

$viewConfig
protected

Definition at line 116 of file Image.php.

◆ MEDIA_TYPE_CONFIG_NODE

const MEDIA_TYPE_CONFIG_NODE = 'images'

Media config node

Definition at line 22 of file Image.php.


The documentation for this class was generated from the following file: