How to add media gallery entries via REST API - Magento 2Changing Media Gallery Primary ImageREST API creating more than one media entry for productMagento 2 add youtube video to media gallery programmaticallyMagento 2 Get Media Gallery in productlistUse API rest media management by storeView code (admin)Magento 2 is there a faster way to change product media gallery entries programmaticallyError on image upload with REST api1.9 - Adding Image To Media Gallery Using Import BehaviorChange label and position of media in store views via APIMedia gallery for product collection
Is there an idiom that means "revealing a secret unintentionally"?
Can you turn a recording upside-down?
Program for finding longest run of zeros from a list of 100 random integers which are either 0 or 1
What is the status of the three crises in the history of mathematics?
Can the president of the United States be guilty of insider trading?
How do I minimise waste on a flight?
How can Sam Wilson fulfill his future role?
Two (probably) equal real numbers which are not proved to be equal?
How is Arya still alive?
Names of the Six Tastes
"Estrontium" on poster
How do carbureted and fuel injected engines compare in high altitude?
Are on’yomi words loanwords?
Why are thrust reversers not used to slow down to taxi speeds?
Can I bring back Planetary Romance as a genre?
Gift for mentor after his thesis defense?
Ugin's Conjurant vs. un-preventable damage
Was the Highlands Ranch shooting the 115th mass shooting in the US in 2019
Does Thread.yield() do anything if we have enough processors to service all threads?
My perfect evil overlord plan... or is it?
What does the "DS" in "DS-..." US visa application forms stand for?
Double underlining a result in a system of equations with calculation steps on the right side
Are wands in any sort of book going to be too much like Harry Potter?
How likely are Coriolis-effect-based quirks to develop in starship crew members?
How to add media gallery entries via REST API - Magento 2
Changing Media Gallery Primary ImageREST API creating more than one media entry for productMagento 2 add youtube video to media gallery programmaticallyMagento 2 Get Media Gallery in productlistUse API rest media management by storeView code (admin)Magento 2 is there a faster way to change product media gallery entries programmaticallyError on image upload with REST api1.9 - Adding Image To Media Gallery Using Import BehaviorChange label and position of media in store views via APIMedia gallery for product collection
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom: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?
rest media gallery magento2.0.8
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
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,245319
1,245319
asked Mar 3 '17 at 14:09
Bilal UseanBilal Usean
5,22044094
5,22044094
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 NedeljkovicSinisa Nedeljkovic
1,283819
1,283819
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 ShangKey Shang
1,7361335
1,7361335
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.
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