Skip to main content

Get width height of image using Varien-image class in Magento

You can get width height of image using Varien_Image (lib/Varien/Image.php) class object. In this example, I assume that your image is present inside media directory of your magento installation. $imagePath = YOUR_IMAGE_PATH_LINK; // like http://example.com/media/myfolder/myimage.jpg // changing url link of image into directory link $dirImg = Mage::getBaseDir().str_replace("/",DS,strstr($imagePath,'/media')); // getting image width and height if (file_exists($dirImg)) { $imageObj = new Varien_Image($dirImg); $width = $imageObj->getOriginalWidth(); $height = $imageObj->getOriginalHeight(); echo $width." x ".$height; } else { echo "File doesn't exist."; }



Resize Image


<?php echo $this->helper('catalog/image')->init($_product'image')
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepFrame(FALSE)
->resize(600,null)
?>
Fixed height of 600px
<?php echo $this->helper('catalog/image')->init($_product'image')
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepFrame(FALSE)
->resize(null,600)
?>
The following code will resize image proportionally and not let the image be greater than height and width specified.
<?php echo $this->helper('catalog/image')->init($_product'image')
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepFrame(FALSE)
->resize(400,400)
?>
Description of the functions used above:-
constrainOnly(bool $flag)
Guarantee, that image picture will not be bigger, than it was. Applicable before calling resize() It is false by default
keepAspectRatio(bool $flag) 
Guarantee, that image picture width/height will not be distorted. Applicable before calling resize() It is true by default.
keepFrame(bool $flag) 
Guarantee, that image will have dimensions, set in $width/$height. Applicable before calling resize() Not applicable, if keepAspectRatio(false).
You can resize image with Varien_Image class as well. This is most suitable when you are resizing image that is not related with catalog product.
// actual path of image
$imageUrl = Mage::getBaseDir('media').DS."myimage".DS.$post->getThumbnail();
// path of the resized image to be saved
// here, the resized image is saved in media/resized folder
$imageResized = Mage::getBaseDir('media').DS."myimage".DS."resized".DS.$post->getThumbnail();
// resize image only if the image file exists and the resized image file doesn't exist
// the image is resized proportionally with the width/height 135px
if (!file_exists($imageResized)&&file_exists($_imageUrl)) :
    $imageObj new Varien_Image($_imageUrl);
    $imageObj->constrainOnly(TRUE);
    $imageObj->keepAspectRatio(TRUE);
    $imageObj->keepFrame(FALSE);
    $imageObj->resize(135, 135);
    $imageObj->save($imageResized);
endif;
You can use the resized image now as:
<img src="<?php echo Mage::getBaseUrl('media')."myimage/resized/".$post->getThumbnail() ?>" />
Another Scenario
Suppose, you have an image link. Now, you want to resize it. The image might be from any place. In this example, I am supposing the image link to be like http://localhost/magento/media/catalog/category/test_image.jpg
Now, I have to resize it. To do so, I will create a directory called resized inside media/catalog/category. And, I will save the resized file into the newly created resized directory.
// my sample image
// create folder
 if(!file_exists("./media/catalog/category/resized"))     mkdir("./media/catalog/category/resized",0777);
 // get image name
 $imageName substr(strrchr($imageUrl,"/"),1);
 // resized image path (media/catalog/category/resized/IMAGE_NAME)
 $imageResized = Mage::getBaseDir('media').DS."catalog".DS."category".DS."resized".DS.$imageName;
 // changing image url into direct path
 $dirImg = Mage::getBaseDir().str_replace("/",DS,strstr($imageUrl,'/media'));
 // if resized image doesn't exist, save the resized image to the resized directory
 if (!file_exists($imageResized)&&file_exists($dirImg)) :
 $imageObj new Varien_Image($dirImg);
 $imageObj->constrainOnly(TRUE);
 $imageObj->keepAspectRatio(TRUE);
 $imageObj->keepFrame(FALSE);
 $imageObj->resize(120, 120);
 $imageObj->save($imageResized);
 endif;
 $newImageUrl = Mage::getBaseUrl('media')."catalog/category/resized/".$imageName;
Displaying newly created resized image.
<img src="<?php echo Mage::getBaseUrl('media')."catalog/category/resized/".$newImageUrl ?>" />
You can check other function for Varien_Image at
http://docs.magentocommerce.com/Varien/elementindex_Varien_Image.html
http://blog.chapagain.com.np/

Comments

Popular posts from this blog

Parsing Domain Name From URL In PHP

To get Domain name from the url, we can use parse_url() php function. This would filter the domain name from the given url. $domain = str_ireplace ( 'www.' , '' , parse_url ( $url , PHP_URL_HOST )); This would return the google.com for both http://google.com and http://www.google.com

get directory paths, get Base URLs, get URLs in static block, get secure URL in Magento

1) Get Directory paths  Mage::getBaseDir()  //output : /var/www/html/magento Mage::getBaseDir('app')  //output : /var/www/html/magento/app Mage::getBaseDir('media') //output : /var/www/html/magento/media Mage::getBaseDir(‘design’) => Get design directory path Mage::getBaseDir(‘code’) => Gives code directory file path Mage::getBaseDir(‘lib’) => Gives lib directory file path Mage::getBaseDir(‘skin’) => Gives skin directory file path Mage::getBaseDir(‘var’) => Gives var directory file path Mage::getBaseDir(‘cache’) => Gives cache directory file path Mage::getBaseDir(‘log’) => Gives log directory file path       2) Get Base URL  Mage::getBaseUrl() => Get base url path e.g. http://yourwebsite.com/  Mage::getBaseUrl('media') => Get MEDIA folder path e.g. http://yourwebsite.com/media/  Mage::getBaseUrl('js') => Get JS folder path e.g. http://yourwebsite.com/js/  Mage::getBaseUrl('skin') => Get...

How to add tab in Customer Information in Magento Admin

Step 1) Override the file /app/code/core/Mage/Adminhtml/Block/Customer/Edit/Tabs.php, Inside _beforeToHtml() method, add the following code: $ this -> addTab ( 'Custom' , array (      'label'   = > Mage :: helper ( 'customer' ) -> __ ( 'Custom' ) ,      'class'   = >     'ajax' ,      'url'     = >     $ this -> getUrl ( '*/*/custom' , array ( '_current' = > true ) ) , ) ) ; Step 2)  Override the file /app/code/core/Mage/Adminhtml/controllers/CustomerController.php, Add the following code: public function customAction() { $this->_initCustomer(); $this->getResponse()->setBody( Mage::app()->getLayout()->createBlock('core/template')->setTemplate('custom/customer/tab/custom.phtml')->setCustomerId(Mage::registry('current_customer')->getId())         ->setUseAjax(true)-...