tags:

views:

12815

answers:

8

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
    }
});

});

+1  A: 

Take a look at Jcrop. I use it and it's very good.

http://deepliquid.com/content/Jcrop.html

FWH
+3  A: 

A couple of suggestions:

  • Make this a function where you can pass in a max or min size, rather than hard-coding it; that will make it more reusable
  • If you use jQuery's .animate method, like .animate({width: maxWidth}), it should scale the other dimension for you automatically.
Nathan Long
Using the jQuery .animate method only lets you scale by one dimension. In other words, I can't set a maxWidth and maxHeight.
davekaro
@davekaro - from the docs: "We can, for example, simultaneously animate the width and height..." http://api.jquery.com/animate/
Nathan Long
A: 

Great Start. Here's what I came up with:

$('img.resize').each(function(){
    $(this).load(function(){
     var maxWidth = $(this).width(); // Max width for the image
     var maxHeight = $(this).height(); // Max height for the image
     $(this).css("width", "auto").css("height", "auto"); // Remove existing CSS
     $(this).removeAttr("width").removeAttr("height"); // Remove HTML attributes
     var width = $(this).width(); // Current image width
     var height = $(this).height();  // Current image height

     if(width > height) {
      // Check if the current width is larger than the max
      if(width > maxWidth){
       var 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
      }
     } else {
      // Check if current height is larger than max
      if(height > maxHeight){
       var 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
      }
     }
    });
});
Tyler
A: 

There are a few plugins that can accomplish this:

http://plugins.jquery.com/taxonomy/term/2532

hugegoudaface
A: 

Cool, your code looks alot like

http://hirdet2.extra.hu/kepresize/

That link is no good (or appears to be no good because it redirects to some weird foreign-language page).
PythonUser
Some common sense dictates that whatever page this person is referring to didn't pay his/her hosting and now has a default domain-registration page. I only speak/read/write 2 languages fluently, and I program in several. Some effort is required but it's not impossible to figure out what's going on so I take offense to "weird foreign language page"
AcidRaZor
A: 

I was looking for a code with cropping and ended up putting my own together. Here's the code: http://www.guybedford.com/?p=31

+1  A: 

The script is very helpful. But doesnt work in IE. Please help me.

D123