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?
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.
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!