justAnoob Posted January 5, 2010 Share Posted January 5, 2010 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 More sharing options...
justAnoob Posted January 5, 2010 Author Share Posted January 5, 2010 Decided to go with the upload script change. Link to comment https://forums.phpfreaks.com/topic/187325-image-resizing-on-the-fly/#findComment-989216 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.