Javascript Resize Image Proportionally and Clipping

Setting height and width to an image is a general scenario in web development. We need to take in account that image shouldn’t get stretched, it should be resized based on aspect ratio. So I created a javascript helper which resizes the image proportionally based on the traget-height and target-width instead of setting the height and width directly. Also in some case we need to resize the image proportionally from center and clip the remaining area that goes out of the target-height and width. 

Here you find the demo http://jsfiddle.net/sesubash/Jq2Rv/2/

Here you go for the source. https://github.com/sesubash/Javascript-Image-Resizer

Usage – html

<img id="myImg" src="url.jpg" onLoad="resizeImage(this)">
<img id="clipImage" src="url.jpg" onLoad="clipImg(this)">

JavaScript

function resizeImage(img){
    ImageResizer.fixImage(img,215,217); 
}

function clipImg(img){
    ImageResizer.clipImage(img,215,217); 
}

Hope this helps for some one.

Cheers!

 

Related Posts

Angularjs directive to show loader for image

Here you go for the directive to show loader for images in your angularjs application. Inject the module “sesu.imageloader” in your main app module and start using…

jQuery plugin for expanding search bar

I have created a jQuery plugin to handle expand and close functionality of searchbar. This would help some to minimize their work, since it is straight forward…

Angularjs fallback image directive

Using fallback image with <img> tag is common scenario to show a default image if the original image is not loaded. In angular you can achieve this…

This Post Has 7 Comments

  1. Hi there, I found your website by way of Google whilst searching for a similar matter, your site came up, it seems great.

    I’ve bookmarked it in my google bookmarks.
    Hi there, just was aware of your blog thru Google,
    and found that it is really informative. I am going to watch out for brussels.

    I’ll be grateful if you continue this in future.
    Many people will be benefited from your writing.
    Cheers!

Leave a Reply to Margie Cancel reply

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