Pixel Pinch Extensions commited on
Commit
ce511b2
Β·
1 Parent(s): 43582fd

fix: resolve syntax errors and enhance webtoon handling

Browse files

- Fix missing closing brace in getCompressionOptions function
- Add proper error codes to timeout errors in withTimeout function
- Improve webtoon resize logic with aspect ratio preservation
- Update resize thresholds for better quality (10MP β†’ 9.8MP target)
- Add detailed error information to compression failures

Files changed (2) hide show
  1. src/compress.js +71 -12
  2. src/config.js +2 -2
src/compress.js CHANGED
@@ -403,7 +403,9 @@ export async function compressImage(buffer, params, acceptHeader = '') {
403
  inputSize: originalSize,
404
  format,
405
  width,
406
- height
 
 
407
  }
408
  };
409
  } else {
@@ -416,9 +418,11 @@ export async function compressImage(buffer, params, acceptHeader = '') {
416
  message: err.message,
417
  stack: err.stack,
418
  inputSize: originalSize,
419
- format,
420
- width,
421
- height
 
 
422
  }
423
  };
424
  }
@@ -429,16 +433,67 @@ export async function compressImage(buffer, params, acceptHeader = '') {
429
  * Calculate smart resize dimensions
430
  */
431
  function calculateResize(width, height, pixels, imageType) {
432
- // Skip smart resize for webtoons to preserve their original dimensions
433
- if (imageType === 'webtoon') {
434
- compressionLogger.debug('Skipping smart resize for webtoon content');
435
- return null;
436
- }
437
-
438
  let targetWidth = width;
439
  let targetHeight = height;
440
  let needsResize = false;
441
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
442
  // Cap maximum dimensions
443
  if (width > CONFIG.MAX_WIDTH || height > CONFIG.MAX_HEIGHT) {
444
  const ratio = Math.min(
@@ -736,7 +791,7 @@ function getCompressionOptions(params, supportsAvif, supportsWebp, optimalFormat
736
  strip: true
737
  }
738
  };
739
- }
740
 
741
  export function parseParams(req) {
742
  // Input validation
@@ -827,7 +882,11 @@ function withTimeout(promise, ms) {
827
  return Promise.race([
828
  promise,
829
  new Promise((_, reject) =>
830
- setTimeout(() => reject(new Error('Timeout')), ms)
 
 
 
 
831
  )
832
  ]);
833
  }
 
403
  inputSize: originalSize,
404
  format,
405
  width,
406
+ height,
407
+ error: err.message,
408
+ stack: err.stack
409
  }
410
  };
411
  } else {
 
418
  message: err.message,
419
  stack: err.stack,
420
  inputSize: originalSize,
421
+ format: format,
422
+ width: width,
423
+ height: height,
424
+ error: err.message,
425
+ stack: err.stack
426
  }
427
  };
428
  }
 
433
  * Calculate smart resize dimensions
434
  */
435
  function calculateResize(width, height, pixels, imageType) {
 
 
 
 
 
 
436
  let targetWidth = width;
437
  let targetHeight = height;
438
  let needsResize = false;
439
 
440
+ // Special handling for webtoons - preserve aspect ratio but resize very large images
441
+ if (imageType === 'webtoon') {
442
+ compressionLogger.debug('Processing webtoon resize with aspect ratio preservation');
443
+
444
+ // Only resize webtoons if they exceed maximum dimensions or pixel threshold
445
+ const exceedsMaxDims = width > CONFIG.MAX_WIDTH || height > CONFIG.MAX_HEIGHT;
446
+ const exceedsPixelThreshold = pixels > CONFIG.RESIZE_THRESHOLD;
447
+
448
+ if (!exceedsMaxDims && !exceedsPixelThreshold) {
449
+ compressionLogger.debug('Webtoon size is acceptable, skipping resize');
450
+ return null;
451
+ }
452
+
453
+ // For webtoons, prioritize height preservation while scaling width proportionally
454
+ if (exceedsMaxDims) {
455
+ if (height > CONFIG.MAX_HEIGHT) {
456
+ // Scale down height to max, preserve aspect ratio
457
+ const ratio = CONFIG.MAX_HEIGHT / height;
458
+ targetHeight = CONFIG.MAX_HEIGHT;
459
+ targetWidth = Math.round(width * ratio);
460
+ needsResize = true;
461
+ compressionLogger.debug(`Webtoon height scaled down: ${height} β†’ ${targetHeight}, width: ${width} β†’ ${targetWidth}`);
462
+ } else if (width > CONFIG.MAX_WIDTH) {
463
+ // Scale down width to max, preserve aspect ratio
464
+ const ratio = CONFIG.MAX_WIDTH / width;
465
+ targetWidth = CONFIG.MAX_WIDTH;
466
+ targetHeight = Math.round(height * ratio);
467
+ needsResize = true;
468
+ compressionLogger.debug(`Webtoon width scaled down: ${width} β†’ ${targetWidth}, height: ${height} β†’ ${targetHeight}`);
469
+ }
470
+ }
471
+
472
+ // Additional pixel count reduction if still too large
473
+ const currentPixels = targetWidth * targetHeight;
474
+ if (currentPixels > CONFIG.TARGET_PIXELS) {
475
+ const scale = Math.sqrt(CONFIG.TARGET_PIXELS / currentPixels);
476
+ targetWidth = Math.round(targetWidth * scale);
477
+ targetHeight = Math.round(targetHeight * scale);
478
+ needsResize = true;
479
+ compressionLogger.debug(`Webtoon pixel reduction: ${currentPixels} β†’ ${targetWidth * targetHeight} pixels`);
480
+ }
481
+
482
+ if (needsResize) {
483
+ compressionLogger.debug(`Webtoon resize: ${width}x${height} β†’ ${targetWidth}x${targetHeight}`);
484
+ }
485
+
486
+ return needsResize ? {
487
+ width: targetWidth,
488
+ height: targetHeight,
489
+ fit: 'inside',
490
+ withoutEnlargement: true,
491
+ fastShrinkOnLoad: true,
492
+ kernel: 'lanczos3' // High quality for webtoon line art
493
+ } : null;
494
+ }
495
+
496
+ // Regular image processing (non-webtoon)
497
  // Cap maximum dimensions
498
  if (width > CONFIG.MAX_WIDTH || height > CONFIG.MAX_HEIGHT) {
499
  const ratio = Math.min(
 
791
  strip: true
792
  }
793
  };
794
+ } // <--- Added missing closing brace
795
 
796
  export function parseParams(req) {
797
  // Input validation
 
882
  return Promise.race([
883
  promise,
884
  new Promise((_, reject) =>
885
+ setTimeout(() => {
886
+ const error = new Error('Timeout');
887
+ error.code = 'TIMEOUT';
888
+ reject(error);
889
+ }, ms)
890
  )
891
  ]);
892
  }
src/config.js CHANGED
@@ -120,8 +120,8 @@ const CONFIG = {
120
  MAX_PIXELS: 30000000, // 30MP max (increased for tall webtoon/manhwa images, HF has 16GB RAM)
121
 
122
  // Smart Resize - QUALITY FOCUSED
123
- RESIZE_THRESHOLD: 4000000, // Resize if > 4MP (less aggressive)
124
- TARGET_PIXELS: 3800000, // Target 3.5MP (better quality)
125
  MAX_WIDTH: 8192,
126
  MAX_HEIGHT: 24000,
127
 
 
120
  MAX_PIXELS: 30000000, // 30MP max (increased for tall webtoon/manhwa images, HF has 16GB RAM)
121
 
122
  // Smart Resize - QUALITY FOCUSED
123
+ RESIZE_THRESHOLD: 10000000, // Resize if > 10MP (less aggressive)
124
+ TARGET_PIXELS: 9700000, // Target 9.8MP (better quality)
125
  MAX_WIDTH: 8192,
126
  MAX_HEIGHT: 24000,
127