0

I stored image in pub/media/catalog/product. How to add that images into media gallery entries.

I have tried like http://myhost/rest/V1/products

"media_gallery_entries"=> array(
    array(
        "media_type"=> "image",
        "label"=> "Product Image",
        "position"=> 1,
        "disabled"=> false,
        "types"=> array(
            "image",
            "small_image",
            "thumbnail"
        ),
        "file"=> "/1/0/10.jpg"
    )
),

but it always complain about "message":"The image content is not valid."

base64_encode is the only way to add image into rest api?

if yes I tried with

"media_gallery_entries"=> array(
    array(
        "media_type"=> "image",
        "label"=> "Product Image",
        "position"=> 1,
        "disabled"=> false,
        "types"=> array(
            "image",
            "small_image",
            "thumbnail"
        ),
        "content"=> array(
            "base64_encoded_data"=> $data->getMainImage(),
            "type"=> "image/jpeg",
            "name"=> "test.jpg"
        ),
    )
),

It throws exception "message":"The image MIME type is not valid or not supported."

I found supported mime type in /vendor/magento/framework/Api/ImageContentValidator.php

private $defaultMimeTypes = [
    'image/jpg',
    'image/jpeg',
    'image/gif',
    'image/png',
];

It support image/jpeg, but I don't know why it throws exception.

Can anyone enlighten me to how to add media galleries entries?

  • oops! it is my fault in second example I encode .png image instead of .jpg. I'm really more concerns about how to add existing image into media gallery entries[First example]. can anyone help me on this? – Bilal Usean Mar 4 '17 at 6:47
0

Try with your second example, but instead of media_type, use mediaType tag.

"media_gallery_entries"=> array(
    array(
        "media_type"=> "image",
        "label"=> "Product Image",
        "position"=> 1,
        "disabled"=> false,
        "types"=> array(
            "image",
            "small_image",
            "thumbnail"
        ),
        "content"=> array(
            "base64_encoded_data"=> $data->getMainImage(),
            "type"=> "image/jpeg",
            "name"=> "test.jpg"
        ),
    )
),

Also, you'll probably have to use base64_encode($data->getMainImage()) instead of $data->getMainImage() only.

  • I tried but no luck, same error MIME type is not valid or not supported. I retrive base64 encode string from database $data->getMainImage() – Bilal Usean Mar 4 '17 at 4:52
  • do you have any idea about first example? – Bilal Usean Mar 4 '17 at 6:49
  • maybe you are retrieving base64 string with "data:image/jpeg;base64," beginning which is not needed. Only base64_encode() function output must be passed. – Gediminas Dec 4 '17 at 10:58
0

Notice

  1. We should use image_type_to_mime_type function to get the MIME type, not use the file extension, because extension sometime is wrong.

  2. 'data:image/'.$type.';base64,' beginning in the base64 string doesn't need in Magento2, we just need base64_encode($imageData);

Work Example: post product image from Magento 1.9 to Magento 2.2

<?php
$imagePath = $product->getImage();
$imageFullPath = Mage::getBaseDir('media').'/catalog/product'.$imagePath;
$imageName = basename($imagePath);
$imageType = exif_imagetype($imageFullPath);
$mimeType = image_type_to_mime_type($imageType);
$imageData = file_get_contents($imageFullPath);
$base64EncodedData = base64_encode($imageData);

$json =
    '{
      "product": {
        ...
        "media_gallery_entries": [{
            "media_type": "image",
            "label": "'.$imageName.'",
            "position": 0,
            "disabled": false,
            "types": [
                "image",
                "small_image",
                "thumbnail"
            ],
            "file": "'.$imagePath.'",
            "content": {
                "base64_encoded_data": "'.$base64EncodedData.'",
                "type": "'.$mimeType.'",
                "name": "'.$imageName.'"
            }
        }],
        ...
      }
    }'

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.