How to add media gallery entries via REST API - Magento 2












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?










share|improve this question
















bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • 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














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?










share|improve this question
















bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • 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












0








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?










share|improve this question















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?







rest media gallery magento2.0.8






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 21 '18 at 4:34









MGento

1,249319




1,249319










asked Mar 3 '17 at 14:09









Bilal Usean

4,44423384




4,44423384





bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • 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




















  • 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


















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






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












2 Answers
2






active

oldest

votes


















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.






share|improve this answer





















  • 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.'"
}
}],
...
}
}'





share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "479"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f162834%2fhow-to-add-media-gallery-entries-via-rest-api-magento-2%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.






    share|improve this answer





















    • 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














    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.






    share|improve this answer





















    • 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












    0








    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.






    share|improve this answer












    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 3 '17 at 16:17









    Sinisa Nedeljkovic

    1,223719




    1,223719












    • 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


















    • 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
















    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




    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




    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




    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.'"
    }
    }],
    ...
    }
    }'





    share|improve this answer


























      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.'"
      }
      }],
      ...
      }
      }'





      share|improve this answer
























        0












        0








        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.'"
        }
        }],
        ...
        }
        }'





        share|improve this answer












        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.'"
        }
        }],
        ...
        }
        }'






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 19 '18 at 8:59









        Key Shang

        1,5361034




        1,5361034






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f162834%2fhow-to-add-media-gallery-entries-via-rest-api-magento-2%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            An IMO inspired problem

            Management

            Has there ever been an instance of an active nuclear power plant within or near a war zone?