Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery resizing image

Tags:

jquery

image

I'd like to start a discussion about the image resizing using jQuery.

That's my contribution: But I think I'm far away from the solution. What about the cropping? Who can help me?

$(document).ready(function() {     $('.story-small img').each(function() {     var maxWidth = 100; // Max width for the image     var maxHeight = 100;    // Max height for the image     var ratio = 0;  // Used for aspect ratio     var width = $(this).width();    // Current image width     var height = $(this).height();  // Current image height      // Check if the current width is larger than the max     if(width > maxWidth){         ratio = maxWidth / width;   // get ratio for scaling image         $(this).css("width", maxWidth); // Set new width         $(this).css("height", height * ratio);  // Scale height based on ratio         height = height * ratio;    // Reset height to match scaled image     }      // Check if current height is larger than max     if(height > maxHeight){         ratio = maxHeight / height; // get ratio for scaling image         $(this).css("height", maxHeight);   // Set new height         $(this).css("width", width * ratio);    // Scale width based on ratio         width = width * ratio;    // Reset width to match scaled image     } }); 

});

like image 317
michele Avatar asked Jul 17 '09 14:07

michele


People also ask

How to resize Images in jQuery?

Resizing an image using jQuery is easily done by using the css() method. We can target the width and height properties using the css() method and use that to resize our image. If we want to resize the image to be a set width and height, we can use the jQuery css() method in the following jQuery code.

What is resize () function in jQuery?

jQuery resize() MethodThe resize event occurs when the browser window changes size. The resize() method triggers the resize event, or attaches a function to run when a resize event occurs.

Can you resize an image in JavaScript?

Image resizing in JavaScript - Using canvas element. The HTML <canvas> element is used to draw graphics, on the fly, via JavaScript. Resizing images in browser using canvas is relatively simple. drawImage function allows us to render and scale images on canvas element.


1 Answers

You need to recalculate width and height after first condition. Here is the code of entire script:

$(document).ready(function() {     $('.story-small img').each(function() {     var maxWidth = 100; // Max width for the image     var maxHeight = 100;    // Max height for the image     var ratio = 0;  // Used for aspect ratio     var width = $(this).width();    // Current image width     var height = $(this).height();  // Current image height      // Check if the current width is larger than the max     if(width > maxWidth){         ratio = maxWidth / width;   // get ratio for scaling image         $(this).css("width", maxWidth); // Set new width         $(this).css("height", height * ratio);  // Scale height based on ratio         height = height * ratio;    // Reset height to match scaled image     }      var width = $(this).width();    // Current image width     var height = $(this).height();  // Current image height      // Check if current height is larger than max     if(height > maxHeight){         ratio = maxHeight / height; // get ratio for scaling image         $(this).css("height", maxHeight);   // Set new height         $(this).css("width", width * ratio);    // Scale width based on ratio         width = width * ratio;    // Reset width to match scaled image     } }); 
like image 187
Aleksandar Janković Avatar answered Sep 20 '22 14:09

Aleksandar Janković