Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ImageProcessor.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Framework\Api;
8 
14 
20 {
26  protected $mimeTypeExtensionMap = [
27  'image/jpg' => 'jpg',
28  'image/jpeg' => 'jpg',
29  'image/gif' => 'gif',
30  'image/png' => 'png',
31  ];
32 
36  private $filesystem;
37 
41  private $contentValidator;
42 
46  private $dataObjectHelper;
47 
51  protected $logger;
52 
56  private $uploader;
57 
61  private $mediaDirectory;
62 
70  public function __construct(
71  Filesystem $fileSystem,
72  ImageContentValidatorInterface $contentValidator,
73  DataObjectHelper $dataObjectHelper,
74  \Psr\Log\LoggerInterface $logger,
75  Uploader $uploader
76  ) {
77  $this->filesystem = $fileSystem;
78  $this->contentValidator = $contentValidator;
79  $this->dataObjectHelper = $dataObjectHelper;
80  $this->logger = $logger;
81  $this->uploader = $uploader;
82  $this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
83  }
84 
88  public function save(
89  CustomAttributesDataInterface $dataObjectWithCustomAttributes,
91  CustomAttributesDataInterface $previousCustomerData = null
92  ) {
93  //Get all Image related custom attributes
94  $imageDataObjects = $this->dataObjectHelper->getCustomAttributeValueByType(
95  $dataObjectWithCustomAttributes->getCustomAttributes(),
96  \Magento\Framework\Api\Data\ImageContentInterface::class
97  );
98 
99  // Return if no images to process
100  if (empty($imageDataObjects)) {
101  return $dataObjectWithCustomAttributes;
102  }
103 
104  // For every image, save it and replace it with corresponding Eav data object
106  foreach ($imageDataObjects as $imageDataObject) {
107 
109  $imageContent = $imageDataObject->getValue();
110 
111  $filename = $this->processImageContent($entityType, $imageContent);
112 
113  //Set filename from static media location into data object
114  $dataObjectWithCustomAttributes->setCustomAttribute(
115  $imageDataObject->getAttributeCode(),
116  $filename
117  );
118 
119  //Delete previously saved image if it exists
120  if ($previousCustomerData) {
121  $previousImageAttribute = $previousCustomerData->getCustomAttribute(
122  $imageDataObject->getAttributeCode()
123  );
124  if ($previousImageAttribute) {
125  $previousImagePath = $previousImageAttribute->getValue();
126  if (!empty($previousImagePath) && ($previousImagePath != $filename)) {
127  @unlink($this->mediaDirectory->getAbsolutePath() . $entityType . $previousImagePath);
128  }
129  }
130  }
131  }
132 
133  return $dataObjectWithCustomAttributes;
134  }
135 
140  {
141  if (!$this->contentValidator->isValid($imageContent)) {
142  throw new InputException(new Phrase('The image content is invalid. Verify the content and try again.'));
143  }
144 
145  $fileContent = @base64_decode($imageContent->getBase64EncodedData(), true);
146  $tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
147  $fileName = $this->getFileName($imageContent);
148  $tmpFileName = substr(md5(rand()), 0, 7) . '.' . $fileName;
149  $tmpDirectory->writeFile($tmpFileName, $fileContent);
150 
151  $fileAttributes = [
152  'tmp_name' => $tmpDirectory->getAbsolutePath() . $tmpFileName,
153  'name' => $imageContent->getName()
154  ];
155 
156  try {
157  $this->uploader->processFileAttributes($fileAttributes);
158  $this->uploader->setFilesDispersion(true);
159  $this->uploader->setFilenamesCaseSensitivity(false);
160  $this->uploader->setAllowRenameFiles(true);
161  $destinationFolder = $entityType;
162  $this->uploader->save($this->mediaDirectory->getAbsolutePath($destinationFolder), $fileName);
163  } catch (\Exception $e) {
164  $this->logger->critical($e);
165  }
166  return $this->uploader->getUploadedFileName();
167  }
168 
173  protected function getMimeTypeExtension($mimeType)
174  {
175  return $this->mimeTypeExtensionMap[$mimeType] ?? '';
176  }
177 
183  private function getFileName($imageContent)
184  {
185  $fileName = $imageContent->getName();
186  if (!pathinfo($fileName, PATHINFO_EXTENSION)) {
187  if (!$imageContent->getType() || !$this->getMimeTypeExtension($imageContent->getType())) {
188  throw new InputException(new Phrase('Cannot recognize image extension.'));
189  }
190  $fileName .= '.' . $this->getMimeTypeExtension($imageContent->getType());
191  }
192  return $fileName;
193  }
194 }
save(CustomAttributesDataInterface $dataObjectWithCustomAttributes, $entityType, CustomAttributesDataInterface $previousCustomerData=null)
$tmpDirectory
$fileName
Definition: translate.phtml:15
setCustomAttribute($attributeCode, $attributeValue)
__construct(Filesystem $fileSystem, ImageContentValidatorInterface $contentValidator, DataObjectHelper $dataObjectHelper, \Psr\Log\LoggerInterface $logger, Uploader $uploader)
processImageContent($entityType, $imageContent)