Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Magento 将base64映像添加到产品 拥有$product实例和$base64图像,如何设置其缩略图?_Magento_Soap_Magento 1.9_Cdn - Fatal编程技术网

Magento 将base64映像添加到产品 拥有$product实例和$base64图像,如何设置其缩略图?

Magento 将base64映像添加到产品 拥有$product实例和$base64图像,如何设置其缩略图?,magento,soap,magento-1.9,cdn,Magento,Soap,Magento 1.9,Cdn,我目前正在通过Magento上的自定义API(API_v2)创建一个分组产品 我的应用程序发送一个SOAP请求,其中包含我需要的所有信息: 名字 Sku StoreURL 描述 图像1(Base64) 图像2(Base64) 一些自定义属性 带有简单产品数据的数组 新端点使用new Mage\u Catalog\u Model\u Product(),然后我手动调用多个->setAttributeName(value),最后调用-save() 通过AdminPainel这样做,图像存储在./me

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

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

  • 名字
  • Sku
  • StoreURL
  • 描述
  • 图像1(Base64)
  • 图像2(Base64)
  • 一些自定义属性
  • 带有简单产品数据的数组
  • 新端点使用
    new Mage\u Catalog\u Model\u Product()
    ,然后我手动调用多个
    ->setAttributeName(value)
    ,最后调用
    -save()

    通过AdminPainel这样做,图像存储在./media/catalog/product/b/o/image.jpg中,但我不认为该路径是硬编码的

    我知道方法
    $product->set缩略图($image)
    存在于
    setBaseImage()
    setSmallImage()
    之间,但我在传递
    $image
    参数时遇到问题


    是否绝对有必要在保存base64之前将其上载到CDN


    我是否可以将其保存在本地,然后以某种方式上载ProgramMaticali?

    看看magento core的这个api方法(“product\u 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);
    }