Let me start off by saying, I LOVE jQuery! It’s the greatest thing since sliced bread! Now that I have that out of the way, I’ve been working on developing some Wordpress themes for work. We are going to be launching a blogs site (using Wordpress MU) fairly soon for faculty, staff, and eventually students. So, in the process we are building a blog that will be used as an online newsletter. On the blog page, they want the top story to take precedence, and for the image to be larger. So to accomplish this, I wanted to scale down the other images and keep the aspect ratio. Enter jQuery…
I was looking out there at some of the solutions already completed. I didn’t see anything that quite did what I was looking for. So I made my own function:
$(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 width = width * ratio; // Reset width 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 } }); });
The 2nd line of code says to grab every image with a class of ’story-small’. The block for the height is just a safety net. I have it there in case the height is still larger than the max. There are a hundred ways to skin a cat, so I’m sure there are plenty of other ways to do this.
Pingback: jQuery Image Resize | Wordpress News
#1 by Mikko on July 15, 2009 - 3:50 am
Quote
You probably should set width in first if-statement like you do with height:
width = maxWidth;
Otherwise it calculates new width wrong in second if.
#2 by Eric Juden on July 15, 2009 - 7:45 am
Quote
Thanks Mikko, I overlooked that.
#3 by Alexandr Kryklia on August 27, 2009 - 3:57 pm
Quote
tnx!
#4 by Takitani on September 20, 2009 - 5:51 am
Quote
Very good, thx for sharing
#5 by Kelly Andrews on October 1, 2009 - 9:02 pm
Quote
Thanks – totally bailed me out!
#6 by khan on October 3, 2009 - 9:43 pm
Quote
You are just modifying the image properties. Can someone tell me if I can actually do some image manipulation on the client side (JQuery) before it gets uploaded to the server?
Say someone is uploading 2MB 2500×2500 image, I want to resize it to 500×500 and then send the modified (smaller) file to the server rather than full 2MB? Or it is too much to ask from javascript?
#7 by Eric Juden on October 7, 2009 - 10:36 am
Quote
khan,
I think you’d be better off using a server-side language like php or asp.net to process the images when the files are being uploaded.
Eric
#8 by sylbal on October 12, 2009 - 3:18 am
Quote
Hi,
I start from this post to create a resize image function but I have problem with safari,
var width = $(this).width(); // Current image width
var height = $(this).height(); // Current image height
returns 0
I try to find a work around then keep you posted
#9 by Cedric on November 2, 2009 - 12:24 pm
Quote
Hi,
I’m sorry to be so bad but this is not working for me.
I’ve downloaded jquery 1.3.2 on jquery.com
I insert it like that
script src=”jquery-1.3.2.js” type=”text/javascript”
I paste your code exactly in my page.
I have only that:
img src=”dff30__N1D0212.JPG” class=”story-small”
and nothing has changed.
Can you please help me. I need so baddly this to work.
Thanks
Cedric
#10 by Eric Juden on November 2, 2009 - 12:41 pm
Quote
Cedric,
Can you send a link to the page you are doing this on? Do you have a javascript debugger that is showing any errors?
Thanks,
Eric
#11 by Cedric on November 2, 2009 - 1:06 pm
Quote
Hi,
thnaks for your quick answer
http://em-bassadors.com/mini/
Thank you.
Cedric
cedric.massonnat@gmail.com
#12 by Eric Juden on November 2, 2009 - 1:36 pm
Quote
Cedric,
Try putting your image inside a div with a class of “story-small”. The css was looking for an element with a class of “story-small” with an image inside of it.
Eric
#13 by Cedric on November 2, 2009 - 1:48 pm
Quote
Thank you so much !!
I don’t know why I put a class inside the img.
Thanks.
BTW, you have a very nice blog, very interesting.
Take care
#14 by Ricardo Cavalcante on December 2, 2009 - 11:08 am
Quote
Thanks a lot! this script is lake Druce Lee, simple and effective.
#15 by Michael on December 13, 2009 - 9:48 am
Quote
Hey Eric,
i have a question to your Script.
Am i right, that this script only changes the size of the div and doesnt resize the picture itself?
Greetings,
Michael
#16 by Eric Juden on December 13, 2009 - 10:42 pm
Quote
Michael,
You are not correct. The script is resizing the image, and not a div. The 2nd line of the script says (’.story-small img’). That is grabbing any image tag inside of a div with a class of story-small.
Thanks,
Eric
#17 by Michael on December 15, 2009 - 12:52 pm
Quote
Ah nice thanks for the Information, now i know how to use it.
Greetings,
Michael
#18 by Jules Van de Velde on December 17, 2009 - 5:20 pm
Quote
To select any img with a class of story-small you could use (’img.story-small’) if you didn’t want to put each image inside of a div
#19 by joren on December 23, 2009 - 10:16 am
Quote
Hi,
This is a very handy script, but I’m having some problems with it.
I’m browsing with Safari, Chrome and Firefox on a mac and the first time I visit a page on my website the images arn’t resized. If I reload the page, they resize.
I think my browser doesn’t know the size of the images when the document is loaded.
this is an example of my website I’m developing:
http://fileflambe.be/topics/117-raad-de-screenshot-game-editie?page=7
Is there a solution for this problem?
#20 by Eric Juden on December 23, 2009 - 10:50 am
Quote
Joren,
When I view the link you posted it shows the images full size until the page is done loading. Then the image is scaled down. I’m not sure how to fix this besides making your page load faster so it’s not as noticeable.
Thanks,
Eric
#21 by Jared on December 23, 2009 - 11:02 am
Quote
Or try preloading the image….
#22 by joren on December 23, 2009 - 11:14 am
Quote
weird,
I have the same problem on this page
http://fileflambe.be/posts/1798-google-auto-aanvullen
the first time the image stay big.
If it is only me who is seeing them big, there is noproblem
#23 by Eric Juden on December 26, 2009 - 11:22 am
Quote
Joren,
I think you’d probably be better of doing this on the server side, instead of using Javascript. Your images are pretty large and it’s taking the page longer to load.
Eric