/**
* js图片等比缩放
* @param {type} ImgD 图片对象,或id
* @param {type} FitWidth 标准宽
* @param {type} FitHeight 标准高
* @param $type=true 等比内压缩 false 等比外压缩
*/
function DrawImage(ImgD, FitWidth, FitHeight, Type) {
Type = (typeof Type == 'undefined') ? true : Type;
ImgD = (typeof ImgD) == 'object' ? ImgD : document.getElementById(ImgD);
var image = new Image();
image.src = ImgD.src;
if (image.width > 0 && image.height > 0) {
var w_bili = image.width / FitWidth;
var h_bili = image.height / FitHeight;
if ((w_bili >= h_bili && Type == true) || (w_bili < h_bili && Type == false)) {
ImgD.width = FitWidth;
ImgD.height = image.height / w_bili;
} else {
ImgD.width = image.width / h_bili;
ImgD.height = FitHeight;
}
}
}
/**
* js图片等比缩放并自动居中
* @param {type} ImgD 图片对象,或id
* @param {type} FitWidth 标准宽
* @param {type} FitHeight 标准高
* @param type true 等比内压缩 false 等比外压缩
* @returns {undefined}
*/
function DrawImage(ImgD, FitWidth, FitHeight, Type) {
Type = (typeof Type == 'undefined') ? true : Type;
ImgD = ((typeof ImgD) == 'object') ? ImgD : document.getElementById(ImgD);
var image = new Image();
image.src = ImgD.src;
if (image.width > 0 && image.height > 0) {
var w_bili = image.width / FitWidth;
var h_bili = image.height / FitHeight;
ImgD.style.padding = '0px';
if ((w_bili >= h_bili && Type == true) || (w_bili < h_bili && Type == false)) {
ImgD.width = FitWidth;
var height = image.height / w_bili;
var padding = Math.floor((FitHeight - height) / 2);
ImgD.height = Math.floor(height);
ImgD.style.paddingTop = padding + 'px';
ImgD.style.paddingBottom = padding + 'px';
} else {
var width = image.width / h_bili;
var padding = Math.floor((FitWidth - width) / 2);
ImgD.width = Math.floor(width);
ImgD.height = FitHeight;
ImgD.style.paddingLeft = padding + 'px';
ImgD.style.paddingRight = padding + 'px';
}
}
}