Image Resize in Magento

<?php 
/* Image */
      $image_width = 216;
      $image_height = 109;
   $fileName = $post['image'];
   
    if(!file_exists("./media/blog/resized/".$image_width."x".$image_height))     mkdir("./media/blog/resized/".$image_width."x".$image_height,0777);
  
  $folderURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA)."blog/";
    $imageURL = $folderURL . $fileName;
   
 
    $basePath = Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS ."blog". DS . $fileName;
   $newPath = Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS ."blog". DS . "resized" . DS .$image_width."x".$image_height. DS . $fileName;
   
    //if width empty then return original size image's URL
   
        //if image has already resized then just return URL
        if (file_exists($basePath) && is_file($basePath) && !file_exists($newPath)) {
            
            $imageObj = new Varien_Image($basePath);
            $imageObj->constrainOnly(TRUE);
            $imageObj->keepAspectRatio(FALSE);
            $imageObj->keepFrame(TRUE);
            $imageObj->backgroundColor(array(255,255,255));
            $imageObj->resize($image_width, $image_height);
            $imageObj->save($newPath);
        }
      $resizedURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . "blog/resized" . '/' .$image_width."x".$image_height."/". $fileName;
?>

 <p><a href="<?php echo $post->getAddress(); ?>" ><img src="<?php echo $resizedURL; ?>" /></a></p>
Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: