6
6
7
7
namespace Magento \Catalog \Model \Product \Gallery ;
8
8
9
+ use Magento \Framework \Api \Data \ImageContentInterface ;
9
10
use Magento \Framework \App \Filesystem \DirectoryList ;
10
11
use Magento \Framework \Exception \LocalizedException ;
11
12
use Magento \Framework \Filesystem \DriverInterface ;
@@ -183,6 +184,13 @@ public function addImage(
183
184
$ attrCode = $ this ->getAttribute ()->getAttributeCode ();
184
185
$ mediaGalleryData = $ product ->getData ($ attrCode );
185
186
$ position = 0 ;
187
+
188
+ $ absoluteFilePath = $ this ->mediaDirectory ->getAbsolutePath ($ file );
189
+ $ imageMimeType = mime_content_type ($ absoluteFilePath );
190
+ $ imageContent = file_get_contents ($ absoluteFilePath );
191
+ $ imageBase64 = base64_encode ($ imageContent );
192
+ $ imageName = pathinfo ($ destinationFile , PATHINFO_FILENAME );
193
+
186
194
if (!is_array ($ mediaGalleryData )) {
187
195
$ mediaGalleryData = ['images ' => []];
188
196
}
@@ -195,11 +203,19 @@ public function addImage(
195
203
196
204
$ position ++;
197
205
$ mediaGalleryData ['images ' ][] = [
198
- 'file ' => $ fileName ,
199
- 'position ' => $ position ,
206
+ 'file ' => $ fileName ,
207
+ 'position ' => $ position ,
208
+ 'label ' => '' ,
209
+ 'disabled ' => (int )$ exclude ,
200
210
'media_type ' => 'image ' ,
201
- 'label ' => '' ,
202
- 'disabled ' => (int )$ exclude ,
211
+ 'types ' => $ mediaAttribute ,
212
+ 'content ' => [
213
+ 'data ' => [
214
+ ImageContentInterface::NAME => $ imageName ,
215
+ ImageContentInterface::BASE64_ENCODED_DATA => $ imageBase64 ,
216
+ ImageContentInterface::TYPE => $ imageMimeType ,
217
+ ]
218
+ ]
203
219
];
204
220
205
221
$ product ->setData ($ attrCode , $ mediaGalleryData );
0 commit comments