@zhaoshijun/compress 1.2.3 → 1.2.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhaoshijun/compress",
3
- "version": "1.2.3",
3
+ "version": "1.2.4",
4
4
  "description": "Image compression CLI and Vite plugin",
5
5
  "type": "module",
6
6
  "bin": {
@@ -218,21 +218,25 @@ export async function addImageWatermark(sharpInstance, options) {
218
218
  }
219
219
 
220
220
  if (opacity < 1) {
221
- const tempBuffer = await watermarkImage.toBuffer();
222
- const tempSharp = sharp(tempBuffer);
223
- const { data, info } = await tempSharp.ensureAlpha().raw().toBuffer({ resolveWithObject: true });
224
-
225
- for (let i = 3; i < data.length; i += 4) {
226
- data[i] = Math.floor(data[i] * opacity);
227
- }
228
-
229
- watermarkImage = sharp(data, {
230
- raw: {
231
- width: info.width,
232
- height: info.height,
233
- channels: 4
221
+ try {
222
+ const tempBuffer = await watermarkImage.ensureAlpha().png().toBuffer();
223
+ const tempSharp = sharp(tempBuffer);
224
+ const { data, info } = await tempSharp.raw().toBuffer({ resolveWithObject: true });
225
+
226
+ for (let i = 3; i < data.length; i += 4) {
227
+ data[i] = Math.floor(data[i] * opacity);
234
228
  }
235
- });
229
+
230
+ watermarkImage = sharp(data, {
231
+ raw: {
232
+ width: info.width,
233
+ height: info.height,
234
+ channels: 4
235
+ }
236
+ });
237
+ } catch (error) {
238
+ console.warn('Failed to apply opacity, using original image:', error.message);
239
+ }
236
240
  }
237
241
 
238
242
  const finalWatermarkBuffer = await watermarkImage.toBuffer();
@@ -339,21 +343,25 @@ export async function addTiledImageWatermark(sharpInstance, options, providedMet
339
343
  }
340
344
 
341
345
  if (opacity < 1) {
342
- const tempBuffer = await watermarkImage.toBuffer();
343
- const tempSharp = sharp(tempBuffer);
344
- const { data, info } = await tempSharp.ensureAlpha().raw().toBuffer({ resolveWithObject: true });
345
-
346
- for (let i = 3; i < data.length; i += 4) {
347
- data[i] = Math.floor(data[i] * opacity);
348
- }
349
-
350
- watermarkImage = sharp(data, {
351
- raw: {
352
- width: info.width,
353
- height: info.height,
354
- channels: 4
346
+ try {
347
+ const tempBuffer = await watermarkImage.ensureAlpha().png().toBuffer();
348
+ const tempSharp = sharp(tempBuffer);
349
+ const { data, info } = await tempSharp.raw().toBuffer({ resolveWithObject: true });
350
+
351
+ for (let i = 3; i < data.length; i += 4) {
352
+ data[i] = Math.floor(data[i] * opacity);
355
353
  }
356
- });
354
+
355
+ watermarkImage = sharp(data, {
356
+ raw: {
357
+ width: info.width,
358
+ height: info.height,
359
+ channels: 4
360
+ }
361
+ });
362
+ } catch (error) {
363
+ console.warn('Failed to apply opacity, using original image:', error.message);
364
+ }
357
365
  }
358
366
 
359
367
  if (angle !== 0) {