简体   繁体   English

如何使用gulp-imagemin +插件获得最佳的PNG压缩?

[英]How do I get the best PNG compression with gulp-imagemin + plugins?

I am trying to get the best setup possible for compressing pngs. 我正在努力获得压缩png的最佳设置。

I have found there are some plugins available here for imagemin: https://www.npmjs.com/browse/keyword/imageminplugin 我发现这里有一些用于imagemin的插件: https ://www.npmjs.com/browse/keyword/imageminplugin

I have tried all the PNG options but the compression rates appear to be pretty bad. 我已经尝试了所有的PNG选项,但压缩率看起来非常糟糕。

pngquant appears to have the highest compression rate, (obviously the worst quality) similar quality to tinypng, but could still get nearer the tinypng figure. pngquant似乎具有最高的压缩率,(显然质量最差)与tinypng相似的质量,但仍然可以更接近tinypng数字。

Essentially I have 3 questions: 基本上我有3个问题:

Question 1: Changing the options for both advpng & optipng doesn't seem to alter the filesize, here is an example, am I using this correctly? 问题1:更改advpng和optipng的选项似乎没有改变文件大小,这是一个例子,我正确使用它吗? I am guessing that maybe it isn't using my settings at all and falling back to the default settings? 我猜可能根本就没有使用我的设置并回到默认设置? If you notice the filesizes of these two are identical!: 如果您注意到这两个文件大小相同!:

.pipe(imagemin(
    imageminOptipng({
        optimizationLevel: 4
    })
))

Question 2: Am I using "pngout" in correctly? 问题2:我正确使用“pngout”吗? Is there a method to use it that I do not know about? 有没有一种方法可以使用我不知道的方法? The examples from their page seem not to work & neither does this method: 他们页面中的示例似乎不起作用,此方法也不起作用:

.pipe(imagemin([
    imageminPngout({
        strategy: 1
    })
]))

Question 3: Is there a better method to deal with png compression that I haven't yet found? 问题3:有没有更好的方法来处理我还没有找到的png压缩? Ideally I would somehow like to get a method that has the rate of pngquant, but the quality is a bit better. 理想情况下,我会想要一种具有pngquant率的方法,但质量要好一些。

After a long process of trials and errors, I've managed to get the imagemin-pngquant plugin to produce the image of the similarly small size as TinyPNG.com. 经过漫长的试验和错误过程后,我设法获得了imagemin-pngquant插件,以生成与TinyPNG.com类似的小尺寸图像。 Still a little bigger, but some parts of the image were looking better than the TinyPNG.com result. 仍然有点大,但图像的某些部分看起来比TinyPNG.com结果更好。

Input image size: 1.1MB 输入图像大小:1.1MB
TinyPNG.com: 223kb TinyPNG.com:223kb
imagemin-pngquant: 251kb imagemin-pngquant:251kb

I used the next plugin settings: 我使用了下一个插件设置:

{
    quality: '70-90', // When used more then 70 the image wasn't saved
    speed: 1, // The lowest speed of optimization with the highest quality
    floyd: 1 // Controls level of dithering (0 = none, 1 = full).
}

Also, it may be a good idea to generate .webp images from .png and serve it to the browsers that support this format with the .png as a backup. 此外,从.png生成.webp图像并将其提供给支持此格式的浏览器并以.png作为备份可能是个好主意。 More info about .webp image format: Google Developer Section 有关.webp图片格式的更多信息: Google Developer Section

After generating .webp from the optimized image (with the minimal quality loss) the image size was 62kB. 从优化图像生成.webp(质量损失最小)后,图像大小为62kB。 You can use the imagemin-webp plugin with gulp-rename to stream images to the dist folder with the same base name, but different extension. 您可以使用带有gulp-rename的imagemin-webp插件将图像流式传输到具有相同基本名称但扩展名不同的dist文件夹。 Example gulp task: 示例gulp任务:

const config = require('./src/gulp/config.json');

const gulp = require('gulp');
const plugins = require('gulp-load-plugins')();
const imageminWebp = require('imagemin-webp');

gulp.task('images', function () {
    return gulp.src(config.src.images)
        pipe(plugins.imagemin([imageminWebp({
            method: 6,
        })]))
        .pipe(plugins.rename({ extname: '.webp' }))
        .pipe(gulp.dest(config.dist.images));
});

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

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