0

How to convert base 64 image format to image file and save image in local file directory in magento 2

  public function setProfileImage($customerId, $base64)
        {
             if ($customerId) {
                     //code for convert base64 to image file and save file directory
                  } 

        }

Thanks in advance !!

5
1
public function setProfileImage($customerId, $base64, $name)
    {
        if ($customerId) {

            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $fileSystem = $objectManager->create('\Magento\Framework\Filesystem');
            $mediaPath = $fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath();
            $media = $mediaPath . 'customer/profile_image/';

            $data = $base64;
            list($type, $data) = explode(';', $data);
            list(, $data)      = explode(',', $data);
            $data = base64_decode($data);
            if (file_put_contents($media . $name . uniqid() . '.png', $data)) {
                return 'success';
            } else {
                return 'failed';
            }
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.