Skip to content

The image content is invalid. Verify the content and try again

We are running a custom import and some products are gettign the error

The image content is invalid. Verify the content and try again.

I found a workaround to this issue saving my model instead of repository (can’t really know why it works but it works). But then, in some cases i’m also getting an other error (but it’s not each time and I can’t see in the database the reason triggering the error).

| Error Sku 364e3038303939363145-pare-boue-avant-gauche SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (surplusautos_db.catalog_product_entity_media_gallery_value, CONSTRAINT CAT_PRD_ENTT_MDA_GLR_VAL_VAL_ID_CAT_PRD_ENTT_MDA_GLR_VAL_ID FOREIGN KEY (value_id) REFERENCES catalog_product_en), query was: INSERT INTO catalog_product_entity_media_gallery_value (value_id, store_id, entity_id, label, position, disabled`) VALUES (?, ?, ?, ?, ?, ?)

As log here is what I have for a working product and a not working product

2024-08-25T04:15:33+00:00 INFO (6): [INFO] Treat SKU : 364e3038303939363145-pare-boue-avant-gauche Webstite : 0
2024-08-25T04:15:34+00:00 INFO (6): [INFO] Save attempt
2024-08-25T04:15:34+00:00 INFO (6): [INFO] The image content is invalid. Verify the content and try again.
2024-08-25T04:15:34+00:00 INFO (6): [INFO] Erreur Sku 364e3038303939363145-pare-boue-avant-gauche SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`surplusautos_db`.`catalog_product_entity_media_gallery_value`, CONSTRAINT `CAT_PRD_ENTT_MDA_GLR_VAL_VAL_ID_CAT_PRD_ENTT_MDA_GLR_VAL_ID` FOREIGN KEY (`value_id`) REFERENCES `catalog_product_en), query was: INSERT INTO `catalog_product_entity_media_gallery_value` (`value_id`, `store_id`, `entity_id`, `label`, `position`, `disabled`) VALUES (?, ?, ?, ?, ?, ?)





2024-08-25T10:30:38+00:00 INFO (6): [INFO] Treat SKU : 3936343031314b413541-pare-soleil-gauche Webstite : 0
2024-08-25T10:30:39+00:00 INFO (6): [INFO] Save attempt
2024-08-25T10:30:39+00:00 INFO (6): [INFO] The image content is invalid. Verify the content and try again.
2024-08-25T10:30:40+00:00 INFO (6): [INFO] Save Success by model
2024-08-25T10:30:40+00:00 INFO (6): [INFO] Treat SKU : 3936343031314b413541-pare-soleil-gauche Webstite : 1
2024-08-25T10:30:40+00:00 INFO (6): [INFO] Save attempt
2024-08-25T10:30:40+00:00 INFO (6): [INFO] The image content is invalid. Verify the content and try again.
2024-08-25T10:30:41+00:00 INFO (6): [INFO] Save Success by model
2024-08-25T10:30:41+00:00 INFO (6): [INFO] Treat SKU : 3936343031314b413541-pare-soleil-gauche Webstite : 2
2024-08-25T10:30:41+00:00 INFO (6): [INFO] Save attempt
2024-08-25T10:30:42+00:00 INFO (6): [INFO] Save Success by repository

And here is the matching code

                    $productModel = $this->productFactory->create();
                    $productModel->load($productNewData->getId());
                    $oldImages = $productModel->getMediaGalleryImages();

                    $imagesToDelete = array();
                    foreach ($oldImages as $oldImage) {
                        $imagesToDelete[] = $oldImage['value_id'];
                    }

                    $productModel = $this->treatImages($lastItem,$productModel);


                    $newImages = $productModel->getMediaGalleryImages();
                    $gallery = $this->gallery->create();
                    foreach ($newImages as $item) {
                        if (in_array($item['value_id'], $imagesToDelete)) {
                            $gallery->deleteGallery($item['value_id']);
                            $this->imageprocessor->removeImage($productModel, $item['file']);
                        }
                    }
                    try{
                        $this->logInfoData($logger,"Save attempt");
                        $this->productRepository->save($productModel);
                        $this->logInfoData($logger,"Save Success by repository");
                    }catch (Exception $e){
                        $this->logInfoData($logger,$e->getMessage());
                        $productModel->save();
                        $this->logInfoData($logger,"Save Success by model");
                    }
                }
            } catch (Exception $e) {
                $this->rapportRunning['error'][] = "Erreur Sku ".$sku.' '.$e->getMessage();
                $this->logInfoData($logger,"Erreur Sku ".$sku.' '.$e->getMessage());
            }

With the treatImage being like

    public function treatImages($lastItem,$productModel) {

        $imageCounter = 0;
        $images = json_decode($lastItem['liens']);
        $mediaattribute = array( 'image', 'thumbnail', 'small_image');

        if ($images !== null) {
            $tmpDir = $this->getMediaDTmpDir();
            $this->file->checkAndCreateFolder($tmpDir);
            foreach ($images as $image) {
                $newFilename = $tmpDir . baseName($image);
                $resultImage = $this->atemoFileHelper->readAtemoFile($image, $newFilename);
                if ($resultImage) {
                    if ($imageCounter === 0) {
                        $productModel->addImageToMediaGallery($newFilename, $mediaattribute, false, false);
                    } else {
                        $productModel->addImageToMediaGallery($newFilename, null, false, false);
                    }
                }
                $imageCounter++;
            }
        }
        return $productModel;
    }