Skip to content

Magento : Resize Any Image URL

but first, i have tell something, the image must stored in “Media” directory  :D,
ok let we start, let say you have an image url like this

http://your-website-url.com/media/catalog/product/b/f/bflo_bflo_A1_140613_M_Website-0229_1.jpg

function resizeImg($fileName, $width, $height = ”,$cachedDir = ‘resized_image’)
{
$folderURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
$imageURL = $folderURL . $fileName;

$fileName = str_replace($folderURL,”,$fileName);

$basePath = Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS . $fileName;
$newPath = Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS . $cachedDir . DS . $fileName;
if ($width != ”) {
if (file_exists($basePath) && is_file($basePath) && !file_exists($newPath)) {
$imageObj = new Varien_Image($basePath);

$imageObj->constrainOnly(true);
$imageObj->keepAspectRatio(true);
$imageObj->keepFrame(false);
$imageObj->keepTransparency(true);
$imageObj->resize($width, $height);
$imageObj->save($newPath);
}
$resizedURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . $cachedDir . DS . $fileName;
} else {
$resizedURL = $imageURL;
}
return $resizedURL;
}

 

#example

 

$imageurl = ‘http://your-website-url.com/media/catalog/product/b/f/bflo_bflo_A1_140613_M_Website-0229_1.jpg’;

 

echo resizeImg($img2nd, 160,155);

 

#result : ‘http://your-website-url.com/media/resized_image/catalog/product/b/f/bflo_bflo_A1_140613_M_Website-0229_1.jpg

Share

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.