@zhaoshijun/compress 1.1.2 → 1.1.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/package.json +1 -1
- package/src/core/compressor.js +2 -2
- package/src/core/watermark.js +9 -3
package/package.json
CHANGED
package/src/core/compressor.js
CHANGED
|
@@ -36,8 +36,8 @@ export async function compressImage(input, options, filePath) {
|
|
|
36
36
|
const format = resizedMetadata.format;
|
|
37
37
|
|
|
38
38
|
// 先添加水印(如果需要)
|
|
39
|
-
if (options.watermark && options.watermark.text) {
|
|
40
|
-
const watermarkedBuffer = await addWatermark(instance, options.watermark);
|
|
39
|
+
if (options.watermark && options.watermark.text && options.watermark.text.trim() !== '') {
|
|
40
|
+
const watermarkedBuffer = await addWatermark(instance, options.watermark, resizedMetadata);
|
|
41
41
|
instance = sharp(watermarkedBuffer);
|
|
42
42
|
}
|
|
43
43
|
|
package/src/core/watermark.js
CHANGED
|
@@ -101,10 +101,15 @@ async function createTiledWatermark(width, height, text, options) {
|
|
|
101
101
|
}
|
|
102
102
|
})
|
|
103
103
|
.composite(composites)
|
|
104
|
-
.extract({ left: 0, top: 0, width, height })
|
|
104
|
+
.extract({ left: 0, top: 0, width: Math.min(tiledWidth, width), height: Math.min(tiledHeight, height) })
|
|
105
105
|
.png()
|
|
106
106
|
.toBuffer();
|
|
107
107
|
|
|
108
|
+
const finalMeta = await sharp(tiledWatermark).metadata();
|
|
109
|
+
if (finalMeta.width !== width || finalMeta.height !== height) {
|
|
110
|
+
console.error(`Watermark size mismatch: expected ${width}x${height}, got ${finalMeta.width}x${finalMeta.height}`);
|
|
111
|
+
}
|
|
112
|
+
|
|
108
113
|
return tiledWatermark;
|
|
109
114
|
}
|
|
110
115
|
|
|
@@ -117,9 +122,10 @@ async function createTiledWatermark(width, height, text, options) {
|
|
|
117
122
|
* @param {number} [watermarkOptions.density=3] - 水印密度 (1-10)
|
|
118
123
|
* @param {string} [watermarkOptions.color='#ffffff'] - 水印颜色
|
|
119
124
|
* @param {number} [watermarkOptions.fontSize=24] - 字体大小
|
|
125
|
+
* @param {Object} [providedMetadata] - 提供的 metadata(避免重复获取)
|
|
120
126
|
* @returns {Promise<Buffer>} 添加水印后的图片 Buffer
|
|
121
127
|
*/
|
|
122
|
-
export async function addWatermark(sharpInstance, watermarkOptions) {
|
|
128
|
+
export async function addWatermark(sharpInstance, watermarkOptions, providedMetadata) {
|
|
123
129
|
const { text, opacity = 0.5, density = 3, color = '#ffffff', fontSize = 24 } = watermarkOptions;
|
|
124
130
|
|
|
125
131
|
if (!text) {
|
|
@@ -134,7 +140,7 @@ export async function addWatermark(sharpInstance, watermarkOptions) {
|
|
|
134
140
|
throw new Error('Density must be between 1 and 10');
|
|
135
141
|
}
|
|
136
142
|
|
|
137
|
-
const metadata = await sharpInstance.metadata();
|
|
143
|
+
const metadata = providedMetadata || await sharpInstance.metadata();
|
|
138
144
|
|
|
139
145
|
const watermarkBuffer = await createTiledWatermark(
|
|
140
146
|
metadata.width,
|