Skip to content

Instantly share code, notes, and snippets.

@ryaan-anthony
Created January 28, 2014 20:21
Show Gist options
  • Select an option

  • Save ryaan-anthony/8675526 to your computer and use it in GitHub Desktop.

Select an option

Save ryaan-anthony/8675526 to your computer and use it in GitHub Desktop.
<?php
class Ip_Category_Model_Image
{
public function getResizedImage($image, $width, $height = null, $quality = 100)
{
if(!$image){return false;}
$imageUrl = Mage::getBaseDir('media').DS."catalog".DS."category".DS.$image;
if(!is_file($imageUrl)){return false;}
$imageResized = Mage::getBaseDir('media').DS."catalog".DS."category".DS."cache".DS."cat_resized".DS.$image;
if(!file_exists($imageResized) &&
file_exists($imageUrl) ||
file_exists($imageUrl) &&
filemtime($imageUrl) > filemtime($imageResized)){
$imageObj = new Varien_Image($imageUrl);
$imageObj->constrainOnly(true);
$imageObj->keepAspectRatio(true);
$imageObj->keepFrame(true);
$imageObj->quality($quality);
$imageObj->backgroundColor(array(255, 255, 255));
$imageObj->resize($width,$height);
$imageObj->save($imageResized);
}
if(file_exists($imageResized)){
return Mage::getBaseUrl('media')."catalog/category/cache/cat_resized/".$image;
}else{
return $imageUrl;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment