@zhaoshijun/compress 1.0.3 → 1.0.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhaoshijun/compress",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Image compression CLI and Vite plugin",
5
5
  "type": "module",
6
6
  "bin": {
@@ -22,8 +22,8 @@ export async function compressImage(input, options, filePath) {
22
22
  instance = instance.resize({
23
23
  width: maxWidth,
24
24
  height: maxHeight,
25
- fit: 'inside', // 保持宽高比,适应指定尺寸
26
- withoutEnlargement: true // 不放大
25
+ fit: 'inside',
26
+ withoutEnlargement: true
27
27
  });
28
28
  } else if (scale && scale > 0 && scale < 1) {
29
29
  const newWidth = Math.round(metadata.width * scale);
@@ -34,6 +34,13 @@ export async function compressImage(input, options, filePath) {
34
34
  // 格式处理
35
35
  const format = metadata.format;
36
36
 
37
+ // 先添加水印(如果需要)
38
+ if (options.watermark && options.watermark.text) {
39
+ const watermarkedBuffer = await addWatermark(instance, options.watermark);
40
+ instance = sharp(watermarkedBuffer);
41
+ }
42
+
43
+ // 再进行压缩
37
44
  switch (format) {
38
45
  case 'jpeg':
39
46
  case 'jpg':
@@ -43,18 +50,13 @@ export async function compressImage(input, options, filePath) {
43
50
  instance = instance.png({
44
51
  compressionLevel: options.pngOptions?.compressionLevel ?? 9,
45
52
  palette: options.pngOptions?.palette ?? true,
46
- quality: options.quality // sharp png 也可以接受 quality 参数配合 palette
53
+ quality: options.quality
47
54
  });
48
55
  break;
49
56
  case 'webp':
50
57
  instance = instance.webp({ quality: options.quality || 88 });
51
58
  break;
52
59
  default:
53
- // 如果不是支持的格式(虽然前面应该过滤了),不做处理直接返回原buffer(如果可能)或者报错
54
- // 这里假设调用方已经过滤了格式,但为了保险,如果不匹配就不压缩直接输出
55
- // 但 sharp 可能已经把输入转成了内部格式,所以这里最好还是根据原格式输出
56
- // 如果格式不受支持,可能在 metadata 阶段就看出来了。
57
- // 题目要求仅支持 .jpg, .jpeg, .png, .webp
58
60
  if (['jpeg', 'jpg', 'png', 'webp'].includes(format)) {
59
61
  // fallthrough
60
62
  } else {
@@ -62,11 +64,5 @@ export async function compressImage(input, options, filePath) {
62
64
  }
63
65
  }
64
66
 
65
- let result = await instance.toBuffer();
66
-
67
- if (options.watermark && options.watermark.text) {
68
- result = await addWatermark(result, options.watermark);
69
- }
70
-
71
- return result;
67
+ return instance.toBuffer();
72
68
  }
@@ -110,7 +110,7 @@ async function createTiledWatermark(width, height, text, options) {
110
110
 
111
111
  /**
112
112
  * 给图片添加水印
113
- * @param {string|Buffer} input - 图片路径或 Buffer
113
+ * @param {Sharp} sharpInstance - sharp 实例
114
114
  * @param {Object} watermarkOptions - 水印配置
115
115
  * @param {string} watermarkOptions.text - 水印文本
116
116
  * @param {number} [watermarkOptions.opacity=0.5] - 透明度 (0-1)
@@ -119,7 +119,7 @@ async function createTiledWatermark(width, height, text, options) {
119
119
  * @param {number} [watermarkOptions.fontSize=24] - 字体大小
120
120
  * @returns {Promise<Buffer>} 添加水印后的图片 Buffer
121
121
  */
122
- export async function addWatermark(input, watermarkOptions) {
122
+ export async function addWatermark(sharpInstance, watermarkOptions) {
123
123
  const { text, opacity = 0.5, density = 3, color = '#ffffff', fontSize = 24 } = watermarkOptions;
124
124
 
125
125
  if (!text) {
@@ -134,8 +134,7 @@ export async function addWatermark(input, watermarkOptions) {
134
134
  throw new Error('Density must be between 1 and 10');
135
135
  }
136
136
 
137
- const image = sharp(input);
138
- const metadata = await image.metadata();
137
+ const metadata = await sharpInstance.metadata();
139
138
 
140
139
  const watermarkBuffer = await createTiledWatermark(
141
140
  metadata.width,
@@ -144,7 +143,7 @@ export async function addWatermark(input, watermarkOptions) {
144
143
  { opacity, density, color, fontSize }
145
144
  );
146
145
 
147
- const result = await image
146
+ const result = await sharpInstance
148
147
  .composite([{ input: watermarkBuffer, blend: 'over' }])
149
148
  .toBuffer();
150
149