8

Has anyone successfully uploaded an image for use with a category? When going through the documentation @ http://devdocs.magento.com/swagger/index.html#/ I don't see any implementation that could support it.

Also when retrieving a category using Magento REST API you don't get the category image back.

In the admin page, you can add the category here: enter image description here

Gerard de Visser
9551 gold badge10 silver badges27 bronze badges
asked Jan 3, 2016 at 19:52
1
  • Unfortunately we only had capacity to do the catalog extension objects at the time and haven't gotten to this yet. It would be great if someone can send in a PR. Thanks, Chuck Commented Jan 11, 2016 at 17:36

2 Answers 2

6
+50

At the moment this is not possible without extending core functionality. However, category image save/get support can be added to category REST API using extension attributes mechanism:

  1. Declare extension attribute of string type for \Magento\Catalog\Api\Data\CategoryInterface (it will hold base64-encoded image)
  2. Write afterSave plugin for \Magento\Catalog\Api\CategoryRepositoryInterface::save to save image
  3. Write afterLoad plugin for \Magento\Catalog\Api\CategoryRepositoryInterface::get to load image

See more detailed instructions on declaring extension attributes in this answer

answered Jan 11, 2016 at 17:29
1
  • Thanks, that will probably do the job but I don't have that much knowledge to write a PR. I guess I'm left without the functionality then. Commented Jan 12, 2016 at 16:15
0

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.

answered Feb 13, 2024 at 13:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.