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 
13 use Magento\Framework\Api\Data\ImageContentInterfaceFactory;
17 
21 class Image extends File
22 {
26  private $imageContentFactory;
27 
46  public function __construct(
47  \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
50  \Magento\Framework\Locale\ResolverInterface $localeResolver,
51  $value,
53  $isAjax,
55  \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $fileValidator,
56  Filesystem $fileSystem,
57  UploaderFactory $uploaderFactory,
58  \Magento\Customer\Model\FileProcessorFactory $fileProcessorFactory = null,
59  \Magento\Framework\Api\Data\ImageContentInterfaceFactory $imageContentInterfaceFactory = null
60  ) {
61  parent::__construct(
62  $localeDate,
63  $logger,
64  $attribute,
65  $localeResolver,
66  $value,
68  $isAjax,
70  $fileValidator,
71  $fileSystem,
72  $uploaderFactory,
74  );
75  $this->imageContentFactory = $imageContentInterfaceFactory ?: ObjectManager::getInstance()
76  ->get(\Magento\Framework\Api\Data\ImageContentInterfaceFactory::class);
77  }
78 
88  protected function _validateByRules($value)
89  {
90  $label = $value['name'];
91  $rules = $this->getAttribute()->getValidationRules();
92 
93  $imageProp = @getimagesize($value['tmp_name']);
94 
95  if (!$this->_isUploadedFile($value['tmp_name']) || !$imageProp) {
96  return [__('"%1" is not a valid file.', $label)];
97  }
98 
99  $allowImageTypes = [1 => 'gif', 2 => 'jpg', 3 => 'png'];
100 
101  if (!isset($allowImageTypes[$imageProp[2]])) {
102  return [__('"%1" is not a valid image format.', $label)];
103  }
104 
105  // modify image name
106  $extension = pathinfo($value['name'], PATHINFO_EXTENSION);
107  if ($extension != $allowImageTypes[$imageProp[2]]) {
108  $value['name'] = pathinfo($value['name'], PATHINFO_FILENAME) . '.' . $allowImageTypes[$imageProp[2]];
109  }
110 
111  $maxFileSize = ArrayObjectSearch::getArrayElementByName(
112  $rules,
113  'max_file_size'
114  );
115  $errors = [];
116  if ($maxFileSize !== null) {
117  $size = $value['size'];
118  if ($maxFileSize < $size) {
119  $errors[] = __('"%1" exceeds the allowed file size.', $label);
120  }
121  }
122 
123  $maxImageWidth = ArrayObjectSearch::getArrayElementByName(
124  $rules,
125  'max_image_width'
126  );
127  if ($maxImageWidth !== null) {
128  if ($maxImageWidth < $imageProp[0]) {
129  $r = $maxImageWidth;
130  $errors[] = __('"%1" width exceeds allowed value of %2 px.', $label, $r);
131  }
132  }
133 
134  $maxImageHeight = ArrayObjectSearch::getArrayElementByName(
135  $rules,
136  'max_image_heght'
137  );
138  if ($maxImageHeight !== null) {
139  if ($maxImageHeight < $imageProp[1]) {
140  $r = $maxImageHeight;
141  $errors[] = __('"%1" height exceeds allowed value of %2 px.', $label, $r);
142  }
143  }
144 
145  return $errors;
146  }
147 
154  protected function processUiComponentValue(array $value)
155  {
156  if ($this->_entityTypeCode == AddressMetadataInterface::ENTITY_TYPE_ADDRESS) {
157  $result = $this->processCustomerAddressValue($value);
158  return $result;
159  }
160 
161  if ($this->_entityTypeCode == CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER) {
162  $result = $this->processCustomerValue($value);
163  return $result;
164  }
165 
166  return $this->_value;
167  }
168 
175  protected function processCustomerAddressValue(array $value)
176  {
177  $result = $this->getFileProcessor()->moveTemporaryFile($value['file']);
178  return $result;
179  }
180 
187  protected function processCustomerValue(array $value)
188  {
189  $temporaryFile = FileProcessor::TMP_DIR . '/' . ltrim($value['file'], '/');
190 
191  if ($this->getFileProcessor()->isExist($temporaryFile)) {
192  $base64EncodedData = $this->getFileProcessor()->getBase64EncodedData($temporaryFile);
193 
195  $imageContentDataObject = $this->imageContentFactory->create()
196  ->setName($value['name'])
197  ->setBase64EncodedData($base64EncodedData)
198  ->setType($value['type']);
199 
200  // Remove temporary file
201  $this->getFileProcessor()->removeUploadedFile($temporaryFile);
202 
203  return $imageContentDataObject;
204  }
205 
206  return $this->_value;
207  }
208 }
__()
Definition: __.php:13
$logger
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
__construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Psr\Log\LoggerInterface $logger, \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute, \Magento\Framework\Locale\ResolverInterface $localeResolver, $value, $entityTypeCode, $isAjax, \Magento\Framework\Url\EncoderInterface $urlEncoder, \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $fileValidator, Filesystem $fileSystem, UploaderFactory $uploaderFactory, \Magento\Customer\Model\FileProcessorFactory $fileProcessorFactory=null, \Magento\Framework\Api\Data\ImageContentInterfaceFactory $imageContentInterfaceFactory=null)
Definition: Image.php:46
$errors
Definition: overview.phtml:9