Here's an example how you can extend Magento functionality to import the category thumbnail image of a category using extension attribute with base64 string. You can adjust it per your own requirements.
Vendor/Module/etc/extension_attributes.xml:
<?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="thumbnail_name" type="string"/>
<attribute code="thumbnail_base64_string" type="string"/>
</extension_attributes>
</config>
Vendor/Module/etc/webapi_rest/di.xml:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Model\CategoryRepository">
<plugin name="category_process_base64_thumbnail" type="Vendor\Module\Plugin\Model\CategoryRepositoryPlugin"/>
</type>
</config>
Vendor/Module/Plugin/Model/CategoryRepositoryPlugin:
<?php
declare(strict_types=1);
namespace Vendor\Module\Plugin\Model;
use Magento\Catalog\Api\Data\CategoryExtension;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Model\CategoryRepository;
use Magento\Framework\Api\Data\ImageContentInterface;
use Magento\Framework\Api\ImageProcessorInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\InputException;
class CategoryRepositoryPlugin
{
public function __construct(
private readonly ImageContentInterface $imageContent,
private readonly ImageProcessorInterface $imageProcessor
) {
}
/**
* @param CategoryRepository $subject
* @param CategoryInterface $category
*
* @return CategoryInterface[]|null
*/
public function beforeSave(CategoryRepository $subject, CategoryInterface $category): ?array
{
/** @var CategoryExtension $categoryExtensionAttributes */
$categoryExtensionAttributes = $category->getExtensionAttributes();
$this->imageContent
->setName($categoryExtensionAttributes->getThumbnailName())
->setBase64EncodedData($categoryExtensionAttributes->getThumbnailBase64String())
->setType('image/jpeg');
try {
$entityType = 'catalog/category';
$uploadedFileName = $this->imageProcessor->processImageContent(
$entityType,
$this->imageContent
);
} catch (InputException $e) {
return null;
}
/** @var \Magento\Catalog\Model\Category $category */
$category->setData(
'thumbnail',
DIRECTORY_SEPARATOR . DirectoryList::MEDIA . DIRECTORY_SEPARATOR . $entityType . $uploadedFileName
);
return [$category];
}
}
Also at the Vendor/Module/registration.php and Vendor/Module/etc/module.xml files and run bin/magento setup:upgrade
and bin/magento cache:flush
.
After that you can do a POST request to categories
endpoint with payload like:
{
"category": {
"parent_id": 2,
"name": "Category Test",
"is_active": true,
"level": 1,
"extension_attributes": {
"thumbnail_name": "category-test-thumbnail.jpeg",
"thumbnail_base64_string": "/9j/....."
}
}
}
The image will be saved as pub/media/catalog/category/category-test-thumbnail.jpeg and added as thumbnail image to the category.