Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Custom code to resize any image in magento

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 373
    Comment on it

    In magento if we are required to resize the image we can do it using the resize() function of magento, but sometimes it wont work then we need to apply some custom codes.

     

    Lets see how can we do it :

     

    In case if we are working on a static blog and we are loading the categories with their thumbnail images in that case to display the images we need to resize the images with custom code.

     

    lets do it step by step: 

     

    First of all load the root category in most cases it would be the default category with id '2'.

     

    <?php
     $categories = Mage::getModel('catalog/category')->getCategories('2');
    ?>

     

    In the above statement we have called 'catalog/category' model to get the categories of the root category (i.e., of Default category). 

    Next step is to load the categories of the Defaul category in a loop one by one and from the "thumbnail" attribute of them extract the images and apply resize code on it.

    For the same follow the below code,

     

    <?php if(count($categories) > 0 ){
    
      foreach ($categories as $category) {
    
       $cat = Mage::getModel('catalog/category')->load($category->getId());
      
       if ($cat->getThumbnail()){
      
       $imageUrl = Mage::getBaseDir ( 'media' ). DS . 'catalog' . DS . 'category' . DS . $cat->getThumbnail();
       $imageResized = Mage::getBaseDir ( 'media' ) . DS . 'catalog' . DS . 'category' . DS . 'cache' . DS . 'cat_resized' . DS . $cat->getThumbnail();
      
            
       $imageObj = new Varien_Image($imageUrl);
              $imageObj->constrainOnly ( true );
              $imageObj->keepAspectRatio ( false );
              $imageObj->keepFrame( false );
              $imageObj->quality( 100 );
              $imageObj->resize( 350, 450 );
              $imageObj->save( $imageResized );
     }
    }
     ?>


    in the above code we first checked in if condition whether the root category has sub categories or not.
    Then if it contains, in foreach loop we loaded categories one by one. Afterwards in the loop at the statement 

     

    $cat = Mage::getModel('catalog/category')->load($category->getId());

     

    we are loading the category from the 'catalog/category' with the id and then in if condition we are checking for the attribute value $cat->getThumbnail().


    If it contains the value then using the thumbnail attribute we get the image from the path and assign it to the variable $imageUrl. In the parameter $imageResized we will load the path of the resize images where those resized images will get saved.

     

    Now, we created an object of the $imageUrl as $imageObj, then by using the same object we apllied the functions of resize properties of the image.


    In this statement $imageObj->resize( 350, 450 ); we passed the aspect ratio of the image as height and width. the aspect ratio is one in which we desire to resize the image.


    Now in the statement $imageObj->save( $imageResized ); we saved the resized image at the new path which we loaded in the parameter $imageResized.

    So, now whenever we are required to display the resized thumbnail image of that aspect ratio of the categories we can assign the path new path to load those images as,

     

    <img class="img-responsive" id="item_img" src="<?php echo Mage::getBaseUrl( 'media' ) . 'catalog' . DS . 'category' . DS . $cat->getThumbnail(); ?>" /> 

     

    Thats it, in the same way we can resize images let say of products or any.
     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: