jQuery Image Resize Plugin

Written by on January 2, 2010 in JavaScript, Projects - 35 Comments

Few days ago i wrote jQuery Plugin Tutorial, today i want to give away my first open source jQuery plugin: image resizer. I made sure that it’s compatible with all major browsers: IE6+, FF3, Opera10, Chrome (didn’t check with Safari, hope someone can do it for me).

The whole idea was to make it as simple as possible to resize image to given size. The common problem developers face is to resize image to (for example) no more then 150px width or no more then 75px height and because getting real image size is not as simple as it seems i think you will find this plugin useful.

About JavaScript Image Resizing

Before you download and start using it, keep in mind that depending on what you are trying to accomplish this plugin might not be the best solution for you. If you are trying to save some bandwidth by resizing image then this plugin won’t help you, because resizing is done on the client side, which means that server is sending to client image in the original size.

Image Resize Plugin Usage

Using plugin is as simple as applying it to the img tags:

$(".shrink-me").resize({maxHeight: 150});

This code will resize all images with class “shrink-me” to no more then 150px height.

Plugin accepts three options
1. maxHeight: maximal image height
2. maxWidth: maximal image width
3. scale: (default size=1) scales image.

You can execute plugin with just one or with all params given (actually you can even execute it without any parameter but it won’t do anything). If you execute plugin with all 3 parameters given then, first image will be scaled, then if maxWidth or maxHeight is exceeded image is resized further.

For example if you want to shrink all images to 50% of their original size, but no more then 100px width or 150px height you can write:

$("img").resize({
    scale: 0.5, // 0.5 = 50%
    maxWidth: 100,
    maxHeight: 150
});

Few IMPORTANT things to consider

1. Resizing is done by adding to <img /> tag attributes: height and width. If you need to add them to css as well then you need to do it manually

2. That should go without saying, but just for the record: you can use resize plugin only on <img /> tags.

Download

Download: jQuery image resizer.

Please post your: suggestions, feedback, bugs in comments. Thanks!

About the Author

Greg Winiarski is a freelance PHP and JavaScript programmer. He specializes in web applications and WordPress development.

35 Comments on "jQuery Image Resize Plugin"

  1. syringe filters February 3, 2010 at 10:09 am ·

    I know little about what you write. Is it about program?

  2. Ta Duy Duc February 18, 2010 at 4:44 pm ·

    Hey Greg. after dizzying all around by this problem on my gallery page design. I found this really useful (I haven’t finish implement it yet) but I’m sure it’ll solve my problem here quickly… :)

    Thanks Greg

  3. serge February 28, 2010 at 9:22 am ·

    Hi Greg, useful plugin. I’m really interested in resizing some images for my new widget ( articles similaires – will display similar posts and their thumbnails in the sidebar widget, much lighter than similar posts) – and you see, there is the code php to resize images.
    Uses all three parameters as yours but a cache as well to save a bandwidth. Will mail you the final code as soon as it’s done . You might do something more with that resizer.
    best regards,
    serge

  4. Harshal April 28, 2010 at 5:16 am ·

    Hi Greg,

    Thanks for this, this was very useful. I have used this and modified it a bit to add a tooltip on any resized images with a link to display the original image.

    I have submitted my modified plugin to JQuery plugins. I hope you don’t mind. I have put a link to this post in there saying that I am using the code from you.

    http://plugins.jquery.com/project/ImageScale

  5. Greg Winiarski April 28, 2010 at 3:42 pm ·

    Hi @Harshal, sure i don’t mind, thanks for linking back to the blog.

  6. Alex May 7, 2010 at 6:12 pm ·

    Thank you very much, this code works very well.

    ONe thing you’d may like to include is the microsoft property:

    img {
    -ms-interpolation-mode : bicubic;
    }

    This will make IE7 render it smoothly!

  7. Greg Winiarski May 7, 2010 at 6:52 pm ·

    @Alex thanks for the tip.

  8. marc-andre menard May 9, 2010 at 2:06 pm ·

    I always like resizing plug in.. but the thing that is ALWAYS missing is a little sharpen.. in php it’s simple to do, can you implement it in JavaScript ?

  9. jng May 19, 2010 at 9:21 am ·

    a nice one. very useful. thanx

  10. Marco May 21, 2010 at 2:56 pm ·

    with Safari doesn’t work…

    best regards Marco

  11. Christian June 24, 2010 at 6:23 pm ·

    Hi Greg,

    enjoyed your tutorial very much and have customized my gallery using this. Was wondering if it is ok to include an image gallery I have made that includes this plugin in a template selling on themeforest.net?

    Best,

    Christian

  12. Greg Winiarski June 25, 2010 at 9:24 pm ·

    Christian, sure, no problem you can use it anywhere you want, of course i always appreciate a link back to this post, but if not than that’s fine too.

  13. Ümit July 1, 2010 at 2:38 pm ·

    Hi i have an problem with your plugin.

    I’m taking image url’s from DB in codebehind and showing in a div.

    $(document).ready(function() {
    $(“body img”).imageScale({ maxWidth: 700, maxHeight: 700 });
    });

    I write that code after DIV createad.

    when image loading its not resize image.
    But after i refresh page its resize image bec image is loaded before.

  14. Doug July 22, 2010 at 8:54 am ·

    i started to use this. very useful.

  15. design web london September 8, 2010 at 1:39 pm ·

    Any online example please ?

  16. Devi September 18, 2010 at 12:01 pm ·

    Hi Greg, great script!

    I have modified a bit if don’t mind.

    please check this link
    http://sandbox.devi.web.id/resizeImg

    tested under Windows XP with:
    - Firefox 3.6.10
    - Safari 5.0.1
    - IE6
    - Opera 10.61
    - Google Chrome 6.0.472.59

  17. lucas December 2, 2010 at 9:24 am ·

    Hi Greg.what a great code!

    How to do if I want trigger the event when the brower’s size is changed? Thanks.

    Best Regards

  18. Greg Winiarski December 3, 2010 at 6:02 pm ·

    @lucas try:

    $(window).resize(function() {
    $(“.shrink-me”).resize({maxHeight: 150});
    });

  19. Hussain December 11, 2010 at 6:49 pm ·

    This is one of the best resize jQuery plugin I have tested so far… Thank you for the good work…

  20. Stepan January 17, 2011 at 5:09 pm ·

    Hi, I’d like to use your plugin on my website, but I can’t find what licence are you using. This is for me quite an issue so if you could included some licence or terms of use it would be great.
    Thanks.

  21. Greg Winiarski January 17, 2011 at 6:31 pm ·

    @Stepan, i do not have any license for it, but feel free to use it in any commercial project without attribution.

    So, that’s probably MIT license :)

  22. Stepan January 22, 2011 at 11:04 am ·

    Thanks, you were helplful :) .

  23. Eric January 28, 2011 at 8:58 pm ·

    Can’t make it work, why don’t you put a demo or the full code ? I’m a total noob when I load the plugin and i add the following code to my page nothing happens :
    $(document).ready(function () {
    $(“.my_div_with_img_to_resize”).resize({maxHeight: 150});
    }

  24. Greg Winiarski January 29, 2011 at 6:53 pm ·

    @Eric, aside of adding the code you need to load the plugin http://ditio.net/wp-content/uploads/2010/01/jquery-resize.js in HEAD section of your HTML document.

  25. Phil February 1, 2011 at 11:29 am ·

    Great script Greg – thanks a lot!

    Just thought I would add this in-case anyone else is having the same problem:
    I found none of my images would appear. On inspecting the generated code they were having their width and height set to 0. I ran it down to the DOM loading before the images, so they didn’t have naturalWidth/Height when the script looked. I solved the problem by moving the initialiser into a window.load call instead of document.ready. Now working like a charm.

    There may be a better way around this, but it worked for me.

  26. Greg Winiarski February 2, 2011 at 6:51 pm ·

    @Phil great, thanks for the comment!

  27. Anoop February 5, 2011 at 10:15 am ·

    I am trying using this script but since I am also using other jquery plugins that uses Prototype.js so I receive the error

    $(“.profile_img_210″) is null

    Could any one please guide how to overcome this issue.

  28. Tomislav Pocedulic February 23, 2011 at 9:24 pm ·

    Finally something that works perfect. Thank you

  29. Anand_inovvorx April 12, 2011 at 8:46 am ·

    thanks for post image Resize, i very helpfull for me.

  30. andi September 1, 2011 at 11:50 am ·

    Hi
    it give me an error:

    TypeError: Result of expression ‘this.tagName’ [undefined] is not an object.

  31. kf mulcahy September 13, 2011 at 10:03 am ·

    Hi Greg,

    Any ideas why the class has some positioning issues? I’ve looked in your .js code and found nothing that even mentions display type or positioning. Here are the problems I’ve run into and I’ve tried numerous CSS attributes to try to fix these issues.

    Here is the HTML that does scale my image:

    The wrapper “projectImg” displays as a ‘block-inline’ next to a left vertical nav bar.
    I’m trying to create a thumbnail navigation that sits just below the scaling image.
    I’ve tried numerous ways of nesting the main image and the thumbnail nav into a wrapper div but something about the scaling code is whigging it out.

    Any ideas why I am having trouble

    1) nesting the first wrapper inside another wrapper?
    2) when only nested in one wrapper, the positioning is very strange i.e. it pushes it way down the page.
    3) I cannot seem to control any div that sits below the scaling div. Everything seems to get forced to the left margin wheras the same div without the scaling code behaves just fine.

    It’s as if it is acting like it has absolute positioning – but the main reason I wanted to use your code is so I could place a div relative to the scaling div.

    Any help would be greatly appreciated!
    Best,
    -k

  32. Joscasam October 4, 2011 at 10:17 am ·

    Hi everybody…

    This plugin is great. It´s easy integrate to my web project; but I have one question. In Safari it doesn´t load the image. Somebody could help me with that?

    Thank so much and excuse my english.

    A lot of regards from Spain

  33. Joscasan October 4, 2011 at 10:19 am ·

    Hey don´t answer my last post. Finally I get it. I can see the image on Safari.

    Bye…

  34. Binu October 27, 2011 at 3:06 pm ·

    Hi.. Greg, thanks for your post. But when resized a large image it get blurred. What i need to do? Is there any other plugin needed to install??

  35. Josh Currier December 9, 2011 at 11:01 pm ·

    Great little plugin, man. Just worked it into a WP header file for a friend and it’s resizing post images beautifully.

Leave a Comment