Resize image from URL in Magento

11,063

Solution 1

There is no built in functionality for resizing category images. However you can utilize Varien_Image class. Here I wrote a piece of code you need:

foreach ($collection as $_category){
    $_file_name = $_category->getImage();
    $_media_dir = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'category' . DS;
    $cache_dir = $_media_dir . 'cache' . DS;
    if (file_exists($cache_dir . $_file_name)) {
        echo Mage::getBaseUrl('media') . DS . 'catalog' . DS . 'category' . DS . 'cache' . DS . $_file_name;
    } elseif (file_exists($_media_dir . $_file_name)) {
        if (!is_dir($cache_dir)) {
            mkdir($cache_dir);
        }

        $_image = new Varien_Image($_media_dir . $_file_name);
        $_image->constrainOnly(true);
        $_image->keepAspectRatio(true);
        $_image->keepFrame(true);
        $_image->keepTransparency(true);
        $_image->resize(50, 50);
        $_image->save($cache_dir . $_file_name);

        echo Mage::getBaseUrl('media') . DS . 'catalog' . DS . 'category' . DS . 'cache' . DS . $_file_name;
    }
}

Solution 2

You shouldn't directly call an image using Mage::getBaseDir('media') to output to the browser otherwise you're inviting hackers to tamper with your servers. Use the Mage::getUrl('media') to get the url to the media directory like below:

foreach ($categories as $category) {
        $category = Mage::getModel('catalog/category')->load($category->getId());
        $category_name = $this->stripTags($category->getName(), null, true);
        $category_url = $category->getUrl();
        $category_img = $category->getImage();
        $media_dir = Mage::getBaseDir('media').DS.'catalog'.DS.'category'.DS;
        $cache_dir = $media_dir.'cache'.DS;
        $cache_url = Mage::getUrl('media').'catalog'.DS.'category'.DS.'cache'.DS;

        if (file_exists($cache_dir.$category_img)) {
            $category_img_url = $cache_url.$category_img;
        } elseif (file_exists($media_dir.$category_img)) {
            if (!is_dir($cache_dir)) {
                mkdir($cache_dir);
            }

            $image = new Varien_Image($media_dir.$category_img);
            $image->constrainOnly(true);
            $image->keepAspectRatio(true);
            $image->keepFrame(true);
            $image->keepTransparency(true);
            $image->resize(140, 140);
            $image->save($cache_dir.$category_img);

            $category_img_url = $cache_url.$category_img;
        }
}
Share:
11,063
Randy Hall
Author by

Randy Hall

I do some pretty odd web stuff. Basically, I'm an uneducated code monkey who likes to play. Sometimes, it turns out cool. Sometimes I build a square wheel. It's all good.

Updated on July 07, 2022

Comments

  • Randy Hall
    Randy Hall almost 2 years

    I am retrieving a url for each image in a custom category view:

    Like so:

    foreach ($collection as $cat){
        $cur_category = Mage::getModel('catalog/category')->load($cat->getId());
        $_img = $cur_category->getImageUrl();
        //stuff 
    }
    

    This is giving me the original image, I would like to resize using Magento's built in resize function. But I'm a newb, and can't figure out how to get that code to work like the code on the product list page:

    $this->helper('catalog/image')->init($_product, 'small_image')->resize(306);
    

    How do I modify/use the original code to make it resize the images? Thanks!