Skip to content

Notice: Undefined index: extension in vendor/magento/framework/File/Uploader.php on line 59

I am trying to import product image programmatically but I am getting Below error

Notice: Undefined index: extension in vendor/magento/framework/File/Uploader.php on line 59

Code For Import Image:

    <?php

namespace VendorDemoModel;

use MagentoCatalogApiProductRepositoryInterface;
use MagentoFrameworkAppFilesystemDirectoryList;
use MagentoFrameworkExceptionNoSuchEntityException;

/*
 * Class ProductImagesImporter
 */

class ProductImagesImporter {

    /**
     * @var DirectoryList
     */
    protected $directoryList;

    /**
     * @var MagentoCatalogApiDataProductAttributeMediaGalleryEntryInterfaceFactory
     */
    protected $attributeMediaGalleryEntryFactory;

    /**
     * @var MagentoFrameworkApiDataImageContentInterfaceFactory
     */
    protected $imageContentFactory;

    /**
     * @var ProductRepositoryInterface
     */
    protected $productRepository;

    /**
     * @var MagentoFrameworkFileMime
     */
    protected $mime;

    /**
     * @var MagentoFrameworkFilesystemIoFile
     */
    protected $io;
    protected $product;
    /**
     * ProductImagesImporter constructor.
     * @param DirectoryList $directoryList
     * @param MagentoFrameworkFilesystemIoFile $io
     * @param MagentoFrameworkFileMime $mime
     * @param MagentoCatalogApiDataProductAttributeMediaGalleryEntryInterfaceFactory $attributeMediaGalleryEntryFactory
     * @param MagentoFrameworkApiDataImageContentInterfaceFactory $imageContentFactory
     * @param ProductRepositoryInterface $productRepository
     */
    public function __construct(
    DirectoryList $directoryList, MagentoFrameworkFilesystemIoFile $io, MagentoFrameworkFileMime $mime, MagentoCatalogApiDataProductAttributeMediaGalleryEntryInterfaceFactory $attributeMediaGalleryEntryFactory, MagentoFrameworkApiDataImageContentInterfaceFactory $imageContentFactory, ProductRepositoryInterface $productRepository,MagentoCatalogModelProductFactory $product
    ) {
        $this->directoryList = $directoryList;
        $this->mime = $mime;
        $this->io = $io;
        $this->attributeMediaGalleryEntryFactory = $attributeMediaGalleryEntryFactory;
        $this->imageContentFactory = $imageContentFactory;
        $this->productRepository = $productRepository;
        $this->product=$product;
    }

    /**
     * @return array
     */
    public static function getRequiredFields() {
        return ['sku', 'image'];
    }

    /**
     * @param array $item
     */
    public function importSingle(array $item,$id) {
        if ($content = $this->getContentObject($item['image'])) {
            try {
                //$product = $this->productRepository->get($item['sku']);
                $product=$this->product->create()->load($id);
                $entries = $product->getMediaGalleryEntries();
                foreach ($entries as $entry) {
                    if (basename($entry->getFile()) === basename($content->getName())) {
                        break;
                    }
                }
                if (!isset($entry)) {
                    $entry = $this->attributeMediaGalleryEntryFactory->create();
                }
                $types= array('thumbnail', 'image', 'small_image','base_image');
                $entry->setContent($content);
                $entry->setMediaType('image');
                $entry->setTypes($types);
                $entry->setDisabled(false);
                $entries[] = $entry;
                $product->setMediaGalleryEntries($entries);
                $product->save();
                //$this->productRepository->save($product);
                return true;
            } catch (MagentoFrameworkExceptionLocalizedException $ex) {
                $this->productImagelog($ex->getMessage());
                var_dump($ex->getMessage());exit;
                return false;
            } catch (MagentoFrameworkExceptionRuntimeException $ex) {
                $this->productImagelog($ex->getMessage());
                var_dump($ex->getMessage());exit;
                return false;
            }catch (Exception $ex){
                $this->productImagelog($ex->getMessage());
                var_dump($ex->getMessage());exit;
                return false;
            }
        }

    }

    /**
     * @param string $fileName
     * @return MagentoFrameworkApiDataImageContentInterface|null
     */
    protected function getContentObject($image) {
        try{
            $srcPath = $image[0];
            $image_name = $image[1];
            if ($this->io->fileExists($srcPath)) {
                $content = $this->imageContentFactory->create();
                $content->setName(strtolower($image_name));
                $content->setBase64EncodedData(base64_encode(file_get_contents($srcPath)));
                $content->setType($this->mime->getMimeType($srcPath));
                return $content;
            }
        } catch (MagentoFrameworkExceptionLocalizedException $ex) {
            $this->productImagelog($ex->getMessage());
            var_dump($ex->getMessage());exit;
            return false;
        } catch (MagentoFrameworkExceptionRuntimeException $ex) {
            $this->productImagelog($ex->getMessage());
            var_dump($ex->getMessage());exit;
            return false;
        }catch (Exception $ex){
            $this->productImagelog($ex->getMessage());
            var_dump($ex->getMessage());exit;
            return false;
        }

        return null;
    }
    public function productImagelog($info) {
        $writer = new ZendLogWriterStream(BP . '/var/log/product_image_sync.log');
        $logger = new ZendLogLogger();
        $logger->addWriter($writer);
        $logger->info($info);
    }

}

Any suggestion how to resolve error