0

I tried to execute the following script

$_product->addImageToMediaGallery($returnUploadedImagePath, array('image','thumbnail','small_image'), false);  

I am getting vendor error , is there a script to upload images to avoid the error

Fatal error: Uncaught Magento\Framework\Exception\LocalizedException: The image does not exist. /vendor/magento/module-catalog/Model/Product/Gallery/Processor.php on line 131

0

The method below looks for images in the folder “/pub/media/import/” and, if found, they get imported for the product.

protected function importImages(\Magento\Catalog\Model\Product $product)
{
    $imageName = $product->getSku();
    $imageFolder = strtolower(explode('_', $imageName)[0]) . "/";

    $dir = $this->_objectManager->get('\Magento\Framework\App\Filesystem\DirectoryList');
    $mediaImportFolder = $dir->getPath(DirectoryList::MEDIA);

    if($this->isSetFlag('remove_existing_images')) {
        // remove images
        $existingMediaGalleryEntries = $product->getMediaGalleryEntries();
        foreach ($existingMediaGalleryEntries as $key => $entry) {
            unset($existingMediaGalleryEntries[$key]);
        }
        $product->setMediaGalleryEntries($existingMediaGalleryEntries);
        $this->_productRepositoryInterface->save($product);
    }

    $product->setMediaGallery(array('images'=>array (), 'values'=>array ()));

    $i = $foundImages = 0;
    while($i < 10) {
        $i++;

        $imagePath = 'import/' . $imageFolder . $imageName . "_IMAGE" . $i . ".jpg";
        $imageAbsolutePath = $mediaImportFolder . '/' . $imagePath;

        if(!$imageAbsolutePath = $this->fileExists($imageAbsolutePath)) {
            continue 1;
        }

        $flags = ($foundImages == 0) ? ['image','thumbnail','small_image'] : [];

        $product->addImageToMediaGallery($imageAbsolutePath, $flags, false, false);

        $this->log(" - Image {$imagePath} imported with success for the product SKU {$product->getSku()}");
        $foundImages++;
    }

    if($foundImages == 0) {
        $this->log(" - No images found for the product SKU {$product->getSku()}");
    }

    return $this;
}
| improve this answer | |

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.