Skip to content

Instantly share code, notes, and snippets.

@rodrigopolo
Created February 10, 2015 05:54
Show Gist options
  • Save rodrigopolo/62c21b25a23d34fee6a0 to your computer and use it in GitHub Desktop.
Save rodrigopolo/62c21b25a23d34fee6a0 to your computer and use it in GitHub Desktop.
Calc image resize dimensions in JS
function calcResize(op){
var ratio = Math[(op.fill)?'max':'min'](op.to.width/op.from.width,op.to.height/op.from.height);
var r = {
width: Math.round(op.from.width * ratio),
height: Math.round(op.from.height * ratio)
}
r.x = Math.round((op.to.width - r.width) / 2);
r.y = Math.round((op.to.height - r.height) / 2);
return r;
}
console.log(calcResize({
from: {
width: 240,
height: 320
},
to: {
width: 320,
height: 240
},
fill: true
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment