0

I'm creating a SOAP v2 api in Magento, and one of the methods is to upload an image to the Magento media folder. So my question is: how can I send an image to magento without using a form? and how can I upload it to the media folder.

2 Answers 2

1

The easiest way is place images to media/import folder before and then use

$product->addImageToMediaGallery($filePath, array ('image','small_image','thumbnail'), false, false);

function.

Check this comment for the full description of this function addImageToMediaGallery copy the image file to media folder

If you can't upload images, you can use two ways:

  1. Send images to API as strings and on your side use file_put_contents to save it

  2. Send images to API as links and on your side use file_get_contents/file_put_contents to get/save them

5
  • Thanks for the answer, but I can't put the images before because I don't know which image you're going to upload, so I need to create a method that receives per parameters an image ,s o the method will store this image into Magento
    – K.Carl
    Commented Jun 22, 2016 at 17:17
  • I update my answer
    – Andrew Sh
    Commented Jun 22, 2016 at 18:50
  • Thanks, this is my code: error_reporting(E_ALL); ini_set('display_errors', TRUE); $api_url = "local/index.php/api/v2_soap/?wsdl=1"; //For Version 2 $username = 'test'; $password = 'test123'; $client = new SoapClient($api_url,array('cache_wsdl' => WSDL_CACHE_NONE)); //Will cnot cache the WSDL //retreive session id from login $session = $client->login($username, $password); $image= '/home/proximitycr/Desktop/hey.jpg'; $result = $client->loadImageToGallery($session,$image); var_dump($result);
    – K.Carl
    Commented Jun 22, 2016 at 19:35
  • and I Need the method loadImageToGallery
    – K.Carl
    Commented Jun 22, 2016 at 19:35
  • In method loadImageToGallery you pass argument - URL to your image. Use file_get_contents to get this image as string and then file_put_contents to save it to media/import. After that, use Magento function addImageToMediaGallery with URL to your file in media/import.
    – Andrew Sh
    Commented Jun 22, 2016 at 20:34
0
 //get the form data 
 $data = $this->getRequest()->getData(); 
 $sku = $data['sku'];
 //get other product attributes and add like below
 the following is the api that you will call
 try {
                $result = $client->call($session, 'catalog_product.create', array('simple', $attributeSet['set_id'], $sku, array(
                        'categories' => array($categories),
                        'websites' => array($websites),
                        'name' => $name,
                        'description' => $description,
                        'short_description' => $short_description,
                        'weight' => $weight,
                        'status' => $status,
                        'url_key' => str_replace(' ', '_', $url_key),
                        'url_path' => str_replace(' ', '_', $url_key),
                        'visibility' => $visibility,
                        'price' => $price,
                        'tax_class_id' => $tax_class,
                        'meta_title' => $meta_title,
                        'meta_keyword' => $meta_keyword,
                        'meta_description' => $meta_description,
                        'front_image' => $image
                )));
            } catch (SoapFault $fault) {
                Mage::getSingleton('core/session')->addError($fault->getMessage());
                header('Location: ' . Mage::getBaseUrl() . 'product');
                exit;
            }

            $productId = Mage::getModel('catalog/product')->getIdBySku($sku);
            $

if you see in the end you have

$productId

you can add images to products using the following code

        if (!file_exists($imageResized) && file_exists($imagePath)) {
            $imageObj = new Varien_Image($imagePath);
            $imageObj->constrainOnly(TRUE);
            $imageObj->keepAspectRatio(TRUE);
            $imageObj->keepFrame(FALSE);
            $imageObj->resize(600);
            $imageObj->save($imageResized);

            $product->setMediaGallery(array('images' => array(), 'values' => array()));
            $product->addImageToMediaGallery($imageResized, array('image', 'small_image', 'thumbnail'), false, false);
            $product->addImageToMediaGallery
            $product->save();
        }

The idea is this: You are creating the product first then you are adding the images directly after creating the product.

Hope you got the idea. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.