Skip to content

Instantly share code, notes, and snippets.

@navinpai
Forked from codepo8/resizer.js
Created February 1, 2012 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save navinpai/1717683 to your computer and use it in GitHub Desktop.
Save navinpai/1717683 to your computer and use it in GitHub Desktop.
Resize image to thumbnail size
function resize( imagewidth, imageheight, thumbwidth, thumbheight ) {
var w = 0, h = 0, x = 0, y = 0,
widthratio = imagewidth / thumbwidth,
heightratio = imageheight / thumbheight,
maxratio = Math.max( widthratio, heightratio );
if ( maxratio > 1 ) {
w = imagewidth / maxratio;
h = imageheight / maxratio;
} else {
w = imagewidth;
h = imageheight;
}
x = ( thumbwidth - w ) / 2;
y = ( thumbheight - h ) / 2;
return { w:w, h:h, x:x, y:y };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment