3

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 for all.

1

1 Answer 1

8

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

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

See

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

/**
 * Add image to the media gallery
 *
 * @param string        $file path of the image in the file system
 * @param string|array  $mediaAttribute    code of attribute with type 'media_image',
 *                                          leave blank if the image should be only in a gallery
 * @param boolean       $move              if true, it will move the 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 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 and acknowledge you have read our privacy policy.

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