2

i use addImageToMediaGallery to add images for products. Images are associated correctly but i've problem, the final path become /media/catalog/product etc etc. I've tried with

$product->addImageToMediaGallery($imgPath, null , false , false); 

or

$product->addImageToMediaGallery($imgPath, null , true , false); 

the result is the same.

How to add an image with a specific path?

Thanks at all.

  • 1
    What path do you want it to be? – Renon Stewart Sep 30 '14 at 13:20
7

Setting the third perimeter of $product->addImageToMediaGallery to true/false doesn't seem to have any effect on the storage destination of the file, it just changes the operation it uses (copy/chmod vs move command) to get it into the correct magento image directory structure.

Also, I don`t think it possible to choose a specific image path without a lot of customization. By default magento store image in the folowing format /media/catalog/product/{a}/{b} where 'a' and 'b' are the first two letter of the image file name.

See

/app/code/core/Mage/Catalog/Model/Product.php

/**
 * Add image to media gallery
 *
 * @param string        $file              file path of image in file system
 * @param string|array  $mediaAttribute    code of attribute with type 'media_image',
 *                                          leave blank if image should be only in gallery
 * @param boolean       $move              if true, it will move source file
 * @param boolean       $exclude           mark image as disabled in product page view
 * @return Mage_Catalog_Model_Product
 */
public function addImageToMediaGallery($file, $mediaAttribute=null, $move=false, $exclude=true)
{
    .....
    /* @var $mediaGalleryAttribute Mage_Catalog_Model_Resource_Eav_Attribute */
    $mediaGalleryAttribute->getBackend()->addImage($this, $file, $mediaAttribute, $move, $exclude);
    return $this;
}

/app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php

/**
 * Add image to media gallery and return new filename
 *
 * @param Mage_Catalog_Model_Product $product
 * @param string                     $file              file path of image in file system
 * @param string|array               $mediaAttribute    code of attribute with type 'media_image',
 *                                                      leave blank if image should be only in gallery
 * @param boolean                    $move              if true, it will move source file
 * @param boolean                    $exclude           mark image as disabled in product page view
 * @return string
 */
public function addImage(Mage_Catalog_Model_Product $product, $file,
    $mediaAttribute = null, $move = false, $exclude = true)
{
  ....
        /** @var $storageHelper Mage_Core_Helper_File_Storage_Database */
        $storageHelper = Mage::helper('core/file_storage_database');
        if ($move) {
            $ioAdapter->mv($file, $this->_getConfig()->getTmpMediaPath($fileName));

            //If this is used, filesystem should be configured properly
            $storageHelper->saveFile($this->_getConfig()->getTmpMediaShortUrl($fileName));
        } else {
            $ioAdapter->cp($file, $this->_getConfig()->getTmpMediaPath($fileName));

            $storageHelper->saveFile($this->_getConfig()->getTmpMediaShortUrl($fileName));
            $ioAdapter->chmod($this->_getConfig()->getTmpMediaPath($fileName), 0777);
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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