How to add media gallery entries via REST API - Magento 2
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
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.
add a comment |
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
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
add a comment |
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
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
rest media gallery magento2.0.8
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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 errorMIME 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. Onlybase64_encode()
function output must be passed.
– Gediminas
Dec 4 '17 at 10:58
add a comment |
Notice
We should use
image_type_to_mime_type
function to get the MIME type, not use the file extension, because extension sometime is wrong.'data:image/'.$type.';base64,'
beginning in the base64 string doesn't need in Magento2, we just needbase64_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.'"
}
}],
...
}
}'
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 errorMIME 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. Onlybase64_encode()
function output must be passed.
– Gediminas
Dec 4 '17 at 10:58
add a comment |
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 errorMIME 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. Onlybase64_encode()
function output must be passed.
– Gediminas
Dec 4 '17 at 10:58
add a comment |
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.
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.
answered Mar 3 '17 at 16:17
Sinisa Nedeljkovic
1,223719
1,223719
I tried but no luck, same errorMIME 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. Onlybase64_encode()
function output must be passed.
– Gediminas
Dec 4 '17 at 10:58
add a comment |
I tried but no luck, same errorMIME 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. Onlybase64_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
add a comment |
Notice
We should use
image_type_to_mime_type
function to get the MIME type, not use the file extension, because extension sometime is wrong.'data:image/'.$type.';base64,'
beginning in the base64 string doesn't need in Magento2, we just needbase64_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.'"
}
}],
...
}
}'
add a comment |
Notice
We should use
image_type_to_mime_type
function to get the MIME type, not use the file extension, because extension sometime is wrong.'data:image/'.$type.';base64,'
beginning in the base64 string doesn't need in Magento2, we just needbase64_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.'"
}
}],
...
}
}'
add a comment |
Notice
We should use
image_type_to_mime_type
function to get the MIME type, not use the file extension, because extension sometime is wrong.'data:image/'.$type.';base64,'
beginning in the base64 string doesn't need in Magento2, we just needbase64_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.'"
}
}],
...
}
}'
Notice
We should use
image_type_to_mime_type
function to get the MIME type, not use the file extension, because extension sometime is wrong.'data:image/'.$type.';base64,'
beginning in the base64 string doesn't need in Magento2, we just needbase64_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.'"
}
}],
...
}
}'
answered Jan 19 '18 at 8:59
Key Shang
1,5361034
1,5361034
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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