3

I want to insert and use an image urls from my own CDN directly into the database. I want to do this to save space because I use the images in other applications as well.

I've wrote a function that populates this two tables: catalog_product_entity_media_gallery and catalog_product_entity_media_gallery_value.

This is what I've inserted as value : http://cdn.domain.com/products/nike/448224-010-00.jpg, while this is the way magento does: /4/4/448224-010-00.jpg

So far so good. But now magento creates an url like this: http://www.domain.com/media/catalog/product/http://cdn.domain.com/products/nike/448224-010-00.jpg

Is there a way to tell magento (extension or module) not to use the media folder and base url? I also want the width or height to be added to the url when it's called.

Add querystring like this: ?w=100&h=100

2

After some research on how to override a function on a Model, I've created my own module/extension. This extension extends the Mage_Catalog_Model_Product_Image Model and this is what I've changed to make it work:

class ImageCdn_Model_Catalog_Product_Image extends Mage_Catalog_Model_Product_Image
{
    protected $_cdnFile;

    public function setBaseFile($file)  {

        // add cdn url
        if ($file && strpos($file, 'http') === 0) {
            $this->_cdnFile = $file;
        }

        return parent::setBaseFile($file);;
    }

    public function getUrl()
    {
        $cdnFile = $this->_cdnFile;

        if ($cdnFile) {

            $width = $this->_width;
            $height = $this->_height;

            if (!is_null($width)){

                if(strpos($cdnFile, "?")){
                    $cdnFile .= "&";
                }else{
                    $cdnFile .= "?";
                }

                $cdnFile.= "w=" . $width;
            }

            if (!is_null($height)){

                if(strpos($cdnFile, "?")){
                    $cdnFile .= "&";
                }else{
                    $cdnFile .= "?";
                }

                $cdnFile.= "h=" . $height;
            }

            return $cdnFile;
        }

        return parent::getUrl();
    }

}

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.