feat(image): add progressive JPEG generation support
- Add convertPDFToJpegBytes method for progressive JPEG images - Integrate @push.rocks/smartjimp for true progressive encoding - Update readme with comprehensive documentation - Update legal section to Task Venture Capital GmbH
This commit is contained in:
@@ -538,4 +538,52 @@ export class SmartPdf {
|
||||
await page.close();
|
||||
return webpBuffers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a PDF to progressive JPEG bytes for each page.
|
||||
* This method creates progressive JPEG images that load in multiple passes,
|
||||
* showing a low-quality preview first, then progressively improving.
|
||||
* Uses SmartJimp for true progressive JPEG encoding.
|
||||
*/
|
||||
public async convertPDFToJpegBytes(
|
||||
pdfBytes: Uint8Array,
|
||||
options: {
|
||||
scale?: number; // Scale factor for output size (default: 3.0 for 216 DPI)
|
||||
quality?: number; // JPEG quality 0-100 (default: 85)
|
||||
maxWidth?: number; // Maximum width in pixels (optional)
|
||||
maxHeight?: number; // Maximum height in pixels (optional)
|
||||
} = {}
|
||||
): Promise<Uint8Array[]> {
|
||||
// First, convert PDF to PNG using our existing method
|
||||
const pngBuffers = await this.convertPDFToPngBytes(pdfBytes, {
|
||||
scale: options.scale,
|
||||
maxWidth: options.maxWidth,
|
||||
maxHeight: options.maxHeight
|
||||
});
|
||||
|
||||
// Initialize SmartJimp in sharp mode for progressive JPEG support
|
||||
const smartJimpInstance = new plugins.smartjimp.SmartJimp({ mode: 'sharp' });
|
||||
|
||||
// Convert each PNG to progressive JPEG
|
||||
const jpegBuffers: Uint8Array[] = [];
|
||||
const quality = options.quality || 85;
|
||||
|
||||
for (const pngBuffer of pngBuffers) {
|
||||
// Convert PNG buffer to progressive JPEG
|
||||
const jpegBuffer = await smartJimpInstance.computeAssetVariation(
|
||||
Buffer.from(pngBuffer),
|
||||
{
|
||||
format: 'jpeg',
|
||||
progressive: true,
|
||||
// SmartJimp uses a different quality scale, need to check if adjustment is needed
|
||||
// For now, pass through the quality value
|
||||
quality
|
||||
}
|
||||
);
|
||||
|
||||
jpegBuffers.push(new Uint8Array(jpegBuffer));
|
||||
}
|
||||
|
||||
return jpegBuffers;
|
||||
}
|
||||
}
|
@@ -13,6 +13,7 @@ import * as smartpath from '@push.rocks/smartpath';
|
||||
import * as smartpuppeteer from '@push.rocks/smartpuppeteer';
|
||||
import * as smartnetwork from '@push.rocks/smartnetwork';
|
||||
import * as smartunique from '@push.rocks/smartunique';
|
||||
import * as smartjimp from '@push.rocks/smartjimp';
|
||||
|
||||
export {
|
||||
smartbuffer,
|
||||
@@ -23,6 +24,7 @@ export {
|
||||
smartpuppeteer,
|
||||
smartunique,
|
||||
smartnetwork,
|
||||
smartjimp,
|
||||
};
|
||||
|
||||
// tsclass scope
|
||||
|
Reference in New Issue
Block a user