Jump to content

image resizing on the fly


justAnoob

Recommended Posts

I found this little script online

What I'm looking to do is associate this function with a div that I have. Why, well, I have thumbnails that the user can rollover and the full size image is displayed in that div. That's great and all but when there are pictures that are in a portrait format, the pic shows but it throws the rest of my page off wack due to the height. So I'm looking to mouse over the thumbnails and have a max width and height of 380 while keeping the original aspect ratio of the picture. (I'm sure I could set a max height/width on the upload script, but this way seems easier)  Can this be done.

function ChangePhoto(newPhoto) {
img = document.getElementById('swapme');
img.src='images/'+newPhoto;
var width = img.naturalWidth;
var height = img.naturalHeight;
var newx = 350; // set to whatever you want the images max width to be.
var newy = 350; // whatever you want the images maximum height to be.
if ( width >= height ) {
// landscape
var tmpy = height*newx/width;
if ( tmpy <= newy )
newy = tmpy;
else
newx = width*newy/height;
}
else {
// portrait
tmpx = width*newy/height;
if ( tmpx <= newx )
newx = tmpx;
else
newy = height*newx/width;
}
img.width = newx;
img.height = newy;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.