Skip to content

Instantly share code, notes, and snippets.

@zj8487
Forked from neekey/img-resize.js
Created July 19, 2014 08:01
Show Gist options
  • Save zj8487/d18cdb1305d0825e3411 to your computer and use it in GitHub Desktop.
Save zj8487/d18cdb1305d0825e3411 to your computer and use it in GitHub Desktop.
/**
* 根据容器宽高和当前图片宽高确定适应宽高
* @param conW 容器宽度
* @param conH 容器高度
* @param imgW 图片宽度
* @param imgH 图片高度
* @returns {{w: *, h: *}}
*/
getImgResize: function( conW, conH, imgW, imgH ){
if( imgW <= conW && imgH <= conH ){
return {
w: imgW,
h: imgH
};
}
else {
var width = imgW * 1.0;
var height = imgH * 1.0;
if(( width / conW ) > ( height / conH )){
var new_width = conW;
var new_height = parseInt( conW * height / width, 0);
}
else {
new_width = parseInt( conH * width / height, 0 );
new_height = conH;
}
return {
w: new_width,
h: new_height
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment