7

I try this code but if the image doesn't exist in this path pub/media/catalog/product; it doesn't work. I want to add an image from any url; any ideas for that ?

 for ($i = 0; $i < count($productIds); $i++) {
 $productObj->load($productIds[$i]);
 //$productObj->addImageToMediaGallery('catalog/product/l/t/lt01.jpg', array('image', 'small_image', 'thumbnail'), false, false);
 $productObj->addImageToMediaGallery('catalog/product/'.$data['image'], array('image', 'small_image', 'thumbnail'), false, false);
 $productObj->save();
 }
Muhammad Anas
1,4673 gold badges13 silver badges33 bronze badges
asked May 8, 2017 at 8:26

1 Answer 1

13

I assume that you’ve already registered your extension like VendorName_ExtensionName and it is located in the app/code/VendorName/ExtensionName. The code of the service:

<?php
/**
 * file location:
 * app/code/VendorName/ExtensionName/Service/ImportImageService.php
 */
namespace VendorName\ExtensionName\Service;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem\Io\File;
/**
 * Class ImportImageService
 * assign images to products by image URL
 */
class ImportImageService
{
 /**
 * Directory List
 *
 * @var DirectoryList
 */
 protected $directoryList;
 /**
 * File interface
 *
 * @var File
 */
 protected $file;
 /**
 * ImportImageService constructor
 *
 * @param DirectoryList $directoryList
 * @param File $file
 */
 public function __construct(
 DirectoryList $directoryList,
 File $file
 ) {
 $this->directoryList = $directoryList;
 $this->file = $file;
 }
 /**
 * Main service executor
 *
 * @param Product $product
 * @param string $imageUrl
 * @param array $imageType
 * @param bool $visible
 *
 * @return bool
 */
 public function execute($product, $imageUrl, $visible = false, $imageType = [])
 {
 /** @var string $tmpDir */
 $tmpDir = $this->getMediaDirTmpDir();
 /** create folder if it is not exists */
 $this->file->checkAndCreateFolder($tmpDir);
 /** @var string $newFileName */
 $newFileName = $tmpDir . baseName($imageUrl);
 /** read file from URL and copy it to the new destination */
 $result = $this->file->read($imageUrl, $newFileName);
 if ($result) {
 /** add saved file to the $product gallery */
 $product->addImageToMediaGallery($newFileName, $imageType, true, $visible);
 }
 return $result;
 }
 /**
 * Media directory name for the temporary file storage
 * pub/media/tmp
 *
 * @return string
 */
 protected function getMediaDirTmpDir()
 {
 return $this->directoryList->getPath(DirectoryList::MEDIA) . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR;
 }
}

Use it like this:

...
use VendorName\ExtensionName\Service;
...
/**
* @var \VendorName\ExtensionName\Service\ImportImageService
*/
protected $importimageservice;
...
public function __construct(
\Magento\Backend\App\Action\Context $context,
\VendorName\ExtensionName\Service\ImportImageService $importimageservice
) {
$this->importimageservice = $importimageservice;
parent::__construct($context);
}
...
$imagePath = "http://test.img.png"; // path of the image
$this->importimageservice->execute($product, $imagePath, $visible = true, $imageType = ['image', 'small_image', 'thumbnail']);
Eduardo
8963 gold badges13 silver badges38 bronze badges
answered May 8, 2017 at 10:22
2
  • Why do you use the folder \Service? I couldn't find its purpose anywhere. Commented Dec 16, 2018 at 11:24
  • @Kishan Patadia How to add external image URL for category. magento.stackexchange.com/questions/279578/… Commented Jun 26, 2019 at 6:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.