If you need to force images to fit within your web page layout you really only have two options:

  • Resize images on the server.
  • Resize images on the client.

Resize images on the server

If you have the resources, resizing on the server is probably the best solution depending on your requirements. This solution requires server-side software to manipulate the images and store the altered image for future reference. For Ruby, the RMagick library does an excellent job with this. However, manipulating images on the server is a very expensive operation. If you are on a shared server, like I am, this may not be an option for you. But don’t worry, there is an alternative below.

Resize images on the client

The other option for resizing images involves using Javascript on the client-side to resize them on the fly. This solution has two drawbacks:

  • It requires Javascript.
  • It requires downloading the full image, whereas the server-side resizing will result in much smaller image sizes.

If these two drawbacks are ok with you, then read on :)

To resize the images, you need to first create a javascript function to do the resizing. Feel free to use this one that I wrote:

function resize(which, max) {
  var elem = document.getElementById(which);
  if (elem == undefined || elem == null) return false;
  if (max == undefined) max = 100;
  if (elem.width > elem.height) {
    if (elem.width > max) elem.width = max;
  } else {
    if (elem.height > max) elem.height = max;
  }
}

This function has been tested successfully against IE 6 and Firefox 1.5 (if you can verify this against others, please let me know and I will update this list). One thing worth mentioning is that only the longest side of the image needs to be resized to fit your maximum size. The aspect ratio will still be maintained by the web browser.

The resize function expects an image id and an optional maximum size. For this to work properly, the resizing must only occur after the image was loaded. This can be enforced by using the onload attribute of the image tag.

So, in pure HTML, this could be called like so:

<img id="some_image" onload="resize('some_image')" src="some_image.jpg" />

And in Ruby on Rails, this would look like:

<%= image_tag('some_image.jpg', {:id => “some_image”, :onload => “resize(’some_image’)”}) %>

Enjoy!

14 Responses to “Resize Images with Javascript”

  1. erhm lol Says:

    this function does not preserve aspectratio whatsoever

  2. tom Says:

    Re: this function does not preserve aspectratio whatsoever

    I did verify that this maintains the aspect ratio for FireFox 1.5 and Internet Explorer 6. If you let me know what browser are you using I will see if I can verify or at least update this post with a NOTE. The alternative is of course to just compute the explicit width and height yourself. I have done this in the past so if you need help just let me konw.

  3. PHP code needed - Computer Forums Says:

    […] Re: PHP code needed JavaScript, not Java. They are two unrelated, different things. I don’t use JavaScript because I don’t like it. However, a quick Google search yielded these results: Dynamically Resizing Images - WebReference.com atom thoughts

  4. Max Kim Tobiasen Says:

    Or you could just us css to resize your images - seems like a much simpler solution.

  5. tom Says:

    Hi Max,

    I could be mistaken but won’t that just reduce the visible area of the image? With the Javascript approach, it will actually dynamically shrink the image to the new dimensions.

    Tom

  6. Max Kim Tobiasen Says:

    Well Tom..

    Yes - you are absolutely right on that one. But I don’t see much difference between resizing the actual image using javascript, and just applying some simple CSS to deal with the problem. Not from a user perspective anyway.

    Max

  7. Scott Says:

    Is there a way to get an image to resize when I click a thumbnail of it, to enlarge the image next to a list of thumbnails. I want the size of the enlarged image to be different than the images actual size though. I can get the images full size to appear, but not a smaller size.

    i.e. I click a thumbnail and the 1588×2080 image shows up and not a 300×400.

    This is my part of code I’m stuck on. The height and width properties I have are doing nothing.

    Thanks
    Scott

  8. Scott Says:

    For some reason the code didn’t show up. Here it is:

  9. Scott Says:

  10. Scott Says:

    sorry I can not seem to figure out how to put in my code

    div id=”loadarea” style=”width: 400px; height: 300px”>/div>

  11. naveen Says:

    good code keep it up

  12. naveenreddy Says:

    Hi, Tom
    As part of your code, image resize is working fine
    in this link
    http://www.eklog.com/naveen.html

    when ever i click sign up button the new page will be opened, that is http://www.eklog.com/mainpage_calendar/signup.aspx?n=&m=
    upto fine,
    in this page if i click the home button, the link is passing to the fallowing link http://www.eklog.com/naveen.html

    but images or not resized when i click the home page link, this is working fine in firefox 2.0 but not internet explorer 6.0 and above. how to resolve this problem, this is very urgent for me, please give me a solution.
    Naveen Reddy

  13. Thanassis Says:

    If you need to change the image keeping its proportions replace the resize code with this:

    function thumb_resize(which, max) {
    var elem = document.getElementById(which);

    if (elem == undefined || elem == null) return false;
    var orig_width = elem.width;
    var orig_height = elem.height;

    if (max == undefined) max = 150;
    if (elem.width > elem.height) {
    if (elem.width > max) { elem.width = max; elem.height = orig_height*(max/orig_width);}
    } else {
    if (elem.height > max) { elem.height = max; elem.width = orig_width*(max/orig_height);};
    }
    }

  14. naveen reddy Says:

    hi,
    Thanassis,
    as part of your suggestion i tried with your code, its not given any solution what ever i mentioned in previous links, when ever i click the browser back button(after click in any link) my images are not resized , please check the once again.
    the link is http://www.eklog.com/naveen.html
    thanking you
    naveen reddy

Leave a Reply