1

I want to create images using Base64EncodedData in Magento 2.

Teja Bhagavan Kollepara
  • 3,816
  • 5
  • 32
  • 69
Pavan U
  • 51
  • 7

1 Answers1

3

Magento 2 provides some APIs to work with images:

vendor/magento/framework/Api/ImageContent.php vendor/magento/framework/Api/Data/ImageContentInterface.php vendor/magento/framework/Api/ImageContentValidator.php vendor/magento/framework/Api/ImageProcessor.php

Let's go to this file: dev/tests/api-functional/testsuite/Magento/Customer/Api/AccountManagementCustomAttributesTest.php, we can find some testing cases:

        $testImagePath = __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'test_image.jpg';
        $imageData = base64_encode(file_get_contents($testImagePath));
        $image = $this->imageFactory->create()
            ->setType('image/jpeg')
            ->setName('sample.jpeg')
            ->setBase64EncodedData($imageData);

We can try to test with the playground file.

Khoa TruongDinh
  • 32,054
  • 11
  • 88
  • 155