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
https://forums.phpfreaks.com/topic/187325-image-resizing-on-the-fly/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.