Resizing and cropping images with jquery
Sometimes, we need images with fixed dimensions (carousels, photo albums …), but all images don’t have the good dimensions or good ratio height /width.
This little jquery script will help you for this usecase.
In this example we will fix the dimension and position of thumbnails placed in blocks using the class « imageContainer ». « imageContainer » have a fixed width and height (in this example 250px/150px). The size of thumbs will be increased or reduced, and the thumb will be moved inside its container to get a centered cropping. Of course images and its containers must have a relative position.
Your html template code looks like :
<a class="imageContainer" href="the_link">
<img src="the_image_src" alt="" width="450" height="300" />
</a>
<a class="imageContainer" href="another_link">
<img src="another_image_src" alt="" width="200" height="100" />
</a>
In your css, you must fix at least :
.imageContainer,
.imageContainer img {
position: relative;
}
.imageContainer {
width: 250px;
height: 150px;
overflow: hidden;
border: 1px solid grey;
}
The javascript code is simple :
var canImproveResolution = false;
thumbResize = function(thumb, min_width, min_height, orientation) {
twidth = jQuery(thumb).width();
theight = jQuery(thumb).height();
new_height = theight;
new_width = twidth;
// strange 1px bug on MSIE
if (jQuery.browser.msie) {
min_width = min_width+1;
min_height = min_height+1;
}
//calculate the good size
if (orientation=='landscape') {
ratio = min_width/min_height;
tratio = twidth/theight;
if (tratio>ratio) {
new_height = min_height;
new_width = parseInt(new_height*tratio);
}
else {
new_width = min_width;
new_height = parseInt(new_width/tratio);
}
}
else {
ratio = min_height/min_width;
tratio = theight/twidth;
if (tratio>ratio) {
new_width = min_width;
new_height = parseInt(new_width*tratio);
}
else {
new_height = min_height;
new_width = parseInt(new_height/tratio);
}
}
// resize thumb
if (theight!=new_height || twidth!=new_width) {
jQuery(thumb).height(new_height);
jQuery(thumb).width(new_width);
}
// adjust position (vertical and horizontal centering for css cropping)
if (new_width > min_width) {
moveleft = parseInt((new_width-min_width)/2);
jQuery(thumb).css('left', '-'+moveleft+'px');
}
if (new_height > min_height) {
movetop = parseInt((new_height-min_height)/2);
jQuery(thumb).css('top', '-'+movetop+'px');
}
// improve resolution
if (canImproveResolution && (new_width > twidth || new_height > theight)) improveResolution(thumb);
}
resizeThumbs = function(thumbs, min_width, min_height) {
orientation = (min_height > min_width) ? 'portrait' : 'landscape';
// hide images before resizing to avoid bad visual effect
thumbs.each( function() {
jQuery(this).css('visibility', 'hidden');
})
// use window.load because on document.ready images are not always loaded
jQuery(window).load(function() {
thumbs.each( function() {
thumbResize(this, min_width, min_height, orientation );
jQuery(this).css('visibility', 'visible');
})
});
}
// change image src for a better resolution
// this is just an example for Plone, adapt it with
// your own sizes rules
improveResolution = function(img) {
img_src = img.src;
img_src = img_src.replace(/\/image_preview/gi, '/image');
img_src = img_src.replace(/\/image_mini/gi, '/image_preview');
img_src = img_src.replace(/\/image_thumb/gi, '/image_mini');
img.src = img_src;
}
For Plone users, there is a small improvement, if you fix the variable :
canImproveResolution = true;
you will get a better resolution when increasing image size (works only with standard plone image thumb sizes …).
At last launch the script for the wanted thumbnails :
jQuery(document).ready(function(){
resizeThumbs(jQuery('.imageContainer img'), 250, 150)});




