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

Public Member Functions

 open ($fileName)
 
 save ($destination=null, $newName=null)
 
 getImage ()
 
 resize ($width=null, $height=null)
 
 rotate ($angle)
 
 crop ($top=0, $left=0, $right=0, $bottom=0)
 
 watermark ($imagePath, $positionX=0, $positionY=0, $opacity=30, $tile=false)
 
 checkDependencies ()
 
 createPngFromString ($text, $font='')
 
 refreshImageDimensions ()
 
 getColorAt ($x, $y)
 
 __construct (\Magento\Framework\Filesystem $filesystem, \Psr\Log\LoggerInterface $logger, array $data=[])
 
 getMimeType ()
 
 getImageType ()
 
 getOriginalWidth ()
 
 getOriginalHeight ()
 
 setWatermarkPosition ($position)
 
 getWatermarkPosition ()
 
 setWatermarkImageOpacity ($imageOpacity)
 
 getWatermarkImageOpacity ()
 
 setWatermarkWidth ($width)
 
 getWatermarkWidth ()
 
 setWatermarkHeight ($height)
 
 getWatermarkHeight ()
 
 keepAspectRatio ($value=null)
 
 keepFrame ($value=null)
 
 keepTransparency ($value=null)
 
 constrainOnly ($value=null)
 
 quality ($value=null)
 
 backgroundColor ($value=null)
 
 getSupportedFormats ()
 
 validateUploadFile ($filePath)
 

Data Fields

 $imageBackgroundColor = 0
 
const POSITION_TOP_LEFT = 'top-left'
 
const POSITION_TOP_RIGHT = 'top-right'
 
const POSITION_BOTTOM_LEFT = 'bottom-left'
 
const POSITION_BOTTOM_RIGHT = 'bottom-right'
 
const POSITION_STRETCH = 'stretch'
 
const POSITION_TILE = 'tile'
 
const POSITION_CENTER = 'center'
 
const DEFAULT_FONT_SIZE = 15
 
- Data Fields inherited from AdapterInterface
const ADAPTER_GD2 = 'GD2'
 
const ADAPTER_IM = 'IMAGEMAGICK'
 

Protected Member Functions

 _getFileAttributes ()
 
 _adaptResizeValues ($frameWidth, $frameHeight)
 
 _checkAspectRatio ($frameWidth, $frameHeight)
 
 _checkDimensions ($frameWidth, $frameHeight)
 
 _checkSrcDimensions ()
 
 _getImageOptions ($filePath)
 
 _prepareDestination ($destination=null, $newName=null)
 
 _canProcess ()
 

Protected Attributes

 $_fileType
 
 $_fileName
 
 $_fileMimeType
 
 $_fileSrcName
 
 $_fileSrcPath
 
 $_imageHandler
 
 $_imageSrcWidth
 
 $_imageSrcHeight
 
 $_requiredExtensions
 
 $_watermarkPosition
 
 $_watermarkWidth
 
 $_watermarkHeight
 
 $_watermarkImageOpacity
 
 $_quality
 
 $_fontSize = self::DEFAULT_FONT_SIZE
 
 $_keepAspectRatio
 
 $_keepFrame
 
 $_keepTransparency
 
 $_backgroundColor
 
 $_constrainOnly
 
 $_filesystem
 
 $directoryWrite
 
 $logger
 

Detailed Description

Definition at line 15 of file AbstractAdapter.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Filesystem  $filesystem,
\Psr\Log\LoggerInterface  $logger,
array  $data = [] 
)

Initialize default values

Parameters
\Magento\Framework\Filesystem$filesystem
\Psr\Log\LoggerInterface$logger
array$data@SuppressWarnings(PHPMD.UnusedFormalParameter)

Definition at line 269 of file AbstractAdapter.php.

273  {
274  $this->_filesystem = $filesystem;
275  $this->logger = $logger;
276  $this->directoryWrite = $this->_filesystem->getDirectoryWrite(DirectoryList::ROOT);
277  }
$filesystem

Member Function Documentation

◆ _adaptResizeValues()

_adaptResizeValues (   $frameWidth,
  $frameHeight 
)
protected

Adapt resize values based on image configuration

Parameters
int$frameWidth
int$frameHeight
Returns
array
Exceptions

Definition at line 535 of file AbstractAdapter.php.

536  {
537  $this->_checkDimensions($frameWidth, $frameHeight);
538 
539  // calculate lacking dimension
540  if (!$this->_keepFrame && $this->_checkSrcDimensions()) {
541  if (null === $frameWidth) {
542  $frameWidth = round($frameHeight * ($this->_imageSrcWidth / $this->_imageSrcHeight));
543  } elseif (null === $frameHeight) {
544  $frameHeight = round($frameWidth * ($this->_imageSrcHeight / $this->_imageSrcWidth));
545  }
546  } else {
547  if (null === $frameWidth) {
548  $frameWidth = $frameHeight;
549  } elseif (null === $frameHeight) {
550  $frameHeight = $frameWidth;
551  }
552  }
553 
554  // define coordinates of image inside new frame
555  $srcX = 0;
556  $srcY = 0;
557  list($dstWidth, $dstHeight) = $this->_checkAspectRatio($frameWidth, $frameHeight);
558 
559  // define position in center
560  // TODO: add positions option
561  $dstY = round(($frameHeight - $dstHeight) / 2);
562  $dstX = round(($frameWidth - $dstWidth) / 2);
563 
564  // get rid of frame (fallback to zero position coordinates)
565  if (!$this->_keepFrame) {
566  $frameWidth = $dstWidth;
567  $frameHeight = $dstHeight;
568  $dstY = 0;
569  $dstX = 0;
570  }
571 
572  return [
573  'src' => ['x' => $srcX, 'y' => $srcY],
574  'dst' => ['x' => $dstX, 'y' => $dstY, 'width' => $dstWidth, 'height' => $dstHeight],
575  // size for new image
576  'frame' => ['width' => $frameWidth, 'height' => $frameHeight]
577  ];
578  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ _canProcess()

_canProcess ( )
protected

Checks is adapter can work with image

Returns
bool

Definition at line 702 of file AbstractAdapter.php.

703  {
704  return !empty($this->_fileName);
705  }

◆ _checkAspectRatio()

_checkAspectRatio (   $frameWidth,
  $frameHeight 
)
protected

Check aspect ratio

Parameters
int$frameWidth
int$frameHeight
Returns
int[]

Definition at line 587 of file AbstractAdapter.php.

588  {
589  $dstWidth = $frameWidth;
590  $dstHeight = $frameHeight;
591  if ($this->_keepAspectRatio && $this->_checkSrcDimensions()) {
592  // do not make picture bigger, than it is, if required
593  if ($this->_constrainOnly) {
594  if ($frameWidth >= $this->_imageSrcWidth && $frameHeight >= $this->_imageSrcHeight) {
595  $dstWidth = $this->_imageSrcWidth;
596  $dstHeight = $this->_imageSrcHeight;
597  }
598  }
599  // keep aspect ratio
600  if ($this->_imageSrcWidth / $this->_imageSrcHeight >= $frameWidth / $frameHeight) {
601  $dstHeight = round($dstWidth / $this->_imageSrcWidth * $this->_imageSrcHeight);
602  } else {
603  $dstWidth = round($dstHeight / $this->_imageSrcHeight * $this->_imageSrcWidth);
604  }
605  }
606  return [$dstWidth, $dstHeight];
607  }

◆ _checkDimensions()

_checkDimensions (   $frameWidth,
  $frameHeight 
)
protected

Check Frame dimensions and throw exception if they are not valid

Parameters
int$frameWidth
int$frameHeight
Returns
void
Exceptions

Definition at line 617 of file AbstractAdapter.php.

618  {
619  if ($frameWidth !== null && $frameWidth <= 0 ||
620  $frameHeight !== null && $frameHeight <= 0 ||
621  empty($frameWidth) && empty($frameHeight)
622  ) {
623  throw new \Exception('Invalid image dimensions.');
624  }
625  }

◆ _checkSrcDimensions()

_checkSrcDimensions ( )
protected

Return false if source width or height is empty

Returns
bool

Definition at line 632 of file AbstractAdapter.php.

633  {
634  return !empty($this->_imageSrcWidth) && !empty($this->_imageSrcHeight);
635  }

◆ _getFileAttributes()

_getFileAttributes ( )
protected

Assign file dirname and basename to object properties

Returns
void

Definition at line 519 of file AbstractAdapter.php.

520  {
521  $pathinfo = pathinfo($this->_fileName);
522 
523  $this->_fileSrcPath = $pathinfo['dirname'];
524  $this->_fileSrcName = $pathinfo['basename'];
525  }

◆ _getImageOptions()

_getImageOptions (   $filePath)
protected

Return information about image using getimagesize function

Parameters
string$filePath
Returns
array

Definition at line 643 of file AbstractAdapter.php.

644  {
645  return getimagesize($filePath);
646  }

◆ _prepareDestination()

_prepareDestination (   $destination = null,
  $newName = null 
)
protected

Create destination folder if not exists and return full file path

Parameters
string$destination
string$newName
Returns
string
Exceptions

Definition at line 666 of file AbstractAdapter.php.

667  {
668  if (empty($destination)) {
669  $destination = $this->_fileSrcPath;
670  } else {
671  if (empty($newName)) {
672  $info = pathinfo($destination);
673  $newName = $info['basename'];
674  $destination = $info['dirname'];
675  }
676  }
677 
678  if (empty($newName)) {
679  $newFileName = $this->_fileSrcName;
680  } else {
681  $newFileName = $newName;
682  }
683  $fileName = $destination . '/' . $newFileName;
684 
685  if (!is_writable($destination)) {
686  try {
687  $this->directoryWrite->create($this->directoryWrite->getRelativePath($destination));
688  } catch (\Magento\Framework\Exception\FileSystemException $e) {
689  $this->logger->critical($e);
690  throw new \Exception('Unable to write file into directory ' . $destination . '. Access forbidden.');
691  }
692  }
693 
694  return $fileName;
695  }
$fileName
Definition: translate.phtml:15
is_writable($path)
Definition: io.php:25
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52

◆ backgroundColor()

backgroundColor (   $value = null)

Get/set keepBackgroundColor

Parameters
null | array$value
Returns
array|void

Definition at line 498 of file AbstractAdapter.php.

499  {
500  if (null !== $value) {
501  if (!is_array($value) || 3 !== count($value)) {
502  return;
503  }
504  foreach ($value as $color) {
505  if (!is_integer($color) || $color < 0 || $color > 255) {
506  return;
507  }
508  }
509  }
510  $this->_backgroundColor = $value;
512  }
$value
Definition: gender.phtml:16

◆ checkDependencies()

checkDependencies ( )
abstract

Checks required dependencies

Returns
void
Exceptions

Implements AdapterInterface.

◆ constrainOnly()

constrainOnly (   $value = null)

Get/set constrainOnly

Parameters
bool$value
Returns
bool

Definition at line 470 of file AbstractAdapter.php.

471  {
472  if (null !== $value) {
473  $this->_constrainOnly = (bool)$value;
474  }
475  return $this->_constrainOnly;
476  }
$value
Definition: gender.phtml:16

◆ createPngFromString()

createPngFromString (   $text,
  $font = '' 
)
abstract

Create Image from string

Parameters
string$text
string$fontPath to font file
Returns
AbstractAdapter

Implements AdapterInterface.

◆ crop()

crop (   $top = 0,
  $left = 0,
  $right = 0,
  $bottom = 0 
)
abstract

Crop image

Parameters
int$top
int$left
int$right
int$bottom
Returns
bool

Implements AdapterInterface.

◆ getColorAt()

getColorAt (   $x,
  $y 
)
abstract

Returns rgba array of the specified pixel

Parameters
int$x
int$y
Returns
array

Implements AdapterInterface.

◆ getImage()

getImage ( )
abstract

Render image and return its binary contents

Returns
string

Implements AdapterInterface.

◆ getImageType()

getImageType ( )

Assign image width, height, fileType to object properties using getimagesize function

Returns
int|null

Definition at line 299 of file AbstractAdapter.php.

300  {
301  if ($this->_fileType) {
302  return $this->_fileType;
303  } else {
304  if ($this->_canProcess()) {
305  list($this->_imageSrcWidth, $this->_imageSrcHeight, $this->_fileType) = getimagesize($this->_fileName);
306  return $this->_fileType;
307  }
308  }
309  return null;
310  }

◆ getMimeType()

getMimeType ( )

Assign image width, height, fileMimeType to object properties

Returns
string|null

Definition at line 284 of file AbstractAdapter.php.

285  {
286  if ($this->_fileMimeType) {
287  return $this->_fileMimeType;
288  } else {
289  $this->_fileMimeType = image_type_to_mime_type($this->getImageType());
290  return $this->_fileMimeType;
291  }
292  }

◆ getOriginalHeight()

getOriginalHeight ( )

Retrieve Original Image Height

Returns
int|null

Definition at line 328 of file AbstractAdapter.php.

◆ getOriginalWidth()

getOriginalWidth ( )

Retrieve Original Image Width

Returns
int|null

Definition at line 317 of file AbstractAdapter.php.

◆ getSupportedFormats()

getSupportedFormats ( )

Return supported image formats

Returns
string[]

Definition at line 653 of file AbstractAdapter.php.

654  {
655  return ['gif', 'jpeg', 'jpg', 'png'];
656  }

◆ getWatermarkHeight()

getWatermarkHeight ( )

Return watermark height

Returns
int

Definition at line 417 of file AbstractAdapter.php.

◆ getWatermarkImageOpacity()

getWatermarkImageOpacity ( )

Get watermark opacity

Returns
int

Definition at line 373 of file AbstractAdapter.php.

◆ getWatermarkPosition()

getWatermarkPosition ( )

Get watermark position

Returns
string

Definition at line 351 of file AbstractAdapter.php.

◆ getWatermarkWidth()

getWatermarkWidth ( )

Get watermark width

Returns
int

Definition at line 395 of file AbstractAdapter.php.

◆ keepAspectRatio()

keepAspectRatio (   $value = null)

Get/set keepAspectRatio

Parameters
bool$value
Returns
bool|\Magento\Framework\Image\Adapter\AbstractAdapter

Definition at line 428 of file AbstractAdapter.php.

429  {
430  if (null !== $value) {
431  $this->_keepAspectRatio = (bool)$value;
432  }
434  }
$value
Definition: gender.phtml:16

◆ keepFrame()

keepFrame (   $value = null)

Get/set keepFrame

Parameters
bool$value
Returns
bool

Definition at line 442 of file AbstractAdapter.php.

443  {
444  if (null !== $value) {
445  $this->_keepFrame = (bool)$value;
446  }
447  return $this->_keepFrame;
448  }
$value
Definition: gender.phtml:16

◆ keepTransparency()

keepTransparency (   $value = null)

Get/set keepTransparency

Parameters
bool$value
Returns
bool

Definition at line 456 of file AbstractAdapter.php.

457  {
458  if (null !== $value) {
459  $this->_keepTransparency = (bool)$value;
460  }
462  }
$value
Definition: gender.phtml:16

◆ open()

open (   $fileName)
abstract

Open image for processing

Parameters
string$fileName
Returns
void

Implements AdapterInterface.

◆ quality()

quality (   $value = null)

Get/set quality, values in percentage from 0 to 100

Parameters
int$value
Returns
int

Definition at line 484 of file AbstractAdapter.php.

485  {
486  if (null !== $value) {
487  $this->_quality = (int)$value;
488  }
489  return $this->_quality;
490  }
$value
Definition: gender.phtml:16

◆ refreshImageDimensions()

refreshImageDimensions ( )
abstract

Reassign image dimensions

Returns
void

Implements AdapterInterface.

◆ resize()

resize (   $width = null,
  $height = null 
)
abstract

Change the image size

Parameters
null | int$width
null | int$height
Returns
void

Implements AdapterInterface.

◆ rotate()

rotate (   $angle)
abstract

Rotate image on specific angle

Parameters
int$angle
Returns
void

Implements AdapterInterface.

◆ save()

save (   $destination = null,
  $newName = null 
)
abstract

Save image to specific path. If some folders of path does not exist they will be created

Parameters
null | string$destination
null | string$newName
Returns
void
Exceptions

Implements AdapterInterface.

◆ setWatermarkHeight()

setWatermarkHeight (   $height)

Set watermark height

Parameters
int$height
Returns
$this

Definition at line 406 of file AbstractAdapter.php.

407  {
408  $this->_watermarkHeight = $height;
409  return $this;
410  }

◆ setWatermarkImageOpacity()

setWatermarkImageOpacity (   $imageOpacity)

Set watermark opacity

Parameters
int$imageOpacity
Returns
$this

Definition at line 362 of file AbstractAdapter.php.

363  {
364  $this->_watermarkImageOpacity = $imageOpacity;
365  return $this;
366  }

◆ setWatermarkPosition()

setWatermarkPosition (   $position)

Set watermark position

Parameters
string$position
Returns
$this

Definition at line 340 of file AbstractAdapter.php.

341  {
342  $this->_watermarkPosition = $position;
343  return $this;
344  }

◆ setWatermarkWidth()

setWatermarkWidth (   $width)

Set watermark width

Parameters
int$width
Returns
$this

Definition at line 384 of file AbstractAdapter.php.

385  {
386  $this->_watermarkWidth = $width;
387  return $this;
388  }

◆ validateUploadFile()

validateUploadFile (   $filePath)

Check - is this file an image

Parameters
string$filePath
Returns
bool
Exceptions

Definition at line 714 of file AbstractAdapter.php.

715  {
716  if (!file_exists($filePath)) {
717  throw new \InvalidArgumentException("File '{$filePath}' does not exists.");
718  }
719  if (!getimagesize($filePath)) {
720  throw new \InvalidArgumentException('Disallowed file type.');
721  }
722  $this->checkDependencies();
723  $this->open($filePath);
724 
725  return $this->getImageType() !== null;
726  }

◆ watermark()

watermark (   $imagePath,
  $positionX = 0,
  $positionY = 0,
  $opacity = 30,
  $tile = false 
)
abstract

Add watermark to image

Parameters
string$imagePath
int$positionX
int$positionY
int$opacity
bool$tile
Returns
void

Implements AdapterInterface.

Field Documentation

◆ $_backgroundColor

$_backgroundColor
protected

Definition at line 138 of file AbstractAdapter.php.

◆ $_constrainOnly

$_constrainOnly
protected

Definition at line 143 of file AbstractAdapter.php.

◆ $_fileMimeType

$_fileMimeType
protected

Definition at line 58 of file AbstractAdapter.php.

◆ $_fileName

$_fileName
protected

Definition at line 53 of file AbstractAdapter.php.

◆ $_fileSrcName

$_fileSrcName
protected

Definition at line 63 of file AbstractAdapter.php.

◆ $_fileSrcPath

$_fileSrcPath
protected

Definition at line 68 of file AbstractAdapter.php.

◆ $_filesystem

$_filesystem
protected

Definition at line 150 of file AbstractAdapter.php.

◆ $_fileType

$_fileType
protected

Definition at line 48 of file AbstractAdapter.php.

◆ $_fontSize

$_fontSize = self::DEFAULT_FONT_SIZE
protected

Definition at line 118 of file AbstractAdapter.php.

◆ $_imageHandler

$_imageHandler
protected

Definition at line 73 of file AbstractAdapter.php.

◆ $_imageSrcHeight

$_imageSrcHeight
protected

Definition at line 83 of file AbstractAdapter.php.

◆ $_imageSrcWidth

$_imageSrcWidth
protected

Definition at line 78 of file AbstractAdapter.php.

◆ $_keepAspectRatio

$_keepAspectRatio
protected

Definition at line 123 of file AbstractAdapter.php.

◆ $_keepFrame

$_keepFrame
protected

Definition at line 128 of file AbstractAdapter.php.

◆ $_keepTransparency

$_keepTransparency
protected

Definition at line 133 of file AbstractAdapter.php.

◆ $_quality

$_quality
protected

Definition at line 113 of file AbstractAdapter.php.

◆ $_requiredExtensions

$_requiredExtensions
protected

Definition at line 88 of file AbstractAdapter.php.

◆ $_watermarkHeight

$_watermarkHeight
protected

Definition at line 103 of file AbstractAdapter.php.

◆ $_watermarkImageOpacity

$_watermarkImageOpacity
protected

Definition at line 108 of file AbstractAdapter.php.

◆ $_watermarkPosition

$_watermarkPosition
protected

Definition at line 93 of file AbstractAdapter.php.

◆ $_watermarkWidth

$_watermarkWidth
protected

Definition at line 98 of file AbstractAdapter.php.

◆ $directoryWrite

$directoryWrite
protected

Definition at line 155 of file AbstractAdapter.php.

◆ $imageBackgroundColor

$imageBackgroundColor = 0

Definition at line 21 of file AbstractAdapter.php.

◆ $logger

$logger
protected

Definition at line 160 of file AbstractAdapter.php.

◆ DEFAULT_FONT_SIZE

const DEFAULT_FONT_SIZE = 15

Default font size

Definition at line 43 of file AbstractAdapter.php.

◆ POSITION_BOTTOM_LEFT

const POSITION_BOTTOM_LEFT = 'bottom-left'

Definition at line 30 of file AbstractAdapter.php.

◆ POSITION_BOTTOM_RIGHT

const POSITION_BOTTOM_RIGHT = 'bottom-right'

Definition at line 32 of file AbstractAdapter.php.

◆ POSITION_CENTER

const POSITION_CENTER = 'center'

Definition at line 38 of file AbstractAdapter.php.

◆ POSITION_STRETCH

const POSITION_STRETCH = 'stretch'

Definition at line 34 of file AbstractAdapter.php.

◆ POSITION_TILE

const POSITION_TILE = 'tile'

Definition at line 36 of file AbstractAdapter.php.

◆ POSITION_TOP_LEFT

const POSITION_TOP_LEFT = 'top-left'

Position constants

Definition at line 26 of file AbstractAdapter.php.

◆ POSITION_TOP_RIGHT

const POSITION_TOP_RIGHT = 'top-right'

Definition at line 28 of file AbstractAdapter.php.


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