@zhaoshijun/compress 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/compress.js +4 -1
- package/package.json +1 -1
- package/src/core/compressor.js +1 -1
- package/src/core/watermark.js +26 -2
package/bin/compress.js
CHANGED
|
@@ -12,13 +12,16 @@ import { loadConfig } from '../src/config/loader.js';
|
|
|
12
12
|
import { compressImage } from '../src/core/compressor.js';
|
|
13
13
|
import { DEFAULT_EXCLUDES } from '../src/utils/constants.js';
|
|
14
14
|
import { isSupportedImage as isSupported } from '../src/utils/file-utils.js';
|
|
15
|
+
import { createRequire } from 'module';
|
|
16
|
+
const require = createRequire(import.meta.url);
|
|
17
|
+
const packageJson = require('../package.json');
|
|
15
18
|
|
|
16
19
|
const program = new Command();
|
|
17
20
|
|
|
18
21
|
program
|
|
19
22
|
.name('compress')
|
|
20
23
|
.description('Image compression CLI tool')
|
|
21
|
-
.version(
|
|
24
|
+
.version(packageJson.version);
|
|
22
25
|
|
|
23
26
|
program
|
|
24
27
|
.option('-c, --config <path>', '配置文件路径')
|
package/package.json
CHANGED
package/src/core/compressor.js
CHANGED
|
@@ -65,7 +65,7 @@ export async function compressImage(input, options, filePath) {
|
|
|
65
65
|
let result = await instance.toBuffer();
|
|
66
66
|
|
|
67
67
|
if (options.watermark && options.watermark.text) {
|
|
68
|
-
result = await addWatermark(result, options.watermark);
|
|
68
|
+
result = await addWatermark(result, options.watermark, format, options);
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
return result;
|
package/src/core/watermark.js
CHANGED
|
@@ -117,9 +117,11 @@ async function createTiledWatermark(width, height, text, options) {
|
|
|
117
117
|
* @param {number} [watermarkOptions.density=3] - 水印密度 (1-10)
|
|
118
118
|
* @param {string} [watermarkOptions.color='#ffffff'] - 水印颜色
|
|
119
119
|
* @param {number} [watermarkOptions.fontSize=24] - 字体大小
|
|
120
|
+
* @param {string} originalFormat - 原始图片格式
|
|
121
|
+
* @param {Object} compressOptions - 压缩选项
|
|
120
122
|
* @returns {Promise<Buffer>} 添加水印后的图片 Buffer
|
|
121
123
|
*/
|
|
122
|
-
export async function addWatermark(input, watermarkOptions) {
|
|
124
|
+
export async function addWatermark(input, watermarkOptions, originalFormat, compressOptions) {
|
|
123
125
|
const { text, opacity = 0.5, density = 3, color = '#ffffff', fontSize = 24 } = watermarkOptions;
|
|
124
126
|
|
|
125
127
|
if (!text) {
|
|
@@ -144,9 +146,31 @@ export async function addWatermark(input, watermarkOptions) {
|
|
|
144
146
|
{ opacity, density, color, fontSize }
|
|
145
147
|
);
|
|
146
148
|
|
|
147
|
-
|
|
149
|
+
let result = await image
|
|
148
150
|
.composite([{ input: watermarkBuffer, blend: 'over' }])
|
|
149
151
|
.toBuffer();
|
|
150
152
|
|
|
153
|
+
// 根据原始格式重新压缩,避免 PNG 水印导致体积增大
|
|
154
|
+
if (originalFormat && originalFormat !== metadata.format) {
|
|
155
|
+
const sharpInstance = sharp(result);
|
|
156
|
+
|
|
157
|
+
switch (originalFormat) {
|
|
158
|
+
case 'jpeg':
|
|
159
|
+
case 'jpg':
|
|
160
|
+
result = await sharpInstance.jpeg({ quality: compressOptions?.quality || 88 }).toBuffer();
|
|
161
|
+
break;
|
|
162
|
+
case 'webp':
|
|
163
|
+
result = await sharpInstance.webp({ quality: compressOptions?.quality || 88 }).toBuffer();
|
|
164
|
+
break;
|
|
165
|
+
case 'png':
|
|
166
|
+
result = await sharpInstance.png({
|
|
167
|
+
compressionLevel: compressOptions?.pngOptions?.compressionLevel ?? 9,
|
|
168
|
+
palette: compressOptions?.pngOptions?.palette ?? true,
|
|
169
|
+
quality: compressOptions?.quality
|
|
170
|
+
}).toBuffer();
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
151
175
|
return result;
|
|
152
176
|
}
|