Skip to content

Commit 78ee8b6

Browse files
authored
Merge pull request #14 from skyNet2017/master
增加lubanforgitee选项,应对gitee的1M一下外链不能访问的限制
2 parents 77f900a + 45d9034 commit 78ee8b6

File tree

7 files changed

+156
-24
lines changed

7 files changed

+156
-24
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
- [imagemin](https://github.com/imagemin/imagemin) +luban 算法,最大程度节省流量,默认选项
2424
- [imagemin](https://github.com/imagemin/imagemin) 压缩过程不需要经过网络,但是图片会有损耗
2525
- [tinypng](https://tinypng.com/) 无损压缩,需要上传到 tinypng
26+
- lubanforgitee 将所有图片压缩到1M以下,解决gitee的1M外链不能访问的问题. 注意,此配置下,gif图会变成jpg静态图.
2627

27-
nameType 是否重命名
28+
nameType 是否重命名
2829

2930
- timestamp 重命名成时间戳
30-
- none 不重名,默认选项
31+
- none 不重名,默认选项
32+
3133

32-
key 可选
34+
key 可选
3335

3436
-[developers](https://tinypng.com/developers) 中申请
3537
- 逗号`,`隔开,可使用多个 Key 叠加使用次数

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"imagemin-mozjpeg": "^8.0.0",
5050
"imagemin-upng": "^2.0.2",
5151
"images": "^3.2.3",
52+
"is-gif": "^3.0.0",
5253
"request": "^2.88.2"
5354
}
5455
}

src/compress/luban.ts

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import imagemin from 'imagemin'
22
import mozjpeg from 'imagemin-mozjpeg'
33
import { CompressOptions, ImgInfo } from '../utils/interfaces'
44
import { getImageBuffer } from '../utils/getImage'
5-
var images = require("images");
65

6+
var images = require('images')
7+
const isGif = require('is-gif')
78

9+
//由于gitee文件大小有1mb限制, 所以超过1mb的文件无法通过外链获取
810

911
export function lubanCompress({ ctx, info }: CompressOptions): Promise<ImgInfo> {
1012
/*function getSample(info: ImgInfo) {
@@ -13,27 +15,27 @@ export function lubanCompress({ ctx, info }: CompressOptions): Promise<ImgInfo>
1315

1416

1517
function computeInSampleSize(srcWidth: number, srcHeight: number) {
16-
srcWidth = srcWidth % 2 == 1 ? srcWidth + 1 : srcWidth;
17-
srcHeight = srcHeight % 2 == 1 ? srcHeight + 1 : srcHeight;
18+
srcWidth = srcWidth % 2 == 1 ? srcWidth + 1 : srcWidth
19+
srcHeight = srcHeight % 2 == 1 ? srcHeight + 1 : srcHeight
1820

19-
var longSide = Math.max(srcWidth, srcHeight);
20-
var shortSide = Math.min(srcWidth, srcHeight);
21+
var longSide = Math.max(srcWidth, srcHeight)
22+
var shortSide = Math.min(srcWidth, srcHeight)
2123

22-
var scale = (shortSide / longSide);
24+
var scale = (shortSide / longSide)
2325
if (scale <= 1 && scale > 0.5625) {
2426
if (longSide < 1664) {
25-
return 1;
27+
return 1
2628
} else if (longSide < 4990) {
27-
return 2;
29+
return 2
2830
} else if (longSide > 4990 && longSide < 10240) {
29-
return 4;
31+
return 4
3032
} else {
31-
return longSide / 1280 == 0 ? 1 : longSide / 1280;
33+
return longSide / 1280 == 0 ? 1 : longSide / 1280
3234
}
3335
} else if (scale <= 0.5625 && scale > 0.5) {
34-
return longSide / 1280 == 0 ? 1 : longSide / 1280;
36+
return longSide / 1280 == 0 ? 1 : longSide / 1280
3537
} else {
36-
return Math.ceil(longSide / (1280.0 / scale));
38+
return Math.ceil(longSide / (1280.0 / scale))
3739
}
3840
}
3941

@@ -45,13 +47,16 @@ export function lubanCompress({ ctx, info }: CompressOptions): Promise<ImgInfo>
4547

4648
return getImageBuffer(ctx, info.url)
4749
.then((buffer) => {
48-
ctx.log.warn('原始文件大小:' + Math.round(buffer.length / 1024) + "k")
50+
ctx.log.warn('原始文件大小:' + Math.round(buffer.length / 1024) + 'k')
4951
if (isJpg(buffer)) {
5052
ctx.log.warn('本身就是jpg,不用转换:' + info.url)
5153
return buffer
5254
}
55+
if (isGif(buffer)) {
56+
return buffer
57+
}
5358
ctx.log.warn('luban 格式转换为jpg:' + info.url)
54-
return images(buffer).encode("jpg")//, {operation:90}
59+
return images(buffer).encode('jpg')//, {operation:90}
5560
})
5661
/*.then((buffer)=>{
5762
var image2 = images(buffer)
@@ -78,15 +83,19 @@ export function lubanCompress({ ctx, info }: CompressOptions): Promise<ImgInfo>
7883
return image2.resize(conpressWidth).encode("jpg")//, {operation:90}
7984
})*/
8085
.then((buffer) => {
81-
ctx.log.warn('文件大小:' + Math.round(buffer.length / 1024) + "k")
86+
ctx.log.warn('文件大小:' + Math.round(buffer.length / 1024) + 'k')
8287

8388
var image2 = images(buffer)
84-
ctx.log.warn('图片尺寸:' + image2.width() + "x" + image2.height())
89+
ctx.log.warn('图片尺寸:' + image2.width() + 'x' + image2.height())
90+
if (isGif(buffer)) {
91+
ctx.log.warn('gif图,不压缩')
92+
return buffer
93+
}
8594
//todo 关键在于获取图片本身的宽高
8695
var sample = Math.round(computeInSampleSize(image2.width(), image2.height()))
8796
var filesize = Math.round(buffer.length / 1024)
8897
var longsize = image2.width() > image2.height() ? image2.width() : image2.height()
89-
var sampleSize = ['1x1'];
98+
var sampleSize = ['1x1']
9099
if (filesize > 100 && sample > 1) {
91100
if (longsize > 3000 && filesize < 700) {
92101

@@ -98,14 +107,14 @@ export function lubanCompress({ ctx, info }: CompressOptions): Promise<ImgInfo>
98107
ctx.log.warn('sampleSize:' + sampleSize[0])
99108

100109
return imagemin.buffer(buffer, {
101-
plugins: [mozjpeg({ quality: 75, sample: sampleSize })],//, optipng({ optimizationLevel: 5 })//, sample:sampleSize
110+
plugins: [mozjpeg({ quality: 75, sample: sampleSize })]//, optipng({ optimizationLevel: 5 })//, sample:sampleSize
102111
})
103112
})
104113
.then((buffer) => {
105-
ctx.log.warn('最后mozjpeg compress in success,最终文件大小:' + Math.round(buffer.length / 1024) + "k")
114+
ctx.log.warn('最后mozjpeg compress in success,最终文件大小:' + Math.round(buffer.length / 1024) + 'k')
106115
return {
107116
...info,
108-
buffer,
117+
buffer
109118
}
110119
})
111120
}

src/compress/lubanforgitee.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import imagemin from 'imagemin'
2+
import mozjpeg from 'imagemin-mozjpeg'
3+
import { CompressOptions, ImgInfo } from '../utils/interfaces'
4+
import { getImageBuffer } from '../utils/getImage'
5+
6+
var images = require('images')
7+
const isGif = require('is-gif')
8+
9+
//由于gitee文件大小有1mb限制, 所以超过1mb的文件无法通过外链获取,通过这个工具将图压到1M以下
10+
11+
export function lubanforgiteeCompress({ ctx, info }: CompressOptions): Promise<ImgInfo> {
12+
/*function getSample(info: ImgInfo) {
13+
return ['1x1']
14+
}*/
15+
16+
17+
function computeInSampleSize(srcWidth: number, srcHeight: number) {
18+
srcWidth = srcWidth % 2 == 1 ? srcWidth + 1 : srcWidth
19+
srcHeight = srcHeight % 2 == 1 ? srcHeight + 1 : srcHeight
20+
21+
var longSide = Math.max(srcWidth, srcHeight)
22+
var shortSide = Math.min(srcWidth, srcHeight)
23+
24+
var scale = (shortSide / longSide)
25+
if (scale <= 1 && scale > 0.5625) {
26+
if (longSide < 1664) {
27+
return 1
28+
} else if (longSide < 4990) {
29+
return 2
30+
} else if (longSide > 4990 && longSide < 10240) {
31+
return 4
32+
} else {
33+
return longSide / 1280 == 0 ? 1 : longSide / 1280
34+
}
35+
} else if (scale <= 0.5625 && scale > 0.5) {
36+
return longSide / 1280 == 0 ? 1 : longSide / 1280
37+
} else {
38+
return Math.ceil(longSide / (1280.0 / scale))
39+
}
40+
}
41+
42+
function isJpg(buffer: Buffer) {
43+
return buffer[0] === 255 &&
44+
buffer[1] === 216 &&
45+
buffer[2] === 255
46+
}
47+
48+
return getImageBuffer(ctx, info.url)
49+
.then((buffer) => {
50+
ctx.log.warn('原始文件大小:' + Math.round(buffer.length / 1024) + 'k')
51+
if (isJpg(buffer)) {
52+
ctx.log.warn('本身就是jpg,不用转换:' + info.url)
53+
return buffer
54+
}
55+
if (isGif(buffer) && Math.round(buffer.length / 1024) < 1024) {
56+
//大于1M的gif,强制压缩成jpg
57+
return buffer
58+
}
59+
ctx.log.warn('luban 格式转换为jpg:' + info.url)
60+
return images(buffer).encode('jpg')//, {operation:90}
61+
})
62+
.then((buffer) => {
63+
ctx.log.warn('文件大小:' + Math.round(buffer.length / 1024) + 'k')
64+
65+
var image2 = images(buffer)
66+
ctx.log.warn('图片尺寸:' + image2.width() + 'x' + image2.height())
67+
if (isGif(buffer)) {
68+
ctx.log.info('gif图,不压缩')
69+
return buffer
70+
}
71+
//todo 关键在于获取图片本身的宽高
72+
var sample = Math.round(computeInSampleSize(image2.width(), image2.height()))
73+
var filesize = Math.round(buffer.length / 1024)
74+
var longsize = image2.width() > image2.height() ? image2.width() : image2.height()
75+
var sampleSize = ['1x1']
76+
if (filesize > 100 && sample > 1) {
77+
if (longsize > 3000 && filesize < 700) {
78+
79+
} else {
80+
sampleSize = [sample + 'x' + sample]
81+
}
82+
}
83+
ctx.log.warn('sampleSize:' + sampleSize[0] + ',质量75')
84+
85+
return imagemin.buffer(buffer, {
86+
plugins: [mozjpeg({ quality: 75, sample: sampleSize })]//, optipng({ optimizationLevel: 5 })//, sample:sampleSize
87+
})
88+
})
89+
.then((buffer) => {
90+
//最终第一道检查
91+
if (Math.round(buffer.length / 1024) < 1024) {
92+
return buffer
93+
}
94+
ctx.log.warn(Math.round(buffer.length / 1024) + 'k,大于1M,继续压,sampleSize:2x1,质量65')
95+
return imagemin.buffer(buffer, {
96+
plugins: [mozjpeg({ quality: 65, sample: ['2x1'] })]
97+
})
98+
})
99+
.then((buffer) => {
100+
//最终第二道检查
101+
if (Math.round(buffer.length / 1024) < 1024) {
102+
return buffer
103+
}
104+
ctx.log.warn(Math.round(buffer.length / 1024) + 'k,大于1M,继续压,sampleSize:1x2,质量60')
105+
return imagemin.buffer(buffer, {
106+
plugins: [mozjpeg({ quality: 60, sample: ['1x2'] })]
107+
})
108+
})
109+
.then((buffer) => {
110+
ctx.log.warn('最后mozjpeg compress in success,最终文件大小:' + Math.round(buffer.length / 1024) + 'k')
111+
return {
112+
...info,
113+
buffer
114+
}
115+
})
116+
}

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ export enum CompressType {
1111
tinypng = 'tinypng',
1212
imagemin = 'imagemin',
1313
luban = 'luban',
14+
lubangitee = 'lubangitee',
1415
}

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { imageminCompress } from './compress/imagemin'
88
import { NameType, CompressType } from './config'
99
import { reName } from './utils/reName'
1010
import { lubanCompress } from './compress/luban'
11+
import { lubanforgiteeCompress } from './compress/lubanforgitee'
1112

1213
//npm install /Users/hss/github/picgo-plugin-compress
1314
function handle(ctx: PicGo) {
@@ -36,6 +37,8 @@ function handle(ctx: PicGo) {
3637
return imageminCompress(options)
3738
case CompressType.luban:
3839
return lubanCompress(options)
40+
case CompressType.lubangitee:
41+
return lubanforgiteeCompress(options)
3942
default:
4043
return lubanCompress(options)
4144
}

src/utils/reName.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function reName(nameType: string, url: string): string {
55
const fileName = path.basename(url)
66
switch (nameType) {
77
case NameType.timestamp:
8-
return `${Date.now()}`
8+
return `${Date.now()}-`+fileName
99
case NameType.none:
1010
default:
1111
return `${fileName}`

0 commit comments

Comments
 (0)