@zhaoshijun/compress 1.1.2 → 1.1.3
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 +3 -2
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
|
@@ -117,9 +117,10 @@ 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 {Object} [providedMetadata] - 提供的 metadata(避免重复获取)
|
|
120
121
|
* @returns {Promise<Buffer>} 添加水印后的图片 Buffer
|
|
121
122
|
*/
|
|
122
|
-
export async function addWatermark(sharpInstance, watermarkOptions) {
|
|
123
|
+
export async function addWatermark(sharpInstance, watermarkOptions, providedMetadata) {
|
|
123
124
|
const { text, opacity = 0.5, density = 3, color = '#ffffff', fontSize = 24 } = watermarkOptions;
|
|
124
125
|
|
|
125
126
|
if (!text) {
|
|
@@ -134,7 +135,7 @@ export async function addWatermark(sharpInstance, watermarkOptions) {
|
|
|
134
135
|
throw new Error('Density must be between 1 and 10');
|
|
135
136
|
}
|
|
136
137
|
|
|
137
|
-
const metadata = await sharpInstance.metadata();
|
|
138
|
+
const metadata = providedMetadata || await sharpInstance.metadata();
|
|
138
139
|
|
|
139
140
|
const watermarkBuffer = await createTiledWatermark(
|
|
140
141
|
metadata.width,
|