dougan7657
2012-12-04 13:00
浏览 64
已采纳

如何使用magento api创建图像

I'm trying to use the magento api to create an image, but it's failing with the message

SoapFault exception: [104] Cannot create image.

This is not a helpful error message! What does this mean? Why is it doing this? How do I create an image, using the magento api?

This is my code:

$client = new SoapClient($wsdlurl);
$sessionId = $client->login($user, $key);
$file = array(
    'content' =>
    base64_encode(file_get_contents($filename)),
    'mime' => 'image/jpeg',
    'name' => 'newfile.jpg'
);

$result = $client->call(
    $sessionId,
    'catalog_product_attribute_media.create',
    array(
        $productId,
        array('file'=>$file, 'label'=>'new_test_label', 'position'=>'100', 'types'=>array('thumbnail'), 'exclude'=>0)
    )
);

What am I doing wrong?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用magento api创建一个图像,但它没有显示消息

  SoapFault异常:[104]无法创建图像。
   
 
 

这不是一个有用的错误消息! 这是什么意思? 它为什么这样做? 如何使用magento api创建图像?

这是我的代码:

  $ client = new SoapClient($ wsdlurl  ); 
 $ sessionId = $ client-> login($ user,$ key); 
 $ file = array(
'content'=> 
 base64_encode(file_get_contents($ filename)),
'  mime'=>'image / jpeg',
'name'=>'newfile.jpg'
); 
 
 $ result = $ client-> call(
 $ sessionId,
'  catalog_product_attribute_media.create',
 array(
 $ productId,
 array('file'=> $ file,'label'=>'new_test_label','position'=>'100','types'  =>数组('缩略图'),'排除'=> 0)
)
); 
   
 
 

我做错了什么?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doujiu3768 2012-12-04 13:39
    已采纳

    I found the solution. The directory it was trying to put the image in (.../media/catalog/product/) did not have permissions set to allow new folders to be created in it.

    For the benefit of others, though, I did find a couple of useful things: First, filesystem operations are carried out by ./lib/Varien/Io/File.php, which has it's own wrappers for cd, mkdir, etc, which throw really vague exceptions if they fail.

    The directory creation was occurring in Mage_Catalog_Model_Product_Attribute_Backend_Media->_moveImageFromTmp

    which was called from ./app/code/core/Mage/Catalog/Model/Product/Attribute/Media/Api.php which handles all the image api calls, e.g. create, update, remove, etc.

    So, if you have the same problem and it's not a permissions issue... you now know where to put your debug code ;).

    Oh yeah, and if you want to output an error message, you can't just echo or die; you need to throw an exception, which will get caught by Mage_Catalog_Model_Product_Attribute_Media_Api, which will then call $this->_fault to send it to the client.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanshan3331 2014-11-16 23:28

    I just wanted to add some other relevant information to this - and thanks Benubird for your comments, they were very helpful. I was trying to upload images via the API and was getting the "Cannot create image" message.

    After diving deep into Magento I found that if you have a memory limit in your php.ini file of -1 then this PREVENTS any image from being uploaded. In the Magento core there is a check made before the image is attempted to be dealt with (lib/Varien/Image/Adapter/Gd2.php line 57) where the size of the image is compared to the available memory. It does not take into account a value of -1 meaning unlimited and throws a "Memory limit has been reached" exception.

    The fix for this is to set your memory_limit value to a non-negative number. The correct fix for this is for the Magento code to handle this properly (although if imagecreatefromjpeg dies with a non-catchable error then maybe it's not possible).

    Good luck!

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题