我正在使用客户端图像调整大小(使用 HTML5 画布)制作和图像上传页面。一切正常,但问题是图像质量不是很好。
这是一个(正在进行的)照片库的链接,其中包含我的调整大小/上传代码生成的图像。
你明白我说的质量差是什么意思吗?许多锯齿状边缘,尤其是在缩略图上。关于如何解决这个问题的任何想法?
这是我用于生成缩略图的 javascript:
img.onload = function()
{
var canvasWidth = 150;
var canvasHeight = 150;
var Rz = resizeCanvasSmall(img,canvasID,canvasWidth,canvasHeight);
ctx.drawImage(img,Rz[0],Rz[1],Rz[2],Rz[3],Rz[4],Rz[5],Rz[6],Rz[7]);
dataurl = canvas.toDataURL("image/jpeg",0.8); // File type and quality (0.0->1.0)
UploadFile();
}
// Function to resize canvas (for thumbnail images)
// img = image object, canvas = canvas element ID
function resizeCanvasSmall(img,canvas,width,height)
{
var sx; //The x coordinate where to start clipping
var sy; //The y coordinate where to start clipping
var swidth; //The width of the clipped image
var sheight; //The height of the clipped image
var aspectRatio = width / height;
if (img.width > img.height) // If landscape
{
sheight = img.height;
swidth = img.height * aspectRatio;
sy = 0;
sx = (img.width - swidth) / 2;
}
else //If portrait
{
swidth = img.width;
sheight = img.width / aspectRatio;
sx = 0;
sy = (img.height - sheight) / 2;
}
document.getElementById(canvas).width = width;
document.getElementById(canvas).height = height;
return [sx,sy,swidth,sheight,0,0,width,height];
}