- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使用imagemin
无损优化 JPEG/PNG 图像,但我在使用扩展时遇到问题,它们“去优化”,这意味着结果图像更大比原来的。我怎样才能避免这种情况?
这是我的Gruntfile.js
...
grunt.initConfig({
imagemin: {
jpeg: {
options: {
//by default it uses imageminJpegtran
progressive: true,//default false, progressive JPEG is better compression https://superuser.com/questions/463477/progressive-jpeg-quality-compare-to-normal-jpeg
arithmetic: false,//true breaks image
//don't know how to use it loseless without making the images even bigger
// use: [
// imageminMozjpeg({
// quality: 95, //Compression quality, in range 0 (worst) to 100 (perfect).
// dcScanOpt: 2,//2 Optimize between one scan for all components and one scan for 1st component plus one scan for remaining components
// arithmetic: false// false, or it breaks the image
// }),
// ],
},
files: [{
expand: true,
cwd: '/www',
src: ['**/*.{jpg,jpeg,JPG,JPEG}'],
dest: '/www',
filter: 'isFile'
}]
}
}
});
...
最佳答案
您在问题的标题中找到了答案——“grunt-contrib-imagemin”
。如果你用谷歌搜索它,你会发现 this page on GitHub 用于 Grunt.js 作为官方 Grunt GitHub 站点。 在这个页面上,所有的描述都非常详细。
例如:
对于 imagemin OptiPNG 插件:
optimizationLevel
(It is for PNG (for OptiPNG plugin) and NOT FOR JPG!)
Type:number
Default:3
Select optimization level between0
and7
.The optimization level
0
enables a set of optimization operations that require minimal effort. There will be no changes to image attributes like bit depth or color type, and no recompression of existingIDAT
datastreams. The optimization level1
enables a singleIDAT
compression trial. The trial chosen is whatOptiPNG
thinks it’s probably the most effective. The optimization levels2
and higher enable multipleIDAT
compression trials; the higher the level, the more trials.Level and trials:
1
trial8
trials16
trials24
trials48
trials120
trials240
trials
不要尝试对 JPG 使用 optimizationLevel
!对于 JPG 和 PNG,您会发现不同的优化选项。 此优化级别选项的所有类型都在插件网站上有完整描述(请参阅下面的插件列表)。
关于 this site 你有很多用于不同图像类型优化的插件。在您的情况下,它们是 JPG 和 PNG(WebP、Gif 和 SVG 也有):
JPG 优化插件:
PNG 优化插件:
所有 PNG 插件都有不同的优化级别选项。例如,对于 AdvPNG 插件,您有一个 optimizationLevel
作为选项,您可以在 0
和 4
之间选择一个优化级别:
0
Don't compress
1
Compress fast (zlib)
2
Compress normal (7z)
3
Compress extra (7z)
4
Compress extreme (zopfli)
JPG优化
对于 JPG 优化,我想推荐 mozjpeg
和 jpegoptim
插件,因为它们有很多配置选项——请参阅上面该插件的链接。
插件使用示例
const imagemin = require('imagemin');
const jpegoptim = require('imagemin-jpegoptim');
//const mozjpeg = require('imagemin-mozjpeg');
const pngquant = require('imagemin-pngquant');
(async () => {
const files = await imagemin(['images/*.{jpg,png}'], 'build/images', {
plugins: [
//jpegoptim plugin for JPG
jpegoptim({
progressive: true, //Lossless conversion to progressive.
max: 80 //try to set with and without this option
//see other options on plugin site (link above) and take / add which you need
}),
/* As comment because on plugin page is nothing about lossless conversion
// mozjpeg plugin for JPG
mozjpeg({
quality: 80
//see other options on plugin site (link above) and take / add which you need
}),*/
//pngquant plugin for PNG
pngquant({
quality: [0.7, 0.8]
//see other options on plugin site (link above) and take / add which you need
})
]
});
console.log(files);
//=> [{data: <Buffer 89 50 4e …>, path: 'build/images/foo.jpg'}, …]
})();
对于 PNG 无损转换,也可以尝试不带任何选项地使用 AdvPNG 插件。 AdvPNG 插件的 optimizationLevel
选项默认设置为 3
(从 0
到 4
)。
进阶阅读
阅读文章很重要:
关于javascript - 如何使用 grunt-contrib-imagemin 的插件无损优化图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54199359/
我只想在我保存的单个文件上运行coffee lint和coffee compile。我的项目中有成百上千个CoffeeScript文件,而编译它们都需要太多时间。 这是我的Gruntfile: mod
我正在使用 grunt-contrib-compass 来处理我的 .SCSS 文件 compass: { dist: { options: { cssDir: 'www/st
不幸的是,grunt-contrib-watch和 grunt-contrib-connect看起来不太好。 关于 grunt-contrib-connect readme它说: Note that
我是 nodeJS 和 grunt 的新手。我在这个项目中有这个 Gruntfile,我想对我项目中的所有 html 文件进行实时重新加载,这样我就不必一直刷新浏览器来检测新的更改。不知何故,我遇到了
我已经根据这些下载并构建了open cv open cv docs . 我正在尝试编译这个 Eigenfaces demo ,我收到以下错误。 fatal error: 'opencv2/contri
我有两个目录 src 和 compiled。我想使用 Grunt Watch 确保从 src 到 compiled 的单向数据同步.作为中间步骤,我想编译 *.less 文件以及使用 ES6 语法编写
我创建了一个 lein 项目, 比使用 eclips 的 dev-dependency 更新它的 project.clj 我跑了“lein deps” 它下载了所有依赖项 但是在 my.m2/repo
我有一些在 RStudio 中完美执行的 R 代码,但是当我在 cmd 中运行它时,我收到以下错误。 Installing package into 'C:/Users/Anish/Documents
我正在尝试为上下文强盗问题 (https://medium.com/emergent-future/simple-reinforcement-learning-with-tensorflow-part
序曲: Ant-Contrib 为安装和启动提供了一个很好解释的 wiki 页面 ([ http://ant-contrib.sourceforge.net/#install][1] )。 事实上,只
当我尝试使用 Ant-contrib 的 FOR 循环时遇到以下错误: Invalid type class org.apache.tools.ant.taskdefs.optional.script
我正在使用DapperExtensions v4.0.30319,并且试图让Dapper.Contrib知道我的架构不是DBO。我提供了: public class EngineMapper : Cl
他们无论如何要直接生成缩小的 js 吗?我不想使用其他命令,例如 grunt uglify 因为源映射不会指向咖啡而是指向 js 最佳答案 我明白你对源 map 的看法,但你仍然可以在一个链中使用 u
我是整个 JVM 方面的新手,正在尝试使用 clojure。我正在尝试加载 clojure-contrib 但失败了: # in bash $ java -cp /path/to/clojure.ja
我正在使用 ant,并且在 if/then/else 任务(ant-contrib-1.0b3.jar)上遇到问题。 我正在运行一些可以使用下面的 build.xml 进行简化的东西。 我期待从 'a
我正在使用grunt-contrib-jasmine运行我的JavaScript规范。运行规范时如何将调试输出写入控制台,即如何获取 console.log("something"); 在控制台中显示
占用的问题 我运行以下命令来安装旧版本的opencv-contrib-python(4.0.0.21)。然后发生错误,无法安装此软件包。如何解决此错误? pip install opencv-cont
互联网上是否有可用的 Wicket Contrib Examples 的源代码- CD应用程序?谢谢。 最佳答案 您说您在对 Don 的回答的评论中查看了“svn”;哪个SVN?是这个吗? http:
我在 Maven 中使用 FindBugs,使用以下配置: org.codehaus.mojo findbugs-mave
我的构建脚本中有以下任务。 我从我的 Mac 机器运行这个构建脚本。它给出了以下错误。 Problem: failed to cr
我是一名优秀的程序员,十分优秀!