0

当我使用drawImage缩放一个大图像(比如5M)时,结果看起来很糟糕,有没有更简单的方法可以用抗锯齿缩放图像?MDN提到了这一点:

注意:放大时图像可能会变得模糊,如果缩小太多,图像会变得有颗粒感。如果您有一些需要保持清晰的文本,则最好不要进行缩放。

EIDT:我想将图像缩放到 90x90,我的代码是这样的:

that.avatar_height >= that.avatar_width ?
that.context.drawImage($(this)[0], 86, 2, 90, that.avatar_height*90/that.avatar_width) :
that.context.drawImage($(this)[0], 86, 2, that.avatar_width*90/that.avatar_height, 90);

avatar 是要缩放的图像。

4

1 回答 1

1

While scaling the image first get the uploaded image aspect ratio.

/* Calculating aspect ratio */
var imageAspectRatio = imgWidth / imgHeight;

/* Give the needed width and height of the image*/
/*For example you need like this */
var newWidth = 1024;
var newHeight = 768;

if( imgWidth <= 1024 ) {
    var newWidth =  newHeight * imageAspectRatio;
} else if(imgHeight <= 768) {
    var newHeight = newWidth / imageAspectRatio;
} else if( imgWidth >= newWidth ) {
    var newWidth =  newHeight * imageAspectRatio;
} else if(imgHeight >= newHeight) {
    var newHeight = newWidth / imageAspectRatio;
}

If you do this you can't loss you aspect ratio so scaling looks good

于 2012-10-09T05:04:25.797 回答