public function testAddImage()
 {
     $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product');
     $product->setId(1);
     $file = $this->_model->addImage($product, self::$_mediaTmpDir . '/magento_small_image.jpg');
     $this->assertStringMatchesFormat('/m/a/magento_small_image%sjpg', $file);
 }
Exemplo n.º 2
0
 /**
  * Add image to media gallery
  *
  * @param string        $file              file path of image in file system
  * @param string|array  $mediaAttribute    code of attribute with type 'media_image',
  *                                          leave blank if image should be only in gallery
  * @param boolean       $move              if true, it will move source file
  * @param boolean       $exclude           mark image as disabled in product page view
  * @return \Magento\Catalog\Model\Product
  */
 public function addImageToMediaGallery($file, $mediaAttribute = null, $move = false, $exclude = true)
 {
     if ($this->hasGalleryAttribute()) {
         $this->mediaGalleryProcessor->addImage($this, $file, $mediaAttribute, $move, $exclude);
     }
     return $this;
 }
 /**
  * @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);
     if (!$product->hasGalleryAttribute()) {
         throw new StateException(__('Requested product does not support images.'));
     }
     $imageFileUri = $this->mediaGalleryProcessor->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
     $this->mediaGalleryProcessor->updateImage($product, $imageFileUri, ['label' => $newEntry['label'], 'position' => $newEntry['position'], 'disabled' => $newEntry['disabled'], 'media_type' => $newEntry['media_type']]);
     return $this;
 }