Skip to content

Commit cf112ce

Browse files
committed
feat: 增加luban压缩,删除none,upng,增加压缩效果对比
1 parent 6416891 commit cf112ce

File tree

16 files changed

+55
-125
lines changed

16 files changed

+55
-125
lines changed

README.md

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,24 @@
1313

1414
- 安装 `picgo add compress`
1515

16-
- 选择使用 compress `picgo use transformer`
16+
- 选择使用 `picgo use transformer`
1717

1818
- 参数配置 `picgo config plugin compress`
1919

2020
compress 选择压缩工具
21+
默认选项
2122

22-
- [imagemin](https://github.com/imagemin/imagemin) 压缩过程不需要经过网络,速度快,但是图片会有损耗,默认选项
23-
23+
- [imagemin](https://github.com/imagemin/imagemin) +luban 算法,最大程度节省流量,默认选项
24+
- [imagemin](https://github.com/imagemin/imagemin) 压缩过程不需要经过网络,但是图片会有损耗
2425
- [tinypng](https://tinypng.com/) 无损压缩,需要上传到 tinypng
25-
26-
- [upng](https://github.com/photopea/UPNG.js) upng 无损压缩
27-
28-
- none 不压缩
29-
30-
- luban [imagemin](https://github.com/imagemin/imagemin) +luban算法,最大程度节省流量,效果如下:
3126

32-
![image-20200922212916280](https://cdn.jsdelivr.net/gh/hss01248/picbed@master/pic/1600781356315.jpeg)
33-
34-
![image-20200922213104719](https://cdn.jsdelivr.net/gh/hss01248/picbed@master/pic/1600781464747.jpeg)
35-
3627
nameType 是否重命名
37-
28+
3829
- timestamp 重命名成时间戳
3930
- none 不重名,默认选项
40-
31+
4132
key 可选
42-
33+
4334
-[developers](https://tinypng.com/developers) 中申请
4435
- 逗号`,`隔开,可使用多个 Key 叠加使用次数
4536

@@ -48,5 +39,10 @@
4839
- 打开详细窗口 > 插件设置 > 搜索 `compress` 即可安装,配置同上
4940
- 离线安装参考[这里](https://picgo.github.io/PicGo-Core-Doc/zh/dev-guide/deploy.html#gui%E6%8F%92%E4%BB%B6)
5041

42+
## 压缩效果对比
5143

52-
44+
| 类型 | tinypng | luban | imagemin |
45+
| ------ | ------------------------ | ---------------------- | ------------------------- |
46+
| 原大小 | 1.5MB | 1.5M | 1.5M |
47+
| 压缩后 | 315 KB | 276 KB | 411 KB |
48+
| 效果图 | ![](./tests/tinypng.png) | ![](./tests/luban.png) | ![](./tests/imagemin.png) |

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"@types/fs-extra": "^8.1.0",
3030
"@types/imagemin": "^7.0.0",
3131
"@types/imagemin-mozjpeg": "^8.0.0",
32-
"@types/imagemin-optipng": "^5.2.0",
3332
"@types/node": "^12.12.36",
3433
"@types/request": "^2.48.4",
3534
"@types/request-promise-native": "^1.0.17",
@@ -48,7 +47,6 @@
4847
"imagemin": "^7.0.1",
4948
"imagemin-jpegtran": "^7.0.0",
5049
"imagemin-mozjpeg": "^8.0.0",
51-
"imagemin-optipng": "^7.1.0",
5250
"imagemin-upng": "^2.0.2",
5351
"images": "^3.2.3",
5452
"request": "^2.88.2"

src/compress/imagemin.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import imagemin from 'imagemin'
22
import mozjpeg from 'imagemin-mozjpeg'
3-
import optipng from 'imagemin-optipng'
3+
import upng from 'imagemin-upng'
44
import { CompressOptions, ImgInfo } from '../utils/interfaces'
55
import { getImageBuffer } from '../utils/getImage'
66

77
export function imageminCompress({ ctx, info }: CompressOptions): Promise<ImgInfo> {
8+
ctx.log.info('imagemin 压缩开始')
89
return getImageBuffer(ctx, info.url)
910
.then((buffer) => {
10-
ctx.log.info('imagemin compress in progress')
11+
ctx.log.info('imagemin 压缩完成')
1112
return imagemin.buffer(buffer, {
12-
plugins: [mozjpeg({ quality: 75, progressive: true }), optipng({ optimizationLevel: 5 })],
13+
plugins: [mozjpeg({ quality: 75, progressive: true }), upng()],
1314
})
1415
})
1516
.then((buffer) => {
16-
ctx.log.info('imagemin compress in success')
17+
ctx.log.info('imagemin 压缩失败')
1718
return {
1819
...info,
1920
buffer,

src/compress/luban.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import imagemin from 'imagemin'
22
import mozjpeg from 'imagemin-mozjpeg'
3-
import optipng from 'imagemin-optipng'
43
import { CompressOptions, ImgInfo } from '../utils/interfaces'
54
import { getImageBuffer } from '../utils/getImage'
65
var images = require("images");
@@ -13,14 +12,14 @@ export function lubanCompress({ ctx, info }: CompressOptions): Promise<ImgInfo>
1312
}*/
1413

1514

16-
function computeInSampleSize(srcWidth :number, srcHeight:number) {
15+
function computeInSampleSize(srcWidth: number, srcHeight: number) {
1716
srcWidth = srcWidth % 2 == 1 ? srcWidth + 1 : srcWidth;
1817
srcHeight = srcHeight % 2 == 1 ? srcHeight + 1 : srcHeight;
1918

2019
var longSide = Math.max(srcWidth, srcHeight);
2120
var shortSide = Math.min(srcWidth, srcHeight);
2221

23-
var scale = ( shortSide / longSide);
22+
var scale = (shortSide / longSide);
2423
if (scale <= 1 && scale > 0.5625) {
2524
if (longSide < 1664) {
2625
return 1;
@@ -34,7 +33,7 @@ function computeInSampleSize(srcWidth :number, srcHeight:number) {
3433
} else if (scale <= 0.5625 && scale > 0.5) {
3534
return longSide / 1280 == 0 ? 1 : longSide / 1280;
3635
} else {
37-
return Math.ceil(longSide / (1280.0 / scale));
36+
return Math.ceil(longSide / (1280.0 / scale));
3837
}
3938
}
4039

@@ -45,14 +44,14 @@ function computeInSampleSize(srcWidth :number, srcHeight:number) {
4544
}
4645

4746
return getImageBuffer(ctx, info.url)
48-
.then((buffer)=>{
49-
ctx.log.warn('原始文件大小:'+Math.round(buffer.length/1024)+"k")
50-
if(isJpg(buffer)){
51-
ctx.log.warn('本身就是jpg,不用转换:'+info.url)
47+
.then((buffer) => {
48+
ctx.log.warn('原始文件大小:' + Math.round(buffer.length / 1024) + "k")
49+
if (isJpg(buffer)) {
50+
ctx.log.warn('本身就是jpg,不用转换:' + info.url)
5251
return buffer
5352
}
54-
ctx.log.warn('luban 格式转换为jpg:'+info.url)
55-
return images(buffer).encode("jpg")//, {operation:90}
53+
ctx.log.warn('luban 格式转换为jpg:' + info.url)
54+
return images(buffer).encode("jpg")//, {operation:90}
5655
})
5756
/*.then((buffer)=>{
5857
var image2 = images(buffer)
@@ -79,31 +78,31 @@ function computeInSampleSize(srcWidth :number, srcHeight:number) {
7978
return image2.resize(conpressWidth).encode("jpg")//, {operation:90}
8079
})*/
8180
.then((buffer) => {
82-
ctx.log.warn('文件大小:'+Math.round(buffer.length/1024)+"k")
81+
ctx.log.warn('文件大小:' + Math.round(buffer.length / 1024) + "k")
8382

8483
var image2 = images(buffer)
85-
ctx.log.warn('图片尺寸:'+image2.width()+"x"+image2.height())
84+
ctx.log.warn('图片尺寸:' + image2.width() + "x" + image2.height())
8685
//todo 关键在于获取图片本身的宽高
87-
var sample = Math.round(computeInSampleSize(image2.width(),image2.height()))
88-
var filesize = Math.round(buffer.length/1024)
89-
var longsize = image2.width() > image2.height() ? image2.width() :image2.height()
86+
var sample = Math.round(computeInSampleSize(image2.width(), image2.height()))
87+
var filesize = Math.round(buffer.length / 1024)
88+
var longsize = image2.width() > image2.height() ? image2.width() : image2.height()
9089
var sampleSize = ['1x1'];
91-
if(filesize > 100 && sample >1){
92-
if(longsize >3000 && filesize< 700){
90+
if (filesize > 100 && sample > 1) {
91+
if (longsize > 3000 && filesize < 700) {
9392

94-
}else {
95-
sampleSize = [sample+'x'+sample]
93+
} else {
94+
sampleSize = [sample + 'x' + sample]
9695
}
9796

9897
}
99-
ctx.log.warn('sampleSize:'+sampleSize[0])
98+
ctx.log.warn('sampleSize:' + sampleSize[0])
10099

101100
return imagemin.buffer(buffer, {
102-
plugins: [mozjpeg({ quality: 75,sample:sampleSize})],//, optipng({ optimizationLevel: 5 })//, sample:sampleSize
101+
plugins: [mozjpeg({ quality: 75, sample: sampleSize })],//, optipng({ optimizationLevel: 5 })//, sample:sampleSize
103102
})
104103
})
105104
.then((buffer) => {
106-
ctx.log.warn('最后mozjpeg compress in success,最终文件大小:'+Math.round(buffer.length/1024)+"k")
105+
ctx.log.warn('最后mozjpeg compress in success,最终文件大小:' + Math.round(buffer.length / 1024) + "k")
107106
return {
108107
...info,
109108
buffer,

src/compress/none.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/compress/tinypng/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export function tinypngKeyCompress({ ctx, info, key }: CompressOptions): Promise
55
return Tinypng.init({ ctx, keys: key!.split(',') })
66
.then(() => Tinypng.upload(info.url))
77
.then((buffer) => {
8+
ctx.log.info('TinypngWeb 上传成功')
89
return {
910
...info,
1011
buffer,

src/compress/tinypng/tinypng.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ class TinyPng {
2828
this.PicGo = options.ctx
2929
this.options = options
3030
await this.readOrWriteConfig(this.options.keys)
31+
this.PicGo.log.info('TinyPng初始化')
3132
}
3233

3334
async upload(url: string) {
35+
this.PicGo.log.info('TinyPng开始上传')
3436
if (isUrl(url)) {
3537
return this.uploadImage({ url, originalUrl: url, key: await this.getKey() })
3638
} else {
@@ -52,7 +54,7 @@ class TinyPng {
5254
}
5355

5456
private uploadImage(options: { key: string; originalUrl: string; url?: string; buffer?: Buffer }): Promise<Buffer> {
55-
this.PicGo.log.info('key:' + options.key)
57+
this.PicGo.log.info('使用TinypngKey:' + options.key)
5658

5759
const bearer = Base64.stringify(Utf8.parse(`api:${options.key}`))
5860

@@ -68,7 +70,7 @@ class TinyPng {
6870
}
6971

7072
if (options.url) {
71-
this.PicGo.log.info('上传网络图片')
73+
this.PicGo.log.info('TinyPng 上传网络图片')
7274
Object.assign(fetchOptions.headers, {
7375
'Content-Type': 'application/json',
7476
})
@@ -84,7 +86,7 @@ class TinyPng {
8486
const req = this.PicGo.Request.request(fetchOptions)
8587

8688
if (options.buffer) {
87-
this.PicGo.log.info('上传本地图片')
89+
this.PicGo.log.info('TinyPng 上传本地图片')
8890
req.end(options.buffer)
8991
}
9092

src/compress/tinypngweb.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function getHeaders() {
1515
}
1616

1717
export function tinypngCompress({ ctx, info }: CompressOptions): Promise<ImgInfo> {
18-
ctx.log.info('TinyPng Web Upload')
18+
ctx.log.info('TinypngWeb 开始上传')
1919
return getImageBuffer(ctx, info.url).then((buffer) => {
2020
const req = ctx.Request.request({
2121
url: TINYPNG_WEBUPLOAD_URL,
@@ -27,10 +27,10 @@ export function tinypngCompress({ ctx, info }: CompressOptions): Promise<ImgInfo
2727
return req
2828
.then((data: Response) => {
2929
if (data.headers.location) {
30-
ctx.log.info('TinyPng Web Upload Success:' + data.headers.location)
30+
ctx.log.info('TinypngWeb 上传成功:' + data.headers.location)
3131
return getImageBuffer(ctx, data.headers.location)
3232
}
33-
throw new Error('TinyPng Web Upload Error')
33+
throw new Error('TinypngWeb 上传失败')
3434
})
3535
.then((buffer) => {
3636
return {

src/config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,5 @@ export enum NameType {
1010
export enum CompressType {
1111
tinypng = 'tinypng',
1212
imagemin = 'imagemin',
13-
upng = 'upng',
1413
luban = 'luban',
15-
none = 'none',
1614
}

src/index.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ import PicGo from 'picgo'
22
import { imageSize } from 'image-size'
33
import * as path from 'path'
44
import { PluginConfig } from 'picgo/dist/src/utils/interfaces'
5-
import { defaultCompress } from './compress/none'
65
import { tinypngCompress } from './compress/tinypngweb'
76
import { tinypngKeyCompress } from './compress/tinypng/index'
87
import { imageminCompress } from './compress/imagemin'
98
import { NameType, CompressType } from './config'
109
import { reName } from './utils/reName'
11-
import { upngCompress } from './compress/upng'
1210
import { lubanCompress } from './compress/luban'
1311

1412
//npm install /Users/hss/github/picgo-plugin-compress
@@ -28,7 +26,7 @@ function handle(ctx: PicGo) {
2826
})
2927
.map((info) => {
3028
const options = { ctx, info }
31-
ctx.log.warn("compress type:"+compress)
29+
ctx.log.warn("compress type:" + compress)
3230
return Promise.resolve()
3331
.then(() => {
3432
switch (compress) {
@@ -38,9 +36,6 @@ function handle(ctx: PicGo) {
3836
return imageminCompress(options)
3937
case CompressType.luban:
4038
return lubanCompress(options)
41-
case CompressType.upng:
42-
return upngCompress(options)
43-
case CompressType.none:
4439
default:
4540
return lubanCompress(options)
4641
}

0 commit comments

Comments
 (0)