2

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();
            }
5

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']);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.