cancel
Showing results for 
Search instead for 
Did you mean: 

Custom product importscript error: The image content is not valid.

SOLVED

Custom product importscript error: The image content is not valid.

Hello,

I'm running Magento 2.2.4 and try to import a product through a script which does something like this:

<?php
require_once 'app/bootstrap.php';

$imageFileName = '/var/www/html/pub/media/pic1.jpg';

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$objectManager->get(\Magento\Framework\Registry::class)->register('isSecureArea', true, true);
$objectManager->get(\Magento\Framework\App\State::class)->setAreaCode('adminhtml');
$productRepo = $objectManager->get(\Magento\Catalog\Model\ProductRepository::class);

$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
$product->setSku('123');
$product->setName('123');
$product->setPrice("19.99");
$product->setTypeId('simple');
$product->setAttributeSetId(4);

$product->addImageToMediaGallery($imageFileName, ['image', 'small_image', 'thumbnail'], true, false);

$productRepo->save($product);

Unfortunately when the script runs the following error is produced:

PHP Fatal error:  Uncaught Magento\Framework\Exception\InputException: The image content is not valid. in /var/www/html/vendor/magento/module-catalog/Model/ProductRepository/MediaGalleryProcessor.php:130
Stack trace:
#0 /var/www/html/vendor/magento/module-catalog/Model/ProductRepository/MediaGalleryProcessor.php(112): Magento\Catalog\Model\ProductRepository\MediaGalleryProcessor->processEntries(Object(Magento\Catalog\Model\Product\Interceptor), Array, Array)
#1 /var/www/html/vendor/magento/module-catalog/Model/ProductRepository.php(477): Magento\Catalog\Model\ProductRepository\MediaGalleryProcessor->processMediaGallery(Object(Magento\Catalog\Model\Product\Interceptor), Array)
#2 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Catalog\Model\ProductRepository->save(Object(Magento\Catalog\Model\Product\Interceptor))
#3 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Catalog\Model\ProductRepository\Interceptor->___callParent('save', Array)
#4 /var/www/html/vendor/m in /var/www/html/vendor/magento/module-catalog/Model/ProductRepository/MediaGalleryProcessor.php on line 130

Without the $product->addImageToMediaGallery(...); line the script works just fine. The image file is readable and a valid jpg.

Perhaps I'm doing something really dumb because I just started coding for Magento. Would be nice if someone could get me in the right direction Smiley Happy

 

7 REPLIES

Re: Custom product importscript error: The image content is not valid.

Hi @frank_entz

 

Are you running this script from the root folder of the magento 2 ?

 

Second thing instead of - $imageFileName = '/var/www/html/pub/media/pic1.jpg';

 

can you write below code and then check ?

 

$imageFileName = '../pub/media/pic1.jpg';

Meaning - remove var/www/html from the path if you are running this script from the root directory of magento 2.

 

then check and let me know if you still have any issue on the same 

 

Hope it helps !

if issue solved,Click Kudos & Accept as Solution

Re: Custom product importscript error: The image content is not valid.

Hi @ManthanDave,

thanks for your reply.



Are you running this script from the root folder of the magento 2 ?

 


Yes. It's just to test things on a local install so I thought this would be ok.

 


Second thing instead of - $imageFileName = '/var/www/html/pub/media/pic1.jpg';

 

can you write below code and then check ?

 

$imageFileName = '../pub/media/pic1.jpg';

Meaning - remove var/www/html from the path if you are running this script from the root directory of magento 2.

 


If I run it with either

$imageFileName = '../pub/media/pic1.jpg';

or

$imageFileName = 'pub/media/pic1.jpg';

I get an "Image does not exist" error. But the image is still under /var/www/html/pub/media/pic1.jpg

 

Re: Custom product importscript error: The image content is not valid.

Hi @frank_entz

 

okay - i understand !! i think this is known issue previously !!

 

can you do first product details save then image code and save it again and then try ? 

 

Below is the code  : 

<?php
require_once 'app/bootstrap.php';


$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$objectManager->get(\Magento\Framework\Registry::class)->register('isSecureArea', true, true);
$objectManager->get(\Magento\Framework\App\State::class)->setAreaCode('adminhtml');
$productRepo = $objectManager->get(\Magento\Catalog\Model\ProductRepository::class);

$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
$product->setSku('123');
$product->setName('123');
$product->setPrice("19.99");
$product->setTypeId('simple');
$product->setAttributeSetId(4);

$productRepo->save($product);

//image code starting from here

$imageFileName = '/var/www/html/pub/media/pic1.jpg';
$product->addImageToMediaGallery($imageFileName, ['image', 'small_image', 'thumbnail'], true, false);

$productRepo->save($product);

 

Replace above code with your once ! and then try , it might work for you !

if issue solved,Click Kudos & Accept as Solution

Re: Custom product importscript error: The image content is not valid.

I still get the "Image content is not valid" error. Even if I save the product before adding the the image and after adding it.

 

Re: Custom product importscript error: The image content is not valid.

Hi @frank_entz

 

okay , i understand !! i have slightly modified the code and now its working fine !

 

Try below code : 

 

<?php

require_once 'app/bootstrap.php';

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

$product = $objectManager->create('\Magento\Catalog\Model\Product');

$objectManager->get(\Magento\Framework\Registry::class)->register('isSecureArea', true, true);
$objectManager->get(\Magento\Framework\App\State::class)->setAreaCode('adminhtml');

$product->setSku('123'); // Set your sku here
$product->setName('123'); // Name of Product
$product->setAttributeSetId(4); // Attribute set id
$product->setStatus(1); // Status on product enabled/ disabled 1/0
$product->setWeight(10); // weight of product
$product->setVisibility(4); // visibilty of product (catalog / search / catalog, search / Not visible individually)
$product->setTaxClassId(0); // Tax class id
$product->setTypeId('simple'); // type of product (simple/virtual/downloadable/configurable)
$product->setPrice(19.99); // price of product
$product->setStockData(
                        array(
                            'use_config_manage_stock' => 0,
                            'manage_stock' => 1,
                            'is_in_stock' => 1,
                            'qty' => 999999999
                        )
                    );
$product->save();
 
// Adding Image to product
$imagePath = "/var/www/html/mm223/pub/media/logo_puma.jpg"; // path of the image
$product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);
$product->save();

Make sure your image path is correct ! 

 

Hope it helps !

if issue solved,Click Kudos & Accept as Solution

Re: Custom product importscript error: The image content is not valid.

Hi,

yes your code works fine. Thank you!

 

Re: Custom product importscript error: The image content is not valid.

Hi @frank_entz

 

Glad to know that you have resolved your issue !! happy to help and keep helping Smiley Happy

if issue solved,Click Kudos & Accept as Solution