66
77namespace Magento \Catalog \Model \Product \Gallery ;
88
9+ use Magento \Framework \Api \Data \ImageContentInterface ;
910use Magento \Framework \App \Filesystem \DirectoryList ;
1011use Magento \Framework \Exception \LocalizedException ;
1112use Magento \Framework \Filesystem \DriverInterface ;
@@ -183,6 +184,13 @@ public function addImage(
183184 $ attrCode = $ this ->getAttribute ()->getAttributeCode ();
184185 $ mediaGalleryData = $ product ->getData ($ attrCode );
185186 $ 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+
186194 if (!is_array ($ mediaGalleryData )) {
187195 $ mediaGalleryData = ['images ' => []];
188196 }
@@ -195,11 +203,19 @@ public function addImage(
195203
196204 $ position ++;
197205 $ mediaGalleryData ['images ' ][] = [
198- 'file ' => $ fileName ,
199- 'position ' => $ position ,
206+ 'file ' => $ fileName ,
207+ 'position ' => $ position ,
208+ 'label ' => '' ,
209+ 'disabled ' => (int )$ exclude ,
200210 '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+ ]
203219 ];
204220
205221 $ product ->setData ($ attrCode , $ mediaGalleryData );
0 commit comments