1

I have run below code

use Magento\Framework\App\Bootstrap;
use Magento\Catalog\Block\Product\Image;
include("app/bootstrap.php");
    //$product_id = 4814;
    $bootstrap = Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $state = $objectManager->get('Magento\Framework\App\State');
    $state->setAreaCode('frontend');
    $simple_product = $objectManager->create('\Magento\Catalog\Model\Product');


    $storeId = '1'; //Store ID
    $product_id = 4814; 
    $productFactory = $objectManager->get('\Magento\Catalog\Model\ProductFactory');
    $product = $productFactory->create()->setStoreId($storeId)->load($product_id);

    echo $imagePath = '/var/import/bulb.jpg'; // path of the image
    //exit();
    $product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);

    $product->save();

And getting following error

Fatal error: Uncaught Magento\Framework\Exception\LocalizedException: The image doesn't exist. in C:\xampp\htdocs\magento23\vendor\magento\module-catalog\Model\Product\Gallery\Processor.php:145 Stack trace: #0 C:\xampp\htdocs\magento23\vendor\magento\module-catalog\Model\Product.php(1577): Magento\Catalog\Model\Product\Gallery\Processor->addImage(Object(Magento\Catalog\Model\Product\Interceptor), '/var/import/bul...', Array, false, false) #1 C:\xampp\htdocs\magento23\generated\code\Magento\Catalog\Model\Product\Interceptor.php(895): Magento\Catalog\Model\Product->addImageToMediaGallery('/var/import/bul...', Array, false, false) #2 C:\xampp\htdocs\magento23\image.php(22): Magento\Catalog\Model\Product\Interceptor->addImageToMediaGallery('/var/import/bul...', Array, false, false) #3 {main} thrown in C:\xampp\htdocs\magento23\vendor\magento\module-catalog\Model\Product\Gallery\Processor.php on line 145

2 Answers 2

1

Replace path

$imagePath = '/var/import/bulb.jpg';

to

$imagePath = $this->getBaseUrl() . 'var/import/bulb.jpg';
4
0

Please, Try this method

<?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';
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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