2

I have below code with save configurable Image. But images are not save pragmatically showing below error

    /*Add Images To The Product*/
    $imagePath = "sample.png"; // path of the image
    $product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);
  $productRepository->save($product);

error

1 exception(s): Exception #0 (Magento\Framework\Exception\InputException): The image content is not valid.

Exception #0 (Magento\Framework\Exception\InputException): The image content is not valid.

0 /var/www/html/awardsideas/vendor/magento/module-catalog/Model/ProductRepository.php(590):

Magento\Catalog\Model\ProductRepository->processMediaGallery(Object(Magento\Catalog\Model\Product\Interceptor), Array)

1 /var/www/html/awardsideas/vendor/magento/framework/Interception/Interceptor.php(58):

Magento\Catalog\Model\ProductRepository->save(Object(Magento\Catalog\Model\Product\Interceptor))

1
  • have u solved this issue? Commented Sep 17, 2018 at 14:43

2 Answers 2

6

Try this $product->save(); instead of $productRepository->save($product);

1
  • 2
    sad but true... you have to use the deprecated way cause the core developer give a fck to finish there functions...
    – ZFNerd
    Commented Nov 13, 2018 at 15:45
0

I was having same issue while trying to duplicate products in 2.3.3 . The image that has issue was in DB (media gallery table) but physical file was not available and copying of images from old product to new product was not working because of this case.

Solution mentioned in this link worked for me: How can I remove not existing images in magento 2

i created a function based on this suggestion which will delete such images from the database. My function looked as below:

public function deleteOrphanImages()
{
    $mediaDir = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA);

    $connection = $this->resourceConnection->getConnection('core_write');

    $tableName = $connection->getTableName('catalog_product_entity_media_gallery');
    $result = $connection->fetchAll('SELECT * FROM ' .$tableName. ' where media_type = "image"');
    foreach ($result as $item) {
        $imagePath = $mediaDir->getAbsolutePath($item['value']);

        if (!$mediaDir->isExist($imagePath)) {
            $sql = "Delete FROM " . $tableName . " Where value_id = " . $item['value_id'];
            $connection->query($sql);
        }
    }

}

I personally did not like using direct SQL to delete from DB but this is something working for me.

Your Answer

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

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