简体   繁体   English

HTML5 Canvas调整大小

[英]Html5 Canvas resize

Am using canvas to resize image at the client side and then upload that image to the server. 我正在使用画布在客户端调整图像大小,然后将该图像上载到服务器。 My intention was to resize the image to half of its original dimensions and wanted to see a decrease in the file size. 我的意图是将图像调整为原始尺寸的一半,并希望减小文件大小。 But the resized image size is always greater than the original. 但是调整大小后的图像大小始终大于原始大小。

For example, 例如,

I was resizing a 2.5 MB JPEG image file with dimensions 2592 x 1936 to 1296 x 968 (JPEG file) and tried to upload it to the server. 我正在调整大小为2592 x 1936至1296 x 968(JPEG文件)的2.5 MB JPEG图像文件的大小,并尝试将其上传到服务器。 On the server I see that the file is saved with the 3.5 MB size. 在服务器上,我看到该文件保存为3.5 MB。

Is there something that we can do while resizing with html5 canvas so that if we are decreasing the dimensions we could expect some decrease in the file size like compression. 在调整html5 canvas的大小时是否可以做些什么,以便减小尺寸,可以预期文件大小会有所减少,例如压缩。

If you are using toDataURL, you can set the second argument to a number between 0.0 and 1.0. 如果使用toDataURL,则可以将第二个参数设置为0.0到1.0之间的数字。 This is the JPEG quality level, with 1.0 meaning very high quality and a very large image and nearer to 0.0 meaning lower quality smaller image. 这是JPEG质量等级,其中1.0表示非常高质量和非常大的图像,而接近0.0则意味着较低质量的小图像。

So, for example, you could try: 因此,例如,您可以尝试:

 data = downsizedCanvas.toDataURL("image/jpeg", 0.2);

Here's a description in the w3c reference: www.w3.org: toDataURL 这是w3c参考中的描述: www.w3.org:toDataURL

It looks like there has been a bug in Firefox to do with ignoring the quality, so you might need to test in some modern browsers. 似乎Firefox中有一个错误与忽略质量有关,因此您可能需要在某些现代浏览器中进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM