Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize image from URL in Magento

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!

like image 787
Randy Hall Avatar asked Dec 14 '12 03:12

Randy Hall


2 Answers

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;
    }
}
like image 55
user487772 Avatar answered Oct 17 '22 02:10

user487772


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;
        }
}
like image 22
Lehlabile Alladyce Kekana Avatar answered Oct 17 '22 01:10

Lehlabile Alladyce Kekana