Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractAdapter.php
Go to the documentation of this file.
1 <?php
7 
9 
15 abstract class AbstractAdapter implements AdapterInterface
16 {
22 
26  const POSITION_TOP_LEFT = 'top-left';
27 
28  const POSITION_TOP_RIGHT = 'top-right';
29 
30  const POSITION_BOTTOM_LEFT = 'bottom-left';
31 
32  const POSITION_BOTTOM_RIGHT = 'bottom-right';
33 
34  const POSITION_STRETCH = 'stretch';
35 
36  const POSITION_TILE = 'tile';
37 
38  const POSITION_CENTER = 'center';
39 
43  const DEFAULT_FONT_SIZE = 15;
44 
48  protected $_fileType;
49 
53  protected $_fileName;
54 
58  protected $_fileMimeType;
59 
63  protected $_fileSrcName;
64 
68  protected $_fileSrcPath;
69 
73  protected $_imageHandler;
74 
78  protected $_imageSrcWidth;
79 
83  protected $_imageSrcHeight;
84 
89 
94 
98  protected $_watermarkWidth;
99 
103  protected $_watermarkHeight;
104 
109 
113  protected $_quality;
114 
119 
123  protected $_keepAspectRatio;
124 
128  protected $_keepFrame;
129 
134 
138  protected $_backgroundColor;
139 
143  protected $_constrainOnly;
144 
150  protected $_filesystem;
151 
155  protected $directoryWrite;
156 
160  protected $logger;
161 
168  abstract public function open($fileName);
169 
179  abstract public function save($destination = null, $newName = null);
180 
186  abstract public function getImage();
187 
195  abstract public function resize($width = null, $height = null);
196 
203  abstract public function rotate($angle);
204 
214  abstract public function crop($top = 0, $left = 0, $right = 0, $bottom = 0);
215 
226  abstract public function watermark($imagePath, $positionX = 0, $positionY = 0, $opacity = 30, $tile = false);
227 
234  abstract public function checkDependencies();
235 
243  abstract public function createPngFromString($text, $font = '');
244 
250  abstract public function refreshImageDimensions();
251 
259  abstract public function getColorAt($x, $y);
260 
269  public function __construct(
270  \Magento\Framework\Filesystem $filesystem,
271  \Psr\Log\LoggerInterface $logger,
272  array $data = []
273  ) {
274  $this->_filesystem = $filesystem;
275  $this->logger = $logger;
276  $this->directoryWrite = $this->_filesystem->getDirectoryWrite(DirectoryList::ROOT);
277  }
278 
284  public function getMimeType()
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  }
293 
299  public function getImageType()
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  }
311 
317  public function getOriginalWidth()
318  {
319  $this->getImageType();
320  return $this->_imageSrcWidth;
321  }
322 
328  public function getOriginalHeight()
329  {
330  $this->getImageType();
331  return $this->_imageSrcHeight;
332  }
333 
340  public function setWatermarkPosition($position)
341  {
342  $this->_watermarkPosition = $position;
343  return $this;
344  }
345 
351  public function getWatermarkPosition()
352  {
354  }
355 
362  public function setWatermarkImageOpacity($imageOpacity)
363  {
364  $this->_watermarkImageOpacity = $imageOpacity;
365  return $this;
366  }
367 
373  public function getWatermarkImageOpacity()
374  {
376  }
377 
384  public function setWatermarkWidth($width)
385  {
386  $this->_watermarkWidth = $width;
387  return $this;
388  }
389 
395  public function getWatermarkWidth()
396  {
397  return $this->_watermarkWidth;
398  }
399 
406  public function setWatermarkHeight($height)
407  {
408  $this->_watermarkHeight = $height;
409  return $this;
410  }
411 
417  public function getWatermarkHeight()
418  {
420  }
421 
428  public function keepAspectRatio($value = null)
429  {
430  if (null !== $value) {
431  $this->_keepAspectRatio = (bool)$value;
432  }
434  }
435 
442  public function keepFrame($value = null)
443  {
444  if (null !== $value) {
445  $this->_keepFrame = (bool)$value;
446  }
447  return $this->_keepFrame;
448  }
449 
456  public function keepTransparency($value = null)
457  {
458  if (null !== $value) {
459  $this->_keepTransparency = (bool)$value;
460  }
462  }
463 
470  public function constrainOnly($value = null)
471  {
472  if (null !== $value) {
473  $this->_constrainOnly = (bool)$value;
474  }
475  return $this->_constrainOnly;
476  }
477 
484  public function quality($value = null)
485  {
486  if (null !== $value) {
487  $this->_quality = (int)$value;
488  }
489  return $this->_quality;
490  }
491 
498  public function backgroundColor($value = null)
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  }
513 
519  protected function _getFileAttributes()
520  {
521  $pathinfo = pathinfo($this->_fileName);
522 
523  $this->_fileSrcPath = $pathinfo['dirname'];
524  $this->_fileSrcName = $pathinfo['basename'];
525  }
526 
535  protected function _adaptResizeValues($frameWidth, $frameHeight)
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  }
579 
587  protected function _checkAspectRatio($frameWidth, $frameHeight)
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  }
608 
617  protected function _checkDimensions($frameWidth, $frameHeight)
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  }
626 
632  protected function _checkSrcDimensions()
633  {
634  return !empty($this->_imageSrcWidth) && !empty($this->_imageSrcHeight);
635  }
636 
643  protected function _getImageOptions($filePath)
644  {
645  return getimagesize($filePath);
646  }
647 
653  public function getSupportedFormats()
654  {
655  return ['gif', 'jpeg', 'jpg', 'png'];
656  }
657 
666  protected function _prepareDestination($destination = null, $newName = null)
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  }
696 
702  protected function _canProcess()
703  {
704  return !empty($this->_fileName);
705  }
706 
714  public function validateUploadFile($filePath)
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  }
727 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
save($destination=null, $newName=null)
watermark($imagePath, $positionX=0, $positionY=0, $opacity=30, $tile=false)
endifif( $block->getLastPageNum()>1)( 'Page') ?></strong >< ul class $text
Definition: pager.phtml:43
$fileName
Definition: translate.phtml:15
$value
Definition: gender.phtml:16
is_writable($path)
Definition: io.php:25
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
__construct(\Magento\Framework\Filesystem $filesystem, \Psr\Log\LoggerInterface $logger, array $data=[])
$filesystem
_prepareDestination($destination=null, $newName=null)
crop($top=0, $left=0, $right=0, $bottom=0)