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
7 
9 
16 class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
17 {
23  protected $_uploaderFactory;
24 
30  protected $_filesystem;
31 
38 
44  protected $_logger;
45 
49  private $imageUploader;
50 
54  private $additionalData = '_additional_data_';
55 
61  public function __construct(
62  \Psr\Log\LoggerInterface $logger,
63  \Magento\Framework\Filesystem $filesystem,
64  \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
65  ) {
66  $this->_filesystem = $filesystem;
67  $this->_fileUploaderFactory = $fileUploaderFactory;
68  $this->_logger = $logger;
69  }
70 
78  private function getUploadedImageName($value)
79  {
80  if (is_array($value) && isset($value[0]['name'])) {
81  return $value[0]['name'];
82  }
83 
84  return '';
85  }
86 
95  public function beforeSave($object)
96  {
97  $attributeName = $this->getAttribute()->getName();
98  $value = $object->getData($attributeName);
99 
100  if ($this->fileResidesOutsideCategoryDir($value)) {
101  // use relative path for image attribute so we know it's outside of category dir when we fetch it
102  $value[0]['name'] = $value[0]['url'];
103  }
104 
105  if ($imageName = $this->getUploadedImageName($value)) {
106  $object->setData($this->additionalData . $attributeName, $value);
107  $object->setData($attributeName, $imageName);
108  } elseif (!is_string($value)) {
109  $object->setData($attributeName, null);
110  }
111 
112  return parent::beforeSave($object);
113  }
114 
120  private function getImageUploader()
121  {
122  if ($this->imageUploader === null) {
124  ->get(\Magento\Catalog\CategoryImageUpload::class);
125  }
126 
127  return $this->imageUploader;
128  }
129 
136  private function isTmpFileAvailable($value)
137  {
138  return is_array($value) && isset($value[0]['tmp_name']);
139  }
140 
147  private function fileResidesOutsideCategoryDir($value)
148  {
149  if (!is_array($value) || !isset($value[0]['url'])) {
150  return false;
151  }
152 
153  $fileUrl = ltrim($value[0]['url'], '/');
154  $baseMediaDir = $this->_filesystem->getUri(DirectoryList::MEDIA);
155 
156  $usingPathRelativeToBase = strpos($fileUrl, $baseMediaDir) === 0;
157 
158  return $usingPathRelativeToBase;
159  }
160 
167  public function afterSave($object)
168  {
169  $value = $object->getData($this->additionalData . $this->getAttribute()->getName());
170 
171  if ($this->isTmpFileAvailable($value) && $imageName = $this->getUploadedImageName($value)) {
172  try {
173  $this->getImageUploader()->moveFileFromTmp($imageName);
174  } catch (\Exception $e) {
175  $this->_logger->critical($e);
176  }
177  }
178 
179  return $this;
180  }
181 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$logger
__construct(\Psr\Log\LoggerInterface $logger, \Magento\Framework\Filesystem $filesystem, \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory)
Definition: Image.php:61
$value
Definition: gender.phtml:16
$filesystem