magentomagento-1.9magento-soap-api

Add base64 image to product


Having a $product instance and a $base64 image, how can I set it's thumbnail image?

I'm currently creating a grouped product via a custom API on Magento (API_v2).

My application sends a SOAP request with all the informations I need:

  1. Name
  2. Sku
  3. StoreURL
  4. Description
  5. Image1 (Base64)
  6. Image2 (Base64)
  7. Some custom attributes
  8. Array with data for simple products

The new endpoint uses new Mage_Catalog_Model_Product(), then I manually call multiple ->setAttributeName(value) then finally -save().

Doing that via Admin Painel, the image is stored inside ./media/catalog/product/b/o/image.jpg, but I don't think that path is hardcoded.

I know the method $product->setThumbnail($image) exists among with setBaseImage() and setSmallImage(), but I'm having issues to pass an $image argument.


Is it absolutelly necessary to upload my base64 to a CDN before saving it?

Can I save it locally then somehow upload programmaticaly?


Solution

  • take a look at this api method ('product_media.create') from magento core. It does exactly what you strive to achieve.

    public function create($productId, $data, $store = null, $identifierType = null)
    {
        $data = $this->_prepareImageData($data);
    
        $product = $this->_initProduct($productId, $store, $identifierType);
    
        $gallery = $this->_getGalleryAttribute($product);
    
        if (!isset($data['file']) || !isset($data['file']['mime']) || !isset($data['file']['content'])) {
            $this->_fault('data_invalid', Mage::helper('catalog')->__('The image is not specified.'));
        }
    
        if (!isset($this->_mimeTypes[$data['file']['mime']])) {
            $this->_fault('data_invalid', Mage::helper('catalog')->__('Invalid image type.'));
        }
    
        $fileContent = @base64_decode($data['file']['content'], true);
        if (!$fileContent) {
            $this->_fault('data_invalid', Mage::helper('catalog')->__('The image contents is not valid base64 data.'));
        }
    
        unset($data['file']['content']);
    
        $tmpDirectory = Mage::getBaseDir('var') . DS . 'api' . DS . $this->_getSession()->getSessionId();
    
        if (isset($data['file']['name']) && $data['file']['name']) {
            $fileName  = $data['file']['name'];
        } else {
            $fileName  = 'image';
        }
        $fileName .= '.' . $this->_mimeTypes[$data['file']['mime']];
    
        $ioAdapter = new Varien_Io_File();
        try {
            // Create temporary directory for api
            $ioAdapter->checkAndCreateFolder($tmpDirectory);
            $ioAdapter->open(array('path'=>$tmpDirectory));
            // Write image file
            $ioAdapter->write($fileName, $fileContent, 0666);
            unset($fileContent);
    
            // try to create Image object - it fails with Exception if image is not supported
            try {
                new Varien_Image($tmpDirectory . DS . $fileName);
            } catch (Exception $e) {
                // Remove temporary directory
                $ioAdapter->rmdir($tmpDirectory, true);
    
                throw new Mage_Core_Exception($e->getMessage());
            }
    
            // Adding image to gallery
            $file = $gallery->getBackend()->addImage(
                $product,
                $tmpDirectory . DS . $fileName,
                null,
                true
            );
    
            // Remove temporary directory
            $ioAdapter->rmdir($tmpDirectory, true);
    
            $gallery->getBackend()->updateImage($product, $file, $data);
    
            if (isset($data['types'])) {
                $gallery->getBackend()->setMediaAttribute($product, $data['types'], $file);
            }
    
            $product->save();
        } catch (Mage_Core_Exception $e) {
            $this->_fault('not_created', $e->getMessage());
        } catch (Exception $e) {
            $this->_fault('not_created', Mage::helper('catalog')->__('Cannot create image.'));
        }
    
        return $gallery->getBackend()->getRenamedImage($file);
    }