Example #1
0
 /**
  * @param ProductInterface $product
  * @param array $newEntry
  * @return $this
  * @throws InputException
  * @throws StateException
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function processNewMediaGalleryEntry(ProductInterface $product, array $newEntry)
 {
     /** @var ImageContentInterface $contentDataObject */
     $contentDataObject = $newEntry['content'];
     /** @var \Magento\Catalog\Model\Product\Media\Config $mediaConfig */
     $mediaConfig = $product->getMediaConfig();
     $mediaTmpPath = $mediaConfig->getBaseTmpMediaPath();
     $relativeFilePath = $this->imageProcessor->processImageContent($mediaTmpPath, $contentDataObject);
     $tmpFilePath = $mediaConfig->getTmpMediaShortUrl($relativeFilePath);
     /** @var \Magento\Catalog\Model\Product\Attribute\Backend\Media $galleryAttributeBackend */
     $galleryAttributeBackend = $product->getGalleryAttributeBackend();
     if ($galleryAttributeBackend == null) {
         throw new StateException(__('Requested product does not support images.'));
     }
     $imageFileUri = $galleryAttributeBackend->addImage($product, $tmpFilePath, isset($newEntry['types']) ? $newEntry['types'] : [], true, isset($newEntry['disabled']) ? $newEntry['disabled'] : true);
     // Update additional fields that are still empty after addImage call
     $galleryAttributeBackend->updateImage($product, $imageFileUri, ['label' => $newEntry['label'], 'position' => $newEntry['position'], 'disabled' => $newEntry['disabled'], 'media_type' => $newEntry['media_type']]);
     return $this;
 }
Example #2
0
 /**
  * @param ProductInterface $product
  * @param array $newEntry
  * @return $this
  * @throws InputException
  * @throws StateException
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function processNewMediaGalleryEntry(ProductInterface $product, array $newEntry)
 {
     /** @var ProductAttributeMediaGalleryEntryContentInterface $contentDataObject */
     $contentDataObject = $newEntry['content'];
     if (!$this->contentValidator->isValid($contentDataObject)) {
         throw new InputException(__('The image content is not valid.'));
     }
     $fileContent = @base64_decode($contentDataObject->getEntryData(), true);
     $fileName = $contentDataObject->getName();
     $mimeType = $contentDataObject->getMimeType();
     /** @var \Magento\Catalog\Model\Product\Media\Config $mediaConfig */
     $mediaConfig = $product->getMediaConfig();
     $mediaTmpPath = $mediaConfig->getBaseTmpMediaPath();
     $mediaDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
     $mediaDirectory->create($mediaTmpPath);
     $fileName = $fileName . '.' . $this->mimeTypeExtensionMap->getMimeTypeExtension($mimeType);
     $relativeFilePath = $mediaTmpPath . DIRECTORY_SEPARATOR . $fileName;
     $absoluteFilePath = $mediaDirectory->getAbsolutePath($relativeFilePath);
     $mediaDirectory->writeFile($relativeFilePath, $fileContent);
     /** @var \Magento\Catalog\Model\Product\Attribute\Backend\Media $galleryAttributeBackend */
     $galleryAttributeBackend = $product->getGalleryAttributeBackend();
     if ($galleryAttributeBackend == null) {
         throw new StateException(__('Requested product does not support images.'));
     }
     $imageFileUri = $galleryAttributeBackend->addImage($product, $absoluteFilePath, isset($newEntry['types']) ? $newEntry['types'] : [], true, isset($newEntry['disabled']) ? $newEntry['disabled'] : true);
     // Update additional fields that are still empty after addImage call
     $galleryAttributeBackend->updateImage($product, $imageFileUri, ['label' => $newEntry['label'], 'position' => $newEntry['position'], 'disabled' => $newEntry['disabled']]);
     return $this;
 }