Magento 2 REST API – How to Save Custom Image in Database Table

apiimagemagento-2.1magento2

How to save custom image coming from third party platform to save into magento 2 custom table.

I have just created REST api for my requirements.

I passed image in base_64 encoded string from Thirdparty platform and i got those image base_64 encoded string in magento 2.

But i dont know how to save image from base_64 encoded string into my custom table. what is the procedure for this event.

Any help will be appericiated.

Thanks.

Best Answer

You need to create custom module for this functionality .

Below are the summarized point for custom module and its code .

  • First i have done the same operation to save category image via rest api in magento 2
  • Create one observer which called on catalog_category_save_before event Then ,You need to create String type of extension_attributes for the custom image.
  • After that you can pass base64_encoded image data into that extension_attributes.
  • you need to decode that base64_encoded image and use file_put_contents function to create/save image.
  • Magento EAV structure will automatically save your image file name into database and value for the same if you overrides correctly.

Now Coming on the code , See below file wise code for more clarity.

Events.xml file :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
  <!-- category save before event -->
  <event name="catalog_category_save_before">
      <observer name="catsaveafter" instance="Vendor\ModuleName\Model\Categorysaveimage" />
  </event>
</config> 

extension_attributes.xml (Here creating custom extension_attributes for image)

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Catalog\Api\Data\CategoryInterface">
        <attribute code="category_baseimage" type="string">            
        </attribute>
    </extension_attributes>
</config>

Observer file.php(Ex - Categorysaveimage.php)

<?php
namespace Vendor\ModuleName\Model;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Filesystem\DirectoryList;

class Categorysaveimage implements ObserverInterface
{
    protected $_filesystem;

    public function __construct(
        \Magento\Framework\Filesystem $_filesystem
    )
    {
        $this->_filesystem = $_filesystem;
    }

    public function execute(\Magento\Framework\Event\Observer $observer) 
    {    
        $category = $observer->getCategory();       
        $extensionAttributes = $category->getExtensionAttributes();     
        if ($extensionAttributes === null) {     
            $extensionAttributes = $this->getOrderExtensionDependency();     
        }
        $attr = $category->getData('category_baseimage');

        if($attr !== null){         
            $mediapath = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('catalog/category');

            $imgContent = explode(",",$attr);           
            $imgName = $imgContent[0];
            $imgEncodedData = base64_decode($imgContent[1]);
            $path = $mediapath.'/'.$imgName;

            file_put_contents($path, $imgEncodedData);
            $extensionAttributes->setCategoryBaseimage($attr);   
            $category->setExtensionAttributes($extensionAttributes);
            $category->setImage($imgName); 
        }
        return;
    }

    private function getOrderExtensionDependency() 
    {    
        $orderExtension = \Magento\Framework\App\ObjectManager::getInstance()->get(
            '\Magento\Catalog\Api\Data\CategoryExtension'
        );   
        return $orderExtension;  
    }

}

Note : As i mention i have done this module to save image of category using rest api , same way you can do as per your need .

Hope it helps..

Related Topic