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))

  • have u solved this issue? – Wojciech Ka Sep 17 '18 at 14:43
4

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

  • 1
    sad but true... you have to use the deprecated way cause the core developer give a fck to finish there functions... – ZFNerd Nov 13 '18 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, privacy policy and cookie policy

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