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:
107
test/test.ts
107
test/test.ts
@@ -174,6 +174,113 @@ tap.test('should verify WebP files are smaller than PNG', async () => {
|
||||
expect(totalWebpSize).toBeLessThan(totalPngSize);
|
||||
});
|
||||
|
||||
tap.test('should create JPEG images from PDF', async () => {
|
||||
const pdfObject = await testSmartPdf.readFileToPdfObject('.nogit/3.pdf');
|
||||
const jpegImages = await testSmartPdf.convertPDFToJpegBytes(pdfObject.buffer);
|
||||
expect(jpegImages.length).toBeGreaterThan(0);
|
||||
console.log('JPEG image sizes:', jpegImages.map(img => img.length));
|
||||
|
||||
// Save the first page as JPEG
|
||||
fs.writeFileSync(path.join(testResultsDir, 'jpeg_default_page1.jpg'), Buffer.from(jpegImages[0]));
|
||||
});
|
||||
|
||||
tap.test('should create JPEG images with different quality levels', async () => {
|
||||
const pdfObject = await testSmartPdf.readFileToPdfObject('.nogit/3.pdf');
|
||||
|
||||
// Test different quality levels
|
||||
const qualityLevels = [50, 70, 85, 95];
|
||||
|
||||
for (const quality of qualityLevels) {
|
||||
const jpegImages = await testSmartPdf.convertPDFToJpegBytes(pdfObject.buffer, {
|
||||
scale: smartpdf.SmartPdf.SCALE_HIGH,
|
||||
quality: quality
|
||||
});
|
||||
|
||||
console.log(`JPEG quality ${quality}: ${jpegImages[0].length} bytes`);
|
||||
|
||||
// Save first page at each quality level
|
||||
fs.writeFileSync(
|
||||
path.join(testResultsDir, `jpeg_quality_${quality}_page1.jpg`),
|
||||
Buffer.from(jpegImages[0])
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should create JPEG images with max dimensions', async () => {
|
||||
const pdfObject = await testSmartPdf.readFileToPdfObject('.nogit/3.pdf');
|
||||
|
||||
// Create constrained JPEG images
|
||||
const constrainedJpegs = await testSmartPdf.convertPDFToJpegBytes(pdfObject.buffer, {
|
||||
scale: smartpdf.SmartPdf.SCALE_HIGH,
|
||||
quality: 85,
|
||||
maxWidth: 1200,
|
||||
maxHeight: 1200
|
||||
});
|
||||
|
||||
expect(constrainedJpegs.length).toBeGreaterThan(0);
|
||||
console.log('Constrained JPEG sizes:', constrainedJpegs.map(img => img.length));
|
||||
|
||||
// Save constrained JPEG
|
||||
fs.writeFileSync(path.join(testResultsDir, 'jpeg_constrained_page1.jpg'), Buffer.from(constrainedJpegs[0]));
|
||||
});
|
||||
|
||||
tap.test('should compare file sizes between PNG, WebP, and JPEG', async () => {
|
||||
const pdfObject = await testSmartPdf.readFileToPdfObject('.nogit/3.pdf');
|
||||
|
||||
// Generate all three formats at the same scale
|
||||
const comparisonScale = smartpdf.SmartPdf.SCALE_HIGH; // 3.0 scale
|
||||
|
||||
const pngImages = await testSmartPdf.convertPDFToPngBytes(pdfObject.buffer, {
|
||||
scale: comparisonScale
|
||||
});
|
||||
const webpImages = await testSmartPdf.convertPDFToWebpBytes(pdfObject.buffer, {
|
||||
scale: comparisonScale,
|
||||
quality: 85
|
||||
});
|
||||
const jpegImages = await testSmartPdf.convertPDFToJpegBytes(pdfObject.buffer, {
|
||||
scale: comparisonScale,
|
||||
quality: 85
|
||||
});
|
||||
|
||||
expect(pngImages.length).toEqual(webpImages.length);
|
||||
expect(pngImages.length).toEqual(jpegImages.length);
|
||||
|
||||
// Compare sizes
|
||||
let totalPngSize = 0;
|
||||
let totalWebpSize = 0;
|
||||
let totalJpegSize = 0;
|
||||
|
||||
pngImages.forEach((png, index) => {
|
||||
const pngSize = png.length;
|
||||
const webpSize = webpImages[index].length;
|
||||
const jpegSize = jpegImages[index].length;
|
||||
|
||||
totalPngSize += pngSize;
|
||||
totalWebpSize += webpSize;
|
||||
totalJpegSize += jpegSize;
|
||||
|
||||
const webpReduction = ((pngSize - webpSize) / pngSize * 100).toFixed(1);
|
||||
const jpegReduction = ((pngSize - jpegSize) / pngSize * 100).toFixed(1);
|
||||
|
||||
console.log(`Page ${index + 1}:`);
|
||||
console.log(` PNG: ${pngSize} bytes`);
|
||||
console.log(` WebP: ${webpSize} bytes (${webpReduction}% smaller than PNG)`);
|
||||
console.log(` JPEG: ${jpegSize} bytes (${jpegReduction}% smaller than PNG)`);
|
||||
});
|
||||
|
||||
const totalWebpReduction = ((totalPngSize - totalWebpSize) / totalPngSize * 100).toFixed(1);
|
||||
const totalJpegReduction = ((totalPngSize - totalJpegSize) / totalPngSize * 100).toFixed(1);
|
||||
|
||||
console.log('\nTotal size comparison:');
|
||||
console.log(`PNG: ${totalPngSize} bytes`);
|
||||
console.log(`WebP: ${totalWebpSize} bytes (${totalWebpReduction}% reduction)`);
|
||||
console.log(`JPEG: ${totalJpegSize} bytes (${totalJpegReduction}% reduction)`);
|
||||
|
||||
// JPEG and WebP should both be smaller than PNG
|
||||
expect(totalJpegSize).toBeLessThan(totalPngSize);
|
||||
expect(totalWebpSize).toBeLessThan(totalPngSize);
|
||||
});
|
||||
|
||||
tap.test('should close the SmartPdf instance properly', async () => {
|
||||
await testSmartPdf.stop();
|
||||
});
|
||||
|
Reference in New Issue
Block a user