Все хотят иметь красивый и функциональный интернет-магазин. Картинки являются их неотъемлемой частью.

Но в Magento нет встроенного функционала для ресайза изображений категорий. Не проблема, напишем сами!

Пишем функции для ресайза:



public function getResizedUrl($imgUrl, $x, $y = NULL) {
        if ($imgUrl == '') {
            return '';
        }
        $imgPath = $this->splitImageValue($imgUrl, "path");
        $imgName = $this->splitImageValue($imgUrl, "name");
        $imgPath = str_replace("/", DS, $imgPath);
        $imgPathFull = Mage::getBaseDir("media").DS.$imgPath.DS.$imgName;
        $widht = $x;
        $y ? $height = $y : $height = $x;
        $resizeFolder = $widht."X".$height;
        $imageResizedPath = Mage::getBaseDir("media").DS.$imgPath.DS.$resizeFolder.DS.$imgName;
        if (!file_exists($imageResizedPath) && file_exists($imgPathFull)) :
            $imageObj = new Varien_Image($imgPathFull);
            $imageObj->constrainOnly(TRUE);
            $imageObj->keepAspectRatio(TRUE);
            $imageObj->resize($widht, $height);
            $imageObj->save($imageResizedPath);
        endif;
        $imgUrl = str_replace(DS, "/", $imgPath);
        return Mage::getBaseUrl("media").$imgUrl."/".$resizeFolder."/".$imgName;
    }




public function splitImageValue($imageValue, $attr = "name") {
        $imArray = explode("/", $imageValue);
        $name = $imArray[count($imArray) - 1];
        $path = implode("/", array_diff($imArray, array($name)));
        if ($attr == "path") {
            return $path;
        }
        else
            return $name;
    }


Для работы можно сделать простой модуль, и вложить эту функцию в его Helper:

Простой пример - в list.phtml вставляем:



<?php
$_catimg2 = Mage::getModel('catalog/category')->load($child3->getId())->getImageUrl();
$_catimg = str_replace($baseurl, '', $_catimg2);
echo '<img src="'.Mage::helper('mygentomodule')->getResizedUrl($_catimg, 180, 222) ."/>';
?>


Готово.

Желающие могут скачать готовый модуль ресайза картинок категорий в Magento

Здесь вы можете приобрести готовые модули Magento для России