Skip to content

Commit c557cb0

Browse files
authored
Merge pull request #28 from xiyu714/master
添加对webp格式的压缩与转换
2 parents bd1768c + 1e44101 commit c557cb0

File tree

8 files changed

+97
-7
lines changed

8 files changed

+97
-7
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
- [imagemin](https://github.com/imagemin/imagemin) +luban 算法,最大程度节省流量,默认选项
2424
- [imagemin](https://github.com/imagemin/imagemin) 压缩过程不需要经过网络,但是图片会有损耗
25+
- imagemin_webp 本地有损压缩,支持GIF格式有损压缩
26+
注意:有些图床(比如sm.ms)不支持webp图片格式,会上传失败
2527
- [tinypng](https://tinypng.com/) 无损压缩,需要上传到 tinypng
2628
- lubanforgitee 将所有图片压缩到1M以下,解决gitee的1M外链不能访问的问题.
2729

@@ -43,8 +45,8 @@ key 可选
4345

4446
## 压缩效果对比
4547

46-
| 类型 | tinypng | luban | imagemin |
47-
| ------ | ---------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
48-
| 原大小 | 1.5 MB | 1.5 MB | 1.5 MB |
49-
| 压缩后 | 315 KB | 276 KB | 411 KB |
50-
| 效果图 | ![](https://raw.githubusercontent.com/JuZiSang/picgo-plugin-compress/master/tests/tinypng.png) | ![](https://raw.githubusercontent.com/JuZiSang/picgo-plugin-compress/master/tests/luban.png) | ![](https://raw.githubusercontent.com/JuZiSang/picgo-plugin-compress/master/tests/imagemin.png) |
48+
| 类型 | tinypng | luban | imagemin | imagemin_webp |
49+
| ------ | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
50+
| 原大小 | 1.5 MB | 1.5 MB | 1.5 MB | 1.5 MB |
51+
| 压缩后 | 315 KB | 276 KB | 411 KB | 216 KB |
52+
| 效果图 | ![](https://raw.githubusercontent.com/JuZiSang/picgo-plugin-compress/master/tests/tinypng.png) | ![](https://raw.githubusercontent.com/JuZiSang/picgo-plugin-compress/master/tests/luban.png) | ![](https://raw.githubusercontent.com/JuZiSang/picgo-plugin-compress/master/tests/imagemin.png) | ![](https://raw.githubusercontent.com/JuZiSang/picgo-plugin-compress/master/tests/imagemin_webp.webp) |

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@
5959
"fs-extra": "^9.0.0",
6060
"image-size": "^0.8.3",
6161
"imagemin": "^7.0.1",
62+
"imagemin-gif2webp": "^3.0.0",
6263
"imagemin-gifsicle": "^7.0.0",
6364
"imagemin-jpegtran": "^7.0.0",
6465
"imagemin-mozjpeg": "^8.0.0",
6566
"imagemin-upng": "^2.0.2",
67+
"imagemin-webp": "^6.0.0",
6668
"images": "^3.2.3",
6769
"is-gif": "^3.0.0",
6870
"request": "^2.88.2"

src/compress/imagemin_webp.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import imagemin from 'imagemin'
2+
import imageminWebp from 'imagemin-webp'
3+
import imageminGif2webp from 'imagemin-gif2webp'
4+
import { CompressOptions, ImgInfo } from '../utils/interfaces'
5+
import { getImageBuffer } from '../utils/getImage'
6+
7+
export function imageminWebPCompress({ ctx, info }: CompressOptions): Promise<ImgInfo> {
8+
ctx.log.info('imagemin_webp 压缩开始')
9+
return getImageBuffer(ctx, info.url)
10+
.then((buffer) => {
11+
return imagemin.buffer(buffer, {
12+
plugins: [imageminWebp({ quality: 75 }), imageminGif2webp({ quality: 75, lossy: true })],
13+
})
14+
})
15+
.then((buffer) => {
16+
ctx.log.info('imagemin_webp 压缩完成')
17+
info.extname = 'webp'
18+
info.fileName = changeExt(info.fileName, 'webp')
19+
return {
20+
...info,
21+
buffer,
22+
}
23+
})
24+
}
25+
26+
// https://stackoverflow.com/questions/5953239/how-do-i-change-file-extension-with-javascript
27+
function changeExt(fileName: String, newExt: String) {
28+
var pos = fileName.includes('.') ? fileName.lastIndexOf('.') : fileName.length
29+
var fileRoot = fileName.substr(0, pos)
30+
return `${fileRoot}.${newExt}`
31+
}

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export enum NameType {
1010
export enum CompressType {
1111
tinypng = 'tinypng',
1212
imagemin = 'imagemin',
13+
imagemin_webp = 'imagemin_webp',
1314
luban = 'luban',
1415
lubangitee = 'lubangitee',
1516
}

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { PluginConfig } from 'picgo/dist/src/utils/interfaces'
55
import { tinypngCompress } from './compress/tinypngweb'
66
import { tinypngKeyCompress } from './compress/tinypng/index'
77
import { imageminCompress } from './compress/imagemin'
8+
import { imageminWebPCompress } from './compress/imagemin_webp'
89
import { NameType, CompressType } from './config'
910
import { reName } from './utils/reName'
1011
import { lubanCompress } from './compress/luban'
@@ -39,6 +40,8 @@ function handle(ctx: PicGo) {
3940
return lubanCompress(options)
4041
case CompressType.lubangitee:
4142
return lubanforgiteeCompress(options)
43+
case CompressType.imagemin_webp:
44+
return imageminWebPCompress(options)
4245
default:
4346
return lubanCompress(options)
4447
}

tests/imagemin_webp.webp

217 KB
Loading

types/lib.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
declare module 'imagemin-upng' {
22
export default function upng(): any
33
}
4+
5+
declare module 'imagemin-webp' {
6+
export default imageminWebp
7+
}
8+
9+
declare module 'imagemin-gif2webp' {
10+
export default imageminGif2webp
11+
}

yarn.lock

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ bin-version@^3.0.0:
511511
execa "^1.0.0"
512512
find-versions "^3.0.0"
513513

514-
bin-wrapper@^4.0.0:
514+
bin-wrapper@^4.0.0, bin-wrapper@^4.0.1:
515515
version "4.1.0"
516516
resolved "https://registry.npm.taobao.org/bin-wrapper/download/bin-wrapper-4.1.0.tgz#99348f2cf85031e3ef7efce7e5300aeaae960605"
517517
integrity sha1-mTSPLPhQMePvfvzn5TAK6q6WBgU=
@@ -971,6 +971,15 @@ currently-unhandled@^0.4.1:
971971
dependencies:
972972
array-find-index "^1.0.1"
973973

974+
cwebp-bin@^5.0.0:
975+
version "5.1.0"
976+
resolved "https://registry.npm.taobao.org/cwebp-bin/download/cwebp-bin-5.1.0.tgz#d5bea87c127358558e7bf7a90a6d440d42dcb074"
977+
integrity sha1-1b6ofBJzWFWOe/epCm1EDULcsHQ=
978+
dependencies:
979+
bin-build "^3.0.0"
980+
bin-wrapper "^4.0.1"
981+
logalot "^2.1.0"
982+
974983
dashdash@^1.12.0:
975984
version "1.14.1"
976985
resolved "https://registry.npm.taobao.org/dashdash/download/dashdash-1.14.1.tgz#853cfa0f7cbe2fed5de20326b8dd581035f6e2f0"
@@ -1697,7 +1706,7 @@ file-type@5.2.0, file-type@^5.2.0:
16971706
resolved "https://registry.npm.taobao.org/file-type/download/file-type-5.2.0.tgz#2ddbea7c73ffe36368dfae49dc338c058c2b8ad6"
16981707
integrity sha1-LdvqfHP/42No365J3DOMBYwritY=
16991708

1700-
file-type@^10.4.0:
1709+
file-type@^10.4.0, file-type@^10.5.0:
17011710
version "10.11.0"
17021711
resolved "https://registry.yarnpkg.com/file-type/-/file-type-10.11.0.tgz#2961d09e4675b9fb9a3ee6b69e9cd23f43fd1890"
17031712
integrity sha512-uzk64HRpUZyTGZtVuvrjP0FYxzQrBf4rojot6J65YMEbwBLB0CWm0CLojVpwpmFmxcE/lkvYICgfcGozbBq6rw==
@@ -1967,6 +1976,15 @@ getpass@^0.1.1:
19671976
dependencies:
19681977
assert-plus "^1.0.0"
19691978

1979+
gif2webp-bin@^3.0.0:
1980+
version "3.0.2"
1981+
resolved "https://registry.npm.taobao.org/gif2webp-bin/download/gif2webp-bin-3.0.2.tgz#8620540fc07b8cdcc262a91d55d7c99d873a1d4f"
1982+
integrity sha1-hiBUD8B7jNzCYqkdVdfJnYc6HU8=
1983+
dependencies:
1984+
bin-build "^3.0.0"
1985+
bin-wrapper "^4.0.0"
1986+
logalot "^2.0.0"
1987+
19701988
gifsicle@^5.0.0:
19711989
version "5.1.0"
19721990
resolved "https://registry.yarnpkg.com/gifsicle/-/gifsicle-5.1.0.tgz#08f878e9048c70adf046185115a6350516a1fdc0"
@@ -2304,6 +2322,15 @@ image-size@^0.8.3:
23042322
dependencies:
23052323
queue "6.0.1"
23062324

2325+
imagemin-gif2webp@^3.0.0:
2326+
version "3.0.0"
2327+
resolved "https://registry.npm.taobao.org/imagemin-gif2webp/download/imagemin-gif2webp-3.0.0.tgz#7685e45727f667594cc81705211dddc62ae7fdbd"
2328+
integrity sha1-doXkVyf2Z1lMyBcFIR3dxirn/b0=
2329+
dependencies:
2330+
exec-buffer "^3.0.0"
2331+
gif2webp-bin "^3.0.0"
2332+
is-gif "^3.0.0"
2333+
23072334
imagemin-gifsicle@^7.0.0:
23082335
version "7.0.0"
23092336
resolved "https://registry.yarnpkg.com/imagemin-gifsicle/-/imagemin-gifsicle-7.0.0.tgz#1a7ab136a144c4678657ba3b6c412f80805d26b0"
@@ -2339,6 +2366,15 @@ imagemin-upng@^2.0.2:
23392366
is-png "2.0.0"
23402367
lib-upng "1.0.1"
23412368

2369+
imagemin-webp@^6.0.0:
2370+
version "6.0.0"
2371+
resolved "https://registry.npm.taobao.org/imagemin-webp/download/imagemin-webp-6.0.0.tgz#bb2d77bab818fd6133df9675326497b877e7be4d"
2372+
integrity sha1-uy13urgY/WEz35Z1MmSXuHfnvk0=
2373+
dependencies:
2374+
cwebp-bin "^5.0.0"
2375+
exec-buffer "^3.0.0"
2376+
is-cwebp-readable "^3.0.0"
2377+
23422378
imagemin@^7.0.1:
23432379
version "7.0.1"
23442380
resolved "https://registry.npm.taobao.org/imagemin/download/imagemin-7.0.1.tgz?cache=0&sync_timestamp=1573808716935&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fimagemin%2Fdownload%2Fimagemin-7.0.1.tgz#f6441ca647197632e23db7d971fffbd530c87dbf"
@@ -2480,6 +2516,13 @@ is-buffer@^1.1.5, is-buffer@~1.1.1:
24802516
resolved "https://registry.npm.taobao.org/is-buffer/download/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"
24812517
integrity sha1-76ouqdqg16suoTqXsritUf776L4=
24822518

2519+
is-cwebp-readable@^3.0.0:
2520+
version "3.0.0"
2521+
resolved "https://registry.npm.taobao.org/is-cwebp-readable/download/is-cwebp-readable-3.0.0.tgz#0554aaa400977a2fc4de366d8c0244f13cde58cb"
2522+
integrity sha1-BVSqpACXei/E3jZtjAJE8TzeWMs=
2523+
dependencies:
2524+
file-type "^10.5.0"
2525+
24832526
is-data-descriptor@^0.1.4:
24842527
version "0.1.4"
24852528
resolved "https://registry.npm.taobao.org/is-data-descriptor/download/is-data-descriptor-0.1.4.tgz#0b5ee648388e2c860282e793f1856fec3f301b56"

0 commit comments

Comments
 (0)