stream feature
Browse files- image-processing-logic.js +23 -39
image-processing-logic.js
CHANGED
|
@@ -1,46 +1,36 @@
|
|
| 1 |
const sharp = require('sharp');
|
|
|
|
| 2 |
|
| 3 |
class ImageProcessor {
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
try {
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
let newHeight = height ? parseInt(height) : null;
|
| 13 |
-
|
| 14 |
-
if (newWidth || newHeight) {
|
| 15 |
-
if (maintainAspect) {
|
| 16 |
-
if (newWidth && !newHeight) {
|
| 17 |
-
newHeight = Math.round(newWidth / (metadata.width / metadata.height));
|
| 18 |
-
} else if (!newWidth && newHeight) {
|
| 19 |
-
newWidth = Math.round(newHeight * (metadata.width / metadata.height));
|
| 20 |
-
}
|
| 21 |
-
}
|
| 22 |
-
|
| 23 |
-
sharpInstance = sharpInstance.resize(newWidth, newHeight, {
|
| 24 |
-
fit: 'inside',
|
| 25 |
-
withoutEnlargement: true
|
| 26 |
-
});
|
| 27 |
-
}
|
| 28 |
-
|
| 29 |
switch (format.toLowerCase()) {
|
| 30 |
case 'jpeg':
|
| 31 |
sharpInstance = sharpInstance.jpeg({ quality: parseInt(quality), mozjpeg: true });
|
| 32 |
break;
|
| 33 |
case 'png':
|
| 34 |
-
sharpInstance = sharpInstance.png({ quality: parseInt(quality) });
|
| 35 |
break;
|
| 36 |
case 'webp':
|
| 37 |
-
sharpInstance = sharpInstance.webp({ quality: parseInt(quality) });
|
| 38 |
break;
|
| 39 |
case 'avif':
|
| 40 |
-
sharpInstance = sharpInstance.avif({ quality: parseInt(quality) });
|
| 41 |
break;
|
| 42 |
case 'heif':
|
| 43 |
-
sharpInstance = sharpInstance.heif({ quality: parseInt(quality) });
|
| 44 |
break;
|
| 45 |
case 'tiff':
|
| 46 |
sharpInstance = sharpInstance.tiff({ quality: parseInt(quality) });
|
|
@@ -48,18 +38,12 @@ class ImageProcessor {
|
|
| 48 |
default:
|
| 49 |
throw new Error('Unsupported format');
|
| 50 |
}
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
buffer: processedBuffer,
|
| 56 |
-
size: processedBuffer.length,
|
| 57 |
-
width: newWidth || metadata.width,
|
| 58 |
-
height: newHeight || metadata.height,
|
| 59 |
-
format: format
|
| 60 |
-
};
|
| 61 |
} catch (error) {
|
| 62 |
-
console.error('Error
|
| 63 |
throw error;
|
| 64 |
}
|
| 65 |
}
|
|
|
|
| 1 |
const sharp = require('sharp');
|
| 2 |
+
const stream = require('stream');
|
| 3 |
|
| 4 |
class ImageProcessor {
|
| 5 |
+
/**
|
| 6 |
+
* Processes an image from a readable stream.
|
| 7 |
+
* @param {stream.Readable} input - The incoming stream of image data.
|
| 8 |
+
* @param {Object} options - Processing options including quality and format.
|
| 9 |
+
* @returns {stream.Readable} - A readable stream of the processed image.
|
| 10 |
+
*/
|
| 11 |
+
processImage(input, options = {}) {
|
| 12 |
+
const { quality = 80, format = 'jpeg' } = options;
|
| 13 |
+
|
| 14 |
try {
|
| 15 |
+
// Create a Sharp instance for streaming.
|
| 16 |
+
let sharpInstance = sharp();
|
| 17 |
+
|
| 18 |
+
// Apply format and quality options.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
switch (format.toLowerCase()) {
|
| 20 |
case 'jpeg':
|
| 21 |
sharpInstance = sharpInstance.jpeg({ quality: parseInt(quality), mozjpeg: true });
|
| 22 |
break;
|
| 23 |
case 'png':
|
| 24 |
+
sharpInstance = sharpInstance.png({ quality: parseInt(quality), compressionLevel: 1 });
|
| 25 |
break;
|
| 26 |
case 'webp':
|
| 27 |
+
sharpInstance = sharpInstance.webp({ quality: parseInt(quality), speed: 8 });
|
| 28 |
break;
|
| 29 |
case 'avif':
|
| 30 |
+
sharpInstance = sharpInstance.avif({ quality: parseInt(quality), speed: 8 });
|
| 31 |
break;
|
| 32 |
case 'heif':
|
| 33 |
+
sharpInstance = sharpInstance.heif({ quality: parseInt(quality), speed: 8 });
|
| 34 |
break;
|
| 35 |
case 'tiff':
|
| 36 |
sharpInstance = sharpInstance.tiff({ quality: parseInt(quality) });
|
|
|
|
| 38 |
default:
|
| 39 |
throw new Error('Unsupported format');
|
| 40 |
}
|
| 41 |
+
|
| 42 |
+
// Pipe the input stream to the sharp pipeline and return the output stream.
|
| 43 |
+
return input.pipe(sharpInstance);
|
| 44 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
} catch (error) {
|
| 46 |
+
console.error('Error setting up image processing stream:', error);
|
| 47 |
throw error;
|
| 48 |
}
|
| 49 |
}
|