Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 25a8449fea | |||
| 42a6c57a68 | |||
| 2eb887dce7 |
@@ -0,0 +1,41 @@
|
|||||||
|
{
|
||||||
|
"@git.zone/cli": {
|
||||||
|
"projectType": "npm",
|
||||||
|
"module": {
|
||||||
|
"githost": "code.foss.global",
|
||||||
|
"gitscope": "push.rocks",
|
||||||
|
"gitrepo": "smartjimp",
|
||||||
|
"shortDescription": "image processing with sharp and jimp",
|
||||||
|
"npmPackagename": "@push.rocks/smartjimp",
|
||||||
|
"license": "MIT",
|
||||||
|
"description": "A TypeScript library for image processing combining both sharp and jimp libraries.",
|
||||||
|
"keywords": [
|
||||||
|
"image processing",
|
||||||
|
"TypeScript",
|
||||||
|
"sharp",
|
||||||
|
"jimp",
|
||||||
|
"resize image",
|
||||||
|
"convert image format",
|
||||||
|
"image caching",
|
||||||
|
"buffer manipulation",
|
||||||
|
"AVIF",
|
||||||
|
"WEBP",
|
||||||
|
"PNG"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"release": {
|
||||||
|
"registries": [
|
||||||
|
"https://verdaccio.lossless.digital",
|
||||||
|
"https://registry.npmjs.org"
|
||||||
|
],
|
||||||
|
"accessLevel": "public"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"@git.zone/tsdoc": {
|
||||||
|
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
||||||
|
},
|
||||||
|
"@ship.zone/szci": {
|
||||||
|
"npmGlobalTools": [],
|
||||||
|
"npmRegistryUrl": "registry.npmjs.org"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,28 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 2026-05-01 - 1.2.1 - fix(build)
|
||||||
|
modernize project configuration and tighten smartjimp TypeScript compatibility
|
||||||
|
|
||||||
|
- migrate npm metadata to the new @git.zone/@ship.zone config structure and add .smartconfig.json
|
||||||
|
- update package scripts and dependency versions for the current toolchain
|
||||||
|
- improve SmartJimp typings and error handling, including optional variation support and typed sharp options
|
||||||
|
- move tests to a Node-specific entry and adapt them to the current smartfile API
|
||||||
|
- enable stricter TypeScript settings with noImplicitAny and explicit node types
|
||||||
|
|
||||||
|
## [1.2.0] - 2025-01-02
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Quality parameter support for image compression
|
||||||
|
- New `quality` option (1-100) in IAssetVariation interface for controlling compression quality
|
||||||
|
- Quality control implementation for both Sharp and Jimp modes
|
||||||
|
- Support for quality settings on all lossy formats (JPEG, WebP, AVIF)
|
||||||
|
- Enhanced documentation with quality control examples
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- Jimp mode now uses fall-through pattern in format switch for cleaner code
|
||||||
|
- Console logging when Jimp falls back to JPEG for unsupported formats (WebP, AVIF)
|
||||||
|
- Updated examples in documentation to showcase quality parameter usage
|
||||||
|
|
||||||
## [1.1.0] - 2025-01-02
|
## [1.1.0] - 2025-01-02
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
Copyright (c) 2020 Task Venture Capital GmbH (hello@lossless.com)
|
Copyright (c) 2020 Task Venture Capital GmbH <hello@task.vc>
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|||||||
+15
-8
@@ -1,14 +1,14 @@
|
|||||||
{
|
{
|
||||||
"gitzone": {
|
"@git.zone/cli": {
|
||||||
"projectType": "npm",
|
"projectType": "npm",
|
||||||
"module": {
|
"module": {
|
||||||
"githost": "code.foss.global",
|
"githost": "code.foss.global",
|
||||||
"gitscope": "push.rocks",
|
"gitscope": "push.rocks",
|
||||||
"gitrepo": "smartjimp",
|
"gitrepo": "smartjimp",
|
||||||
"description": "A TypeScript library for image processing combining both sharp and jimp libraries.",
|
"shortDescription": "image processing with sharp and jimp",
|
||||||
"npmPackagename": "@push.rocks/smartjimp",
|
"npmPackagename": "@push.rocks/smartjimp",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"projectDomain": "push.rocks",
|
"description": "A TypeScript library for image processing combining both sharp and jimp libraries.",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"image processing",
|
"image processing",
|
||||||
"TypeScript",
|
"TypeScript",
|
||||||
@@ -22,13 +22,20 @@
|
|||||||
"WEBP",
|
"WEBP",
|
||||||
"PNG"
|
"PNG"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
"release": {
|
||||||
|
"registries": [
|
||||||
|
"https://verdaccio.lossless.digital",
|
||||||
|
"https://registry.npmjs.org"
|
||||||
|
],
|
||||||
|
"accessLevel": "public"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"npmci": {
|
"@git.zone/tsdoc": {
|
||||||
"npmGlobalTools": [],
|
|
||||||
"npmAccessLevel": "public"
|
|
||||||
},
|
|
||||||
"tsdoc": {
|
|
||||||
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
||||||
|
},
|
||||||
|
"@ship.zone/szci": {
|
||||||
|
"npmGlobalTools": [],
|
||||||
|
"npmRegistryUrl": "registry.npmjs.org"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+19
-16
@@ -1,32 +1,33 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/smartjimp",
|
"name": "@push.rocks/smartjimp",
|
||||||
"version": "1.1.0",
|
"version": "1.2.1",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "A TypeScript library for image processing combining both sharp and jimp libraries.",
|
"description": "A TypeScript library for image processing combining both sharp and jimp libraries.",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist_ts/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
"author": "Lossless GmbH",
|
"author": "Task Venture Capital GmbH <hello@task.vc>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/)",
|
"test": "tstest test/",
|
||||||
"build": "(tsbuild --allowimplicitany)",
|
"format": "gitzone format",
|
||||||
"buildDocs": "tsdoc",
|
"build": "tsbuild",
|
||||||
"localPublish": "gitzone commit && pnpm run build && pnpm publish && pnpm publish --access public --registry=\"https://registry.npmjs.org\""
|
"buildDocs": "tsdoc"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@git.zone/tsbuild": "^2.1.72",
|
"@git.zone/tsbuild": "^4.4.0",
|
||||||
"@git.zone/tsrun": "^1.2.44",
|
"@git.zone/tsrun": "^2.0.3",
|
||||||
"@git.zone/tstest": "^2.3.2",
|
"@git.zone/tstest": "^3.6.3",
|
||||||
"@types/node": "^20.11.17"
|
"@types/node": "^25.6.0",
|
||||||
|
"@types/pngjs": "^6.0.5"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@push.rocks/levelcache": "^3.0.8",
|
"@push.rocks/levelcache": "^3.2.2",
|
||||||
"@push.rocks/smartfile": "^11.0.4",
|
"@push.rocks/smartfile": "^13.1.3",
|
||||||
"@push.rocks/smarthash": "^3.0.4",
|
"@push.rocks/smarthash": "^3.2.7",
|
||||||
"@push.rocks/smartpath": "^6.0.0",
|
"@push.rocks/smartpath": "^6.0.0",
|
||||||
"@push.rocks/smartrequest": "^4.2.1",
|
"@push.rocks/smartrequest": "^5.0.1",
|
||||||
"jimp": "^1.6.0",
|
"jimp": "^1.6.1",
|
||||||
"sharp": "^0.34.3"
|
"sharp": "^0.34.5"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
@@ -37,6 +38,8 @@
|
|||||||
"dist_ts_web/**/*",
|
"dist_ts_web/**/*",
|
||||||
"assets/**/*",
|
"assets/**/*",
|
||||||
"cli.js",
|
"cli.js",
|
||||||
|
".smartconfig.json",
|
||||||
|
"license",
|
||||||
"npmextra.json",
|
"npmextra.json",
|
||||||
"readme.md"
|
"readme.md"
|
||||||
],
|
],
|
||||||
|
|||||||
Generated
+3058
-4835
File diff suppressed because it is too large
Load Diff
@@ -93,6 +93,38 @@ const avifBuffer = await processor.getFromSmartfile(imageFile, {
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 🎚️ Quality Control
|
||||||
|
Fine-tune image compression quality for the perfect balance between file size and visual fidelity:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// High quality JPEG (larger file size)
|
||||||
|
const highQualityJpeg = await processor.getFromSmartfile(imageFile, {
|
||||||
|
format: 'jpeg',
|
||||||
|
quality: 90 // 1-100, higher = better quality
|
||||||
|
});
|
||||||
|
|
||||||
|
// Optimized for web (smaller file size)
|
||||||
|
const webOptimized = await processor.getFromSmartfile(imageFile, {
|
||||||
|
format: 'jpeg',
|
||||||
|
quality: 75, // Good balance for web
|
||||||
|
progressive: true
|
||||||
|
});
|
||||||
|
|
||||||
|
// Ultra-compressed WebP
|
||||||
|
const tinyWebP = await processor.getFromSmartfile(imageFile, {
|
||||||
|
format: 'webp',
|
||||||
|
quality: 60 // WebP handles lower quality better than JPEG
|
||||||
|
});
|
||||||
|
|
||||||
|
// Quality works with all lossy formats
|
||||||
|
const qualityAvif = await processor.getFromSmartfile(imageFile, {
|
||||||
|
format: 'avif',
|
||||||
|
quality: 80 // AVIF provides excellent quality even at lower values
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
> 💡 **Pro tip**: Different formats handle quality settings differently. AVIF and WebP generally look better than JPEG at the same quality level.
|
||||||
|
|
||||||
### 📸 Progressive JPEG Support
|
### 📸 Progressive JPEG Support
|
||||||
Create progressive JPEGs that load in multiple passes for better perceived performance:
|
Create progressive JPEGs that load in multiple passes for better perceived performance:
|
||||||
|
|
||||||
@@ -138,13 +170,15 @@ Work directly with buffers for maximum flexibility:
|
|||||||
const processedBuffer = await processor.computeAssetVariation(imageBuffer, {
|
const processedBuffer = await processor.computeAssetVariation(imageBuffer, {
|
||||||
width: 300,
|
width: 300,
|
||||||
format: 'webp',
|
format: 'webp',
|
||||||
|
quality: 85,
|
||||||
invert: true
|
invert: true
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create progressive JPEG from buffer
|
// Create progressive JPEG from buffer
|
||||||
const progressiveBuffer = await processor.computeAssetVariation(imageBuffer, {
|
const progressiveBuffer = await processor.computeAssetVariation(imageBuffer, {
|
||||||
format: 'jpeg',
|
format: 'jpeg',
|
||||||
progressive: true
|
progressive: true,
|
||||||
|
quality: 88
|
||||||
});
|
});
|
||||||
|
|
||||||
// Special AVIF creation method (sharp mode only)
|
// Special AVIF creation method (sharp mode only)
|
||||||
@@ -251,20 +285,23 @@ async function optimizeForWeb(sourceImage: SmartFile) {
|
|||||||
// Modern browsers: AVIF
|
// Modern browsers: AVIF
|
||||||
avif: await processor.getFromSmartfile(sourceImage, {
|
avif: await processor.getFromSmartfile(sourceImage, {
|
||||||
format: 'avif',
|
format: 'avif',
|
||||||
width: 1200
|
width: 1200,
|
||||||
|
quality: 85 // AVIF excels at lower quality settings
|
||||||
}),
|
}),
|
||||||
|
|
||||||
// Good browser support: WebP
|
// Good browser support: WebP
|
||||||
webp: await processor.getFromSmartfile(sourceImage, {
|
webp: await processor.getFromSmartfile(sourceImage, {
|
||||||
format: 'webp',
|
format: 'webp',
|
||||||
width: 1200
|
width: 1200,
|
||||||
|
quality: 82 // WebP sweet spot for quality/size
|
||||||
}),
|
}),
|
||||||
|
|
||||||
// Universal fallback: Progressive JPEG
|
// Universal fallback: Progressive JPEG
|
||||||
jpeg: await processor.getFromSmartfile(sourceImage, {
|
jpeg: await processor.getFromSmartfile(sourceImage, {
|
||||||
format: 'jpeg',
|
format: 'jpeg',
|
||||||
progressive: true,
|
progressive: true,
|
||||||
width: 1200
|
width: 1200,
|
||||||
|
quality: 80 // Standard web quality
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -289,6 +326,7 @@ interface IAssetVariation {
|
|||||||
height?: number; // Target height (maintains aspect ratio if width not set)
|
height?: number; // Target height (maintains aspect ratio if width not set)
|
||||||
invert?: boolean; // Invert colors (jimp only currently)
|
invert?: boolean; // Invert colors (jimp only currently)
|
||||||
progressive?: boolean; // Create progressive JPEG (sharp only, jpeg format only)
|
progressive?: boolean; // Create progressive JPEG (sharp only, jpeg format only)
|
||||||
|
quality?: number; // Compression quality 1-100 (lossy formats only)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||||
|
import * as smartjimp from '../ts/index.js';
|
||||||
|
import * as smartfile from '@push.rocks/smartfile';
|
||||||
|
|
||||||
|
const smartFileFactory = smartfile.SmartFileFactory.nodeFs();
|
||||||
|
const sourceImageUrl = 'https://images.unsplash.com/photo-1673276628202-737bf3020ac2?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=3774&q=80';
|
||||||
|
|
||||||
|
let testSmartJimp: smartjimp.SmartJimp;
|
||||||
|
|
||||||
|
tap.test('first test', async () => {
|
||||||
|
testSmartJimp = new smartjimp.SmartJimp({ mode: 'sharp' });
|
||||||
|
expect(testSmartJimp).toBeInstanceOf(smartjimp.SmartJimp);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should be able to create a master', async () => {
|
||||||
|
const smartfileInstance = await smartFileFactory.fromUrl(sourceImageUrl);
|
||||||
|
const convertedAsset = await testSmartJimp.getFromSmartfile(smartfileInstance, { width: 500 });
|
||||||
|
await smartFileFactory.fromBuffer('.nogit/result.avif', convertedAsset).write();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should be able to use jimp', async () => {
|
||||||
|
const testSmartJimp = new smartjimp.SmartJimp({ mode: 'jimp' });
|
||||||
|
const smartfileInstance = await smartFileFactory.fromUrl(sourceImageUrl);
|
||||||
|
const convertedAsset = await testSmartJimp.computeAssetVariation(smartfileInstance.contentBuffer, {
|
||||||
|
format: 'png',
|
||||||
|
});
|
||||||
|
await smartFileFactory.fromBuffer('.nogit/result2.png', convertedAsset).write();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should create progressive JPEG with Sharp', async () => {
|
||||||
|
const testSmartJimp = new smartjimp.SmartJimp({ mode: 'sharp' });
|
||||||
|
const smartfileInstance = await smartFileFactory.fromUrl(sourceImageUrl);
|
||||||
|
|
||||||
|
// Create progressive JPEG
|
||||||
|
const progressiveJpeg = await testSmartJimp.getFromSmartfile(smartfileInstance, {
|
||||||
|
format: 'jpeg',
|
||||||
|
progressive: true,
|
||||||
|
width: 800,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(progressiveJpeg).toBeInstanceOf(Buffer);
|
||||||
|
await smartFileFactory.fromBuffer('.nogit/progressive.jpg', progressiveJpeg).write();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should convert PNG to progressive JPEG', async () => {
|
||||||
|
const testSmartJimp = new smartjimp.SmartJimp({ mode: 'sharp' });
|
||||||
|
|
||||||
|
// First create a PNG
|
||||||
|
const smartfileInstance = await smartFileFactory.fromUrl(sourceImageUrl);
|
||||||
|
const pngBuffer = await testSmartJimp.computeAssetVariation(smartfileInstance.contentBuffer, {
|
||||||
|
format: 'png',
|
||||||
|
width: 500,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Then convert it to progressive JPEG
|
||||||
|
const progressiveJpeg = await testSmartJimp.computeAssetVariation(pngBuffer, {
|
||||||
|
format: 'jpeg',
|
||||||
|
progressive: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(progressiveJpeg).toBeInstanceOf(Buffer);
|
||||||
|
await smartFileFactory.fromBuffer('.nogit/png-to-progressive.jpg', progressiveJpeg).write();
|
||||||
|
});
|
||||||
|
|
||||||
|
export default tap.start();
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
|
||||||
import * as smartjimp from '../ts/index.js';
|
|
||||||
import * as smartfile from '@push.rocks/smartfile';
|
|
||||||
|
|
||||||
let testSmartJimp: smartjimp.SmartJimp;
|
|
||||||
|
|
||||||
tap.test('first test', async () => {
|
|
||||||
testSmartJimp = new smartjimp.SmartJimp({ mode: 'sharp'});
|
|
||||||
expect(testSmartJimp).toBeInstanceOf(smartjimp.SmartJimp);
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('should be able to create a master', async () => {
|
|
||||||
const smartfileInstance = await smartfile.SmartFile.fromUrl('https://images.unsplash.com/photo-1673276628202-737bf3020ac2?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=3774&q=80')
|
|
||||||
const convertedAsset = await testSmartJimp.getFromSmartfile(smartfileInstance, {width: 500});
|
|
||||||
(await smartfile.SmartFile.fromBuffer('.nogit/result.avif', convertedAsset)).write();
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('should be able to use jimp', async () => {
|
|
||||||
const testSmartJimp = new smartjimp.SmartJimp({ mode: 'jimp'});
|
|
||||||
const smartfileInstance = await smartfile.SmartFile.fromUrl('https://images.unsplash.com/photo-1673276628202-737bf3020ac2?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=3774&q=80')
|
|
||||||
const convertedAsset = await testSmartJimp.computeAssetVariation(smartfileInstance.contentBuffer, {
|
|
||||||
format: 'png',
|
|
||||||
});
|
|
||||||
(await smartfile.SmartFile.fromBuffer('.nogit/result2.png', convertedAsset)).write();
|
|
||||||
})
|
|
||||||
|
|
||||||
tap.test('should create progressive JPEG with Sharp', async () => {
|
|
||||||
const testSmartJimp = new smartjimp.SmartJimp({ mode: 'sharp'});
|
|
||||||
const smartfileInstance = await smartfile.SmartFile.fromUrl('https://images.unsplash.com/photo-1673276628202-737bf3020ac2?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=3774&q=80')
|
|
||||||
|
|
||||||
// Create progressive JPEG
|
|
||||||
const progressiveJpeg = await testSmartJimp.getFromSmartfile(smartfileInstance, {
|
|
||||||
format: 'jpeg',
|
|
||||||
progressive: true,
|
|
||||||
width: 800
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(progressiveJpeg).toBeInstanceOf(Buffer);
|
|
||||||
(await smartfile.SmartFile.fromBuffer('.nogit/progressive.jpg', progressiveJpeg)).write();
|
|
||||||
})
|
|
||||||
|
|
||||||
tap.test('should convert PNG to progressive JPEG', async () => {
|
|
||||||
const testSmartJimp = new smartjimp.SmartJimp({ mode: 'sharp'});
|
|
||||||
|
|
||||||
// First create a PNG
|
|
||||||
const smartfileInstance = await smartfile.SmartFile.fromUrl('https://images.unsplash.com/photo-1673276628202-737bf3020ac2?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=3774&q=80')
|
|
||||||
const pngBuffer = await testSmartJimp.computeAssetVariation(smartfileInstance.contentBuffer, {
|
|
||||||
format: 'png',
|
|
||||||
width: 500
|
|
||||||
});
|
|
||||||
|
|
||||||
// Then convert it to progressive JPEG
|
|
||||||
const progressiveJpeg = await testSmartJimp.computeAssetVariation(pngBuffer, {
|
|
||||||
format: 'jpeg',
|
|
||||||
progressive: true
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(progressiveJpeg).toBeInstanceOf(Buffer);
|
|
||||||
(await smartfile.SmartFile.fromBuffer('.nogit/png-to-progressive.jpg', progressiveJpeg)).write();
|
|
||||||
})
|
|
||||||
|
|
||||||
tap.start();
|
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* autocreated commitinfo by @pushrocks/commitinfo
|
* autocreated commitinfo by @push.rocks/commitinfo
|
||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartjimp',
|
name: '@push.rocks/smartjimp',
|
||||||
version: '1.0.18',
|
version: '1.2.1',
|
||||||
description: 'a tool fr working with images in TypeScript'
|
description: 'A TypeScript library for image processing combining both sharp and jimp libraries.'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ export interface IAssetVariation {
|
|||||||
height?: number;
|
height?: number;
|
||||||
invert?: boolean;
|
invert?: boolean;
|
||||||
progressive?: boolean;
|
progressive?: boolean;
|
||||||
|
quality?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ISmartJimpOptions {
|
export interface ISmartJimpOptions {
|
||||||
@@ -32,7 +33,7 @@ export class SmartJimp {
|
|||||||
sourceTypeArg: 'streamfile' | 'smartfile',
|
sourceTypeArg: 'streamfile' | 'smartfile',
|
||||||
sourceIdArg: string,
|
sourceIdArg: string,
|
||||||
assetVariationArg?: IAssetVariation
|
assetVariationArg?: IAssetVariation
|
||||||
) {
|
): string {
|
||||||
return `${sourceTypeArg}_${sourceIdArg}_${
|
return `${sourceTypeArg}_${sourceIdArg}_${
|
||||||
assetVariationArg
|
assetVariationArg
|
||||||
? `${assetVariationArg.width || 'auto'}x${assetVariationArg.height || 'auto'}`
|
? `${assetVariationArg.width || 'auto'}x${assetVariationArg.height || 'auto'}`
|
||||||
@@ -40,7 +41,7 @@ export class SmartJimp {
|
|||||||
}`;
|
}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
sharpMod: typeof pluginsTyped.sharpType.default;
|
private sharpMod?: typeof pluginsTyped.sharpType.default;
|
||||||
public async getSharpMod(): Promise<
|
public async getSharpMod(): Promise<
|
||||||
typeof pluginsTyped.sharpType.default
|
typeof pluginsTyped.sharpType.default
|
||||||
> {
|
> {
|
||||||
@@ -50,7 +51,7 @@ export class SmartJimp {
|
|||||||
return this.sharpMod;
|
return this.sharpMod;
|
||||||
}
|
}
|
||||||
|
|
||||||
jimpMod: typeof pluginsTyped.jimpType;
|
private jimpMod?: typeof pluginsTyped.jimpType;
|
||||||
public async getJimpMod(): Promise<typeof pluginsTyped.jimpType> {
|
public async getJimpMod(): Promise<typeof pluginsTyped.jimpType> {
|
||||||
if (!this.jimpMod) {
|
if (!this.jimpMod) {
|
||||||
this.jimpMod = await import('jimp');
|
this.jimpMod = await import('jimp');
|
||||||
@@ -58,12 +59,16 @@ export class SmartJimp {
|
|||||||
return this.jimpMod;
|
return this.jimpMod;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async computeAssetVariation(assetBufferArg: Buffer, assetVariationArg: IAssetVariation) {
|
public async computeAssetVariation(
|
||||||
if (this.options.mode === 'sharp') {
|
assetBufferArg: Buffer,
|
||||||
const sharp = await this.getSharpMod();
|
assetVariationArg?: IAssetVariation
|
||||||
|
): Promise<Buffer> {
|
||||||
if (!assetVariationArg) {
|
if (!assetVariationArg) {
|
||||||
return assetBufferArg;
|
return assetBufferArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.options.mode === 'sharp') {
|
||||||
|
const sharp = await this.getSharpMod();
|
||||||
let sharpImage = sharp(assetBufferArg);
|
let sharpImage = sharp(assetBufferArg);
|
||||||
sharpImage = sharpImage.resize(assetVariationArg.width, assetVariationArg.height);
|
sharpImage = sharpImage.resize(assetVariationArg.width, assetVariationArg.height);
|
||||||
const resultResize = sharpImage.resize(assetVariationArg.width, assetVariationArg.height);
|
const resultResize = sharpImage.resize(assetVariationArg.width, assetVariationArg.height);
|
||||||
@@ -72,56 +77,83 @@ export class SmartJimp {
|
|||||||
}
|
}
|
||||||
switch (assetVariationArg.format) {
|
switch (assetVariationArg.format) {
|
||||||
case 'avif':
|
case 'avif':
|
||||||
sharpImage = resultResize.avif();
|
const avifOptions: pluginsTyped.sharpType.AvifOptions = {};
|
||||||
|
if (assetVariationArg.quality !== undefined) {
|
||||||
|
avifOptions.quality = assetVariationArg.quality;
|
||||||
|
}
|
||||||
|
sharpImage = resultResize.avif(avifOptions);
|
||||||
break;
|
break;
|
||||||
case 'webp':
|
case 'webp':
|
||||||
sharpImage = resultResize.webp();
|
const webpOptions: pluginsTyped.sharpType.WebpOptions = {};
|
||||||
|
if (assetVariationArg.quality !== undefined) {
|
||||||
|
webpOptions.quality = assetVariationArg.quality;
|
||||||
|
}
|
||||||
|
sharpImage = resultResize.webp(webpOptions);
|
||||||
break;
|
break;
|
||||||
case 'png':
|
case 'png':
|
||||||
sharpImage = resultResize.png();
|
const pngOptions: pluginsTyped.sharpType.PngOptions = {};
|
||||||
|
if (assetVariationArg.quality !== undefined) {
|
||||||
|
pngOptions.quality = assetVariationArg.quality;
|
||||||
|
}
|
||||||
|
sharpImage = resultResize.png(pngOptions);
|
||||||
break;
|
break;
|
||||||
case 'jpeg':
|
case 'jpeg':
|
||||||
sharpImage = resultResize.jpeg({
|
const jpegOptions: pluginsTyped.sharpType.JpegOptions = {
|
||||||
progressive: assetVariationArg.progressive || false
|
progressive: assetVariationArg.progressive || false,
|
||||||
});
|
};
|
||||||
|
if (assetVariationArg.quality !== undefined) {
|
||||||
|
jpegOptions.quality = assetVariationArg.quality;
|
||||||
|
}
|
||||||
|
sharpImage = resultResize.jpeg(jpegOptions);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// Default to JPEG
|
// Default to JPEG
|
||||||
sharpImage = resultResize.jpeg({
|
const defaultJpegOptions: pluginsTyped.sharpType.JpegOptions = {
|
||||||
progressive: assetVariationArg.progressive || false
|
progressive: assetVariationArg.progressive || false,
|
||||||
});
|
};
|
||||||
|
if (assetVariationArg.quality !== undefined) {
|
||||||
|
defaultJpegOptions.quality = assetVariationArg.quality;
|
||||||
|
}
|
||||||
|
sharpImage = resultResize.jpeg(defaultJpegOptions);
|
||||||
}
|
}
|
||||||
return sharpImage.toBuffer();
|
return sharpImage.toBuffer();
|
||||||
} else if (this.options.mode === 'jimp') {
|
} else if (this.options.mode === 'jimp') {
|
||||||
const jimpMod = await this.getJimpMod();
|
const jimpMod = await this.getJimpMod();
|
||||||
let jimpImage = await jimpMod.Jimp.read(assetBufferArg);
|
let jimpImage = await jimpMod.Jimp.read(assetBufferArg);
|
||||||
if (assetVariationArg.width || assetVariationArg.height) {
|
if (assetVariationArg.width || assetVariationArg.height) {
|
||||||
const resizeOptions: any = {};
|
if (assetVariationArg.width && assetVariationArg.height) {
|
||||||
if (assetVariationArg.width) resizeOptions.w = assetVariationArg.width;
|
jimpImage.resize({ w: assetVariationArg.width, h: assetVariationArg.height });
|
||||||
if (assetVariationArg.height) resizeOptions.h = assetVariationArg.height;
|
} else if (assetVariationArg.width) {
|
||||||
jimpImage.resize(resizeOptions);
|
jimpImage.resize({ w: assetVariationArg.width });
|
||||||
|
} else if (assetVariationArg.height) {
|
||||||
|
jimpImage.resize({ h: assetVariationArg.height });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (assetVariationArg.invert) {
|
if (assetVariationArg.invert) {
|
||||||
jimpImage.invert();
|
jimpImage.invert();
|
||||||
}
|
}
|
||||||
// Note: Jimp does not support progressive JPEG encoding
|
// Note: Jimp does not support progressive JPEG encoding
|
||||||
// Progressive option is ignored in jimp mode
|
// Progressive option is ignored in jimp mode
|
||||||
|
const jpegOptions: { quality?: number } = {};
|
||||||
|
if (assetVariationArg.quality !== undefined) {
|
||||||
|
jpegOptions.quality = assetVariationArg.quality;
|
||||||
|
}
|
||||||
|
|
||||||
switch (assetVariationArg.format) {
|
switch (assetVariationArg.format) {
|
||||||
case 'png':
|
case 'png':
|
||||||
return await jimpImage.getBuffer("image/png");
|
return await jimpImage.getBuffer('image/png');
|
||||||
case 'jpeg':
|
|
||||||
return await jimpImage.getBuffer("image/jpeg");
|
|
||||||
case 'webp':
|
case 'webp':
|
||||||
// Jimp doesn't support WebP, fallback to JPEG
|
|
||||||
return await jimpImage.getBuffer("image/jpeg");
|
|
||||||
case 'avif':
|
case 'avif':
|
||||||
// Jimp doesn't support AVIF, fallback to JPEG
|
console.log(`Jimp doesn't support ${assetVariationArg.format}, falling back to JPEG`);
|
||||||
return await jimpImage.getBuffer("image/jpeg");
|
// Fall through to JPEG
|
||||||
|
case 'jpeg':
|
||||||
default:
|
default:
|
||||||
// Default to JPEG
|
// Default to JPEG
|
||||||
return await jimpImage.getBuffer("image/jpeg");
|
return await jimpImage.getBuffer('image/jpeg', jpegOptions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new Error(`Unsupported smartjimp mode: ${this.options.mode}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getFromSmartfile(
|
public async getFromSmartfile(
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import * as plugins from './plugins.js';
|
import * as plugins from './plugins.js';
|
||||||
|
|
||||||
export const packageDir = plugins.path.join(plugins.smartpath.get.dirnameFromImportMetaUrl(import.meta.url), '../');
|
export const packageDir = plugins.path.join(plugins.smartpath.get.dirnameFromImportMetaUrl(import.meta.url), '../');
|
||||||
export const nogitDir = plugins.path.join(packageDir, '.nogit/')
|
export const nogitDir = plugins.path.join(packageDir, '.nogit/');
|
||||||
|
|||||||
+4
-4
@@ -5,10 +5,10 @@
|
|||||||
"target": "ES2022",
|
"target": "ES2022",
|
||||||
"module": "NodeNext",
|
"module": "NodeNext",
|
||||||
"moduleResolution": "NodeNext",
|
"moduleResolution": "NodeNext",
|
||||||
|
"noImplicitAny": true,
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"verbatimModuleSyntax": true
|
"verbatimModuleSyntax": true,
|
||||||
|
"types": ["node"]
|
||||||
},
|
},
|
||||||
"exclude": [
|
"exclude": ["dist_*/**/*.d.ts"]
|
||||||
"dist_*/**/*.d.ts"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user