【问题标题】:Add base64 image to product将base64图像添加到产品
【发布时间】:2019-02-25 01:11:37
【问题描述】:

有一个 $product 实例和一个 $base64 图片,如何设置它的缩略图?

我目前正在通过 Magento (API_v2) 上的自定义 API 创建分组产品。

我的应用程序发送一个包含我需要的所有信息的 SOAP 请求:

  1. 姓名
  2. Sku
  3. StoreURL
  4. 说明
  5. Image1 (Base64)
  6. Image2 (Base64)
  7. 一些自定义属性
  8. 包含简单产品数据的数组

新端点使用new Mage_Catalog_Model_Product(),然后我手动调用多个->setAttributeName(value),最后是-save()

通过 Admin Painel 执行此操作,图像存储在 ./media/catalog/product/b/o/image.jpg 中,但我不认为该路径是硬编码的。

我知道$product->setThumbnail($image) 方法存在于setBaseImage()setSmallImage() 中,但我在传递$image 参数时遇到问题。


在保存之前绝对有必要将我的 base64 上传到 CDN 吗?

我可以将其保存在本地,然后以某种方式以编程方式上传吗?

【问题讨论】:

    标签: magento soap magento-1.9 cdn


    【解决方案1】:

    看看这个来自 magento 核心的 api 方法('product_media.create')。它完全符合您的目标。

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多