简体   繁体   English

使用HTML Canvas + Javascript优化和图像处理+减少文件大小

[英]Using HTML Canvas + Javascript to optimize and image + reduce filesize

I am making a Chrome extension which automatically takes a screenshot of the visible tab and download a JPG to your filesystem. 我正在制作一个Chrome扩展程序,该程序会自动拍摄可见标签的屏幕截图,然后将JPG下载到您的文件系统中。 That all works fine but the images are very large (~400kb for a 1280x720px image) and I want to optimize them (ideally around 40kb). 一切正常,但图像非常大(对于1280x720px图像,约为400kb),我想对其进行优化(理想情况下约为40kb)。

Here is the code I'm using: 这是我正在使用的代码:

var image = new Image();
    image.onload = function() {
        var canvas = screenshot.content;
        canvas.width = image.width;
        canvas.height = image.height;
        var context = canvas.getContext("2d");
        context.drawImage(image, 0, 0);

        // save the image
        var link = document.createElement('a');
        link.download = shotname + ".jpg";
        link.href = screenshot.content.toDataURL();
        link.click();
        screenshot.data = '';
    };
    image.src = screenshot.data; 

How can I optimize the image to reduce quality and filesize? 如何优化图像以降低质量和缩小文件大小? I'd like to play around with different quality options so I can see what an acceptable use-case is. 我想尝试不同的质量选项,以便了解什么是可以接受的用例。

You can use JPEG and a quality setting like this: 您可以使用JPEG和如下质量设置:

link.href = screenshot.content.toDataURL("image/jpeg", 0.3);

The quality is in the range [0, 1]. 质量在[0,1]范围内。 If you don't specify any image type, the default will be PNG, which is relative large in many cases. 如果您未指定任何图像类型,则默认值为PNG,在许多情况下该尺寸相对较大。

Just be aware of that you will loose the alpha-channel when using JPEG. 请注意,使用JPEG时,您将失去alpha通道。

Additionally you can blur the image slightly (see f.ex. my realtime-blur or stack-box blur ). 另外,您可以稍微模糊图像(请参阅f.ex.我的实时模糊堆栈框模糊 )。 Blurring filters some of the high frequencies that are hard to compress. 模糊会滤除一些难以压缩的高频。

You could encode the url in jpg format. 您可以将网址编码为jpg格式。

The second parameter specifies the quality of the resulting jpg. 第二个参数指定生成的jpg的质量。

link.href = screenshot.content.toDataURL('image/jpeg', 0.6);

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

相关问题 Javascript / HTML5使用图像填充画布 - Javascript/HTML5 using image to fill canvas 使用html画布,使用JavaScript和php时图像渲染模糊 - image rendering blurry when using html to canvas, using JavaScript and php 性能:Html5 Canvas / Javascript - 使用 canvas.drawImage 与 canvas.style 移动背景图像 - Performance: Html5 Canvas / Javascript - Moving the background image using canvas.drawImage vs canvas.style 使用JavaScript绘制到HTML5画布时调整.png图像的大小 - Resizing a .png image when drawing to HTML5 canvas using JavaScript 无法使用 HTML 和 JavaScript 显示完整的图像绘制画布 - Could not display the full image draw canvas using HTML and JavaScript 使用面向对象的JavaScript的HTML5画布旋转图像 - HTML5 canvas rotating image using object oriented JavaScript HTML画布使用javascript旋转文本而不移动背景图像 - HTML Canvas using javascript to rotate text without moving the background image 使用javascript在html画布中添加图像不起作用 - adding image inside html canvas using javascript not working 使用JavaScript调整HTML画布的大小 - Resizing html canvas using javascript 想要优化代码以捕获的图像调整大小并使用html5画布旋转,现在可在移动设备上发出内存不足警告 - Want to optimize code for captured image resize and rotate using canvas of html5, which now giving low memory warning on mobile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM