@zhaoshijun/compress 1.2.0 → 1.2.2
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/watermark.js +17 -15
package/package.json
CHANGED
package/src/core/watermark.js
CHANGED
|
@@ -204,7 +204,8 @@ export async function addImageWatermark(sharpInstance, options) {
|
|
|
204
204
|
throw new Error(`Watermark image not found: ${watermarkPath}`);
|
|
205
205
|
}
|
|
206
206
|
|
|
207
|
-
|
|
207
|
+
const watermarkBuffer = await fs.readFile(watermarkPath);
|
|
208
|
+
let watermarkImage = sharp(watermarkBuffer);
|
|
208
209
|
const watermarkMeta = await watermarkImage.metadata();
|
|
209
210
|
|
|
210
211
|
if (width || height) {
|
|
@@ -216,8 +217,8 @@ export async function addImageWatermark(sharpInstance, options) {
|
|
|
216
217
|
}
|
|
217
218
|
|
|
218
219
|
if (opacity < 1) {
|
|
219
|
-
const
|
|
220
|
-
watermarkImage = sharp(
|
|
220
|
+
const tempBuffer = await watermarkImage.toBuffer();
|
|
221
|
+
watermarkImage = sharp(tempBuffer).ensureAlpha();
|
|
221
222
|
const { data } = await watermarkImage.raw().toBuffer({ resolveWithObject: true });
|
|
222
223
|
|
|
223
224
|
for (let i = 3; i < data.length; i += 4) {
|
|
@@ -233,8 +234,8 @@ export async function addImageWatermark(sharpInstance, options) {
|
|
|
233
234
|
});
|
|
234
235
|
}
|
|
235
236
|
|
|
236
|
-
const
|
|
237
|
-
const finalWatermarkMeta = await sharp(
|
|
237
|
+
const finalWatermarkBuffer = await watermarkImage.toBuffer();
|
|
238
|
+
const finalWatermarkMeta = await sharp(finalWatermarkBuffer).metadata();
|
|
238
239
|
|
|
239
240
|
let compositeX = x;
|
|
240
241
|
let compositeY = y;
|
|
@@ -266,7 +267,7 @@ export async function addImageWatermark(sharpInstance, options) {
|
|
|
266
267
|
|
|
267
268
|
const result = await sharpInstance
|
|
268
269
|
.composite([{
|
|
269
|
-
input:
|
|
270
|
+
input: finalWatermarkBuffer,
|
|
270
271
|
left: compositeX,
|
|
271
272
|
top: compositeY,
|
|
272
273
|
blend: 'over'
|
|
@@ -321,7 +322,8 @@ export async function addTiledImageWatermark(sharpInstance, options, providedMet
|
|
|
321
322
|
throw new Error(`Watermark image not found: ${watermarkPath}`);
|
|
322
323
|
}
|
|
323
324
|
|
|
324
|
-
|
|
325
|
+
const watermarkBuffer = await fs.readFile(watermarkPath);
|
|
326
|
+
let watermarkImage = sharp(watermarkBuffer);
|
|
325
327
|
const watermarkMeta = await watermarkImage.metadata();
|
|
326
328
|
|
|
327
329
|
if (width || height) {
|
|
@@ -333,8 +335,8 @@ export async function addTiledImageWatermark(sharpInstance, options, providedMet
|
|
|
333
335
|
}
|
|
334
336
|
|
|
335
337
|
if (opacity < 1) {
|
|
336
|
-
const
|
|
337
|
-
watermarkImage = sharp(
|
|
338
|
+
const tempBuffer = await watermarkImage.toBuffer();
|
|
339
|
+
watermarkImage = sharp(tempBuffer).ensureAlpha();
|
|
338
340
|
const { data, info } = await watermarkImage.raw().toBuffer({ resolveWithObject: true });
|
|
339
341
|
|
|
340
342
|
for (let i = 3; i < data.length; i += 4) {
|
|
@@ -351,21 +353,21 @@ export async function addTiledImageWatermark(sharpInstance, options, providedMet
|
|
|
351
353
|
}
|
|
352
354
|
|
|
353
355
|
if (angle !== 0) {
|
|
354
|
-
const
|
|
355
|
-
const rotatedMeta = await sharp(
|
|
356
|
+
const rotatedBuffer = await watermarkImage.toBuffer();
|
|
357
|
+
const rotatedMeta = await sharp(rotatedBuffer).metadata();
|
|
356
358
|
const radians = angle * Math.PI / 180;
|
|
357
359
|
const cos = Math.abs(Math.cos(radians));
|
|
358
360
|
const sin = Math.abs(Math.sin(radians));
|
|
359
361
|
const newWidth = Math.ceil(rotatedMeta.width * cos + rotatedMeta.height * sin);
|
|
360
362
|
const newHeight = Math.ceil(rotatedMeta.width * sin + rotatedMeta.height * cos);
|
|
361
363
|
|
|
362
|
-
watermarkImage = sharp(
|
|
364
|
+
watermarkImage = sharp(rotatedBuffer).rotate(angle, {
|
|
363
365
|
background: { r: 0, g: 0, b: 0, alpha: 0 }
|
|
364
366
|
}).resize(newWidth, newHeight);
|
|
365
367
|
}
|
|
366
368
|
|
|
367
|
-
const
|
|
368
|
-
const finalWatermarkMeta = await sharp(
|
|
369
|
+
const finalWatermarkBuffer = await watermarkImage.toBuffer();
|
|
370
|
+
const finalWatermarkMeta = await sharp(finalWatermarkBuffer).metadata();
|
|
369
371
|
|
|
370
372
|
const cols = Math.ceil(metadata.width / spacingX) + 1;
|
|
371
373
|
const rows = Math.ceil(metadata.height / spacingY) + 1;
|
|
@@ -374,7 +376,7 @@ export async function addTiledImageWatermark(sharpInstance, options, providedMet
|
|
|
374
376
|
for (let row = 0; row < rows; row++) {
|
|
375
377
|
for (let col = 0; col < cols; col++) {
|
|
376
378
|
composites.push({
|
|
377
|
-
input:
|
|
379
|
+
input: finalWatermarkBuffer,
|
|
378
380
|
left: col * spacingX,
|
|
379
381
|
top: row * spacingY
|
|
380
382
|
});
|