gulp-image不会压缩JPG

时间:2018-01-04 12:14:13

标签: gulp compression jpeg

gulp-image正在压缩png文件而没有任何缺陷,但是当谈到jpg格式时,它会保持原样。如果它有任何帮助,这里是我的配置:

var gulp = require('gulp');
var image = require("gulp-image");
var imagemin = require('gulp-imagemin');
var jpegRecompress = require("imagemin-jpeg-recompress");
var mozjpeg = require("imagemin-mozjpeg");
var jpegoptim = require('jpegoptim-bin');


// Optimization tasks
// -------------------

gulp.task("images:minify", function () {
    return gulp.src("images_src/**/*")
        .pipe(imagemin([imageminMozJpeg()], {
            verbose: true
        }))
        .pipe(gulp.dest("images_src/compressed"));
});

gulp.task("images:compress", function () {
    gulp.src("images_src/**/*.*")
    .pipe(image({
        jpegRecompress: true,
        jpegoptim: false,
        mozjpeg: true,
        concurrent: 10
    }))
    .pipe(gulp.dest("tmp"));
})

And here's what the terminal outputs when I run `gulp-image`:
[14:57:29] Using gulpfile ~/code/udacity-responsive-images/p1-my-solution/gulpfile.js
[14:57:29] Starting 'images:compress'...
[14:57:29] Finished 'images:compress' after 23 ms
[14:57:32] ✔ fixed/smiley_face.png -> before=3.8 KB after=478 B reduced=3.34 KB(87.7%)
[14:57:33] - horses.jpg -> Cannot improve upon 149.26 KB
[14:57:34] - still_life.jpg -> Cannot improve upon 81.13 KB
[14:57:35] - grasshopper.jpg -> Cannot improve upon 234.97 KB
[14:57:38] - rosella.jpg -> Cannot improve upon 676.67 KB
[14:57:39] - postcard.jpg -> Cannot improve upon 486.93 KB
[14:57:39] - cockatoos.jpg -> Cannot improve upon 485.34 KB
[14:57:39] - volt.jpg -> Cannot improve upon 522.92 KB
[14:57:39] - sfo.jpg -> Cannot improve upon 492.9 KB
[14:57:42] ✔ fixed/flourish.png -> before=43.43 KB after=15.42 KB reduced=28 KB(64.5%)

你能帮我吗?

1 个答案:

答案 0 :(得分:0)

我认为没有任何错误。各种jpeg压缩算法具有各种设置,您可以在其中调整压缩量。 jpeg-recompress的默认设置为:

['--strip', '--quality', 'medium', '--min', 40, '--max', 80]

参数描述为:

--quality               Set a quality preset: low, medium, high, veryhigh
--min                   Minimum JPEG quality
--max                   Maximum JPEG quality
--strip                 Strip metadata

如果指定不同的质量,最小值和最大值,您将得到不同的结果。如果gulp-image没有检测到给定设置的任何压缩(即输入大小等于输出大小),它将为您提供无法改进消息。默认设置为:

C:\a>gulp images:compress
[16:52:38] Using gulpfile ~\a\gulpfile.js
[16:52:38] Starting 'images:compress'...
[16:52:38] Finished 'images:compress' after 6.49 ms
[16:52:39] - cockatoos.jpg -> Cannot improve upon 485.34 KB

这些设置:

gulp.task("images:compress", function () {
  gulp.src("images/**/*.*")
    .pipe(image({
      jpegRecompress: ['--strip', '--quality', 'medium', '--min', 1, '--max', 2],
      jpegoptim: false,
      mozjpeg: true,
      concurrent: 10,
    }))
    .pipe(gulp.dest("tmp"));
});

给出以下结果:

C:\a>gulp images:compress
[16:52:46] Using gulpfile ~\a\gulpfile.js
[16:52:46] Starting 'images:compress'...
[16:52:46] Finished 'images:compress' after 6.4 ms
[16:52:48] ✔ cockatoos.jpg -> before=485.34 KB after=84.93 KB reduced=400.42 KB(82.5%)

现在凤头鹦鹉看起来很糟糕,但我想你可以到处寻找体面的设置。看起来gulp-image可以正常工作。

Cockatoos