25 Commits

Author SHA1 Message Date
c919507375 update description 2024-05-29 14:13:46 +02:00
68e4d9cc74 update tsconfig 2024-04-14 17:44:47 +02:00
19b723146b update npmextra.json: githost 2024-04-01 21:35:40 +02:00
cc540ad39e update npmextra.json: githost 2024-04-01 19:58:32 +02:00
0b53a9514a update npmextra.json: githost 2024-03-30 21:47:30 +01:00
a2b408a7bf 1.0.18 2024-02-14 01:56:00 +01:00
67fdcf0362 fix(core): update 2024-02-14 01:56:00 +01:00
c3f840e1be 1.0.17 2023-12-03 23:32:48 +01:00
6840d94517 fix(core): update 2023-12-03 23:32:47 +01:00
4600cde549 1.0.16 2023-12-03 23:30:58 +01:00
33e8ba5f30 fix(core): update 2023-12-03 23:30:58 +01:00
8566c5f57f 1.0.15 2023-12-03 23:09:49 +01:00
4cc5accff1 fix(core): update 2023-12-03 23:09:48 +01:00
2b82a4b74a 1.0.14 2023-12-03 19:04:37 +01:00
499b7e5f42 fix(core): update 2023-12-03 19:04:36 +01:00
6b1948d834 1.0.13 2023-12-03 18:57:45 +01:00
7b340ea783 fix(core): update 2023-12-03 18:57:44 +01:00
063905bfaa 1.0.12 2023-12-03 18:41:44 +01:00
f1ad45289a fix(core): update 2023-12-03 18:41:43 +01:00
193e174fd7 1.0.11 2023-12-03 18:10:36 +01:00
703d21c426 fix(core): update 2023-12-03 18:10:35 +01:00
2d83ebac4d 1.0.10 2023-12-03 16:35:52 +01:00
7454a52d03 fix(core): update 2023-12-03 16:35:51 +01:00
1e93429186 1.0.9 2023-12-03 16:32:53 +01:00
ee926e18a7 fix(core): update 2023-12-03 16:32:52 +01:00
11 changed files with 1542 additions and 706 deletions

View File

@ -2,17 +2,33 @@
"gitzone": {
"projectType": "npm",
"module": {
"githost": "gitlab.com",
"githost": "code.foss.global",
"gitscope": "push.rocks",
"gitrepo": "smartjimp",
"description": "a tool fr working with images in TypeScript",
"description": "A TypeScript library for image processing combining both sharp and jimp libraries.",
"npmPackagename": "@push.rocks/smartjimp",
"license": "MIT",
"projectDomain": "push.rocks"
"projectDomain": "push.rocks",
"keywords": [
"image processing",
"TypeScript",
"sharp",
"jimp",
"resize image",
"convert image format",
"image caching",
"buffer manipulation",
"AVIF",
"WEBP",
"PNG"
]
}
},
"npmci": {
"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"
}
}

View File

@ -1,8 +1,8 @@
{
"name": "@push.rocks/smartjimp",
"version": "1.0.8",
"version": "1.0.18",
"private": false,
"description": "a tool fr working with images in TypeScript",
"description": "A TypeScript library for image processing combining both sharp and jimp libraries.",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"author": "Lossless GmbH",
@ -10,23 +10,24 @@
"scripts": {
"test": "(tstest test/)",
"build": "(tsbuild --allowimplicitany)",
"buildDocs": "tsdoc"
"buildDocs": "tsdoc",
"localPublish": "gitzone commit && pnpm run build && pnpm publish && pnpm publish --access public --registry=\"https://registry.npmjs.org\""
},
"devDependencies": {
"@git.zone/tsbuild": "^2.1.66",
"@git.zone/tsbuild": "^2.1.72",
"@git.zone/tsrun": "^1.2.44",
"@git.zone/tstest": "^1.0.77",
"@git.zone/tstest": "^1.0.86",
"@push.rocks/tapbundle": "^5.0.8",
"@types/node": "^20.10.0"
"@types/node": "^20.11.17"
},
"dependencies": {
"@push.rocks/levelcache": "^3.0.3",
"@push.rocks/levelcache": "^3.0.8",
"@push.rocks/smartfile": "^11.0.4",
"@push.rocks/smarthash": "^3.0.4",
"@push.rocks/smartpath": "^5.0.5",
"@push.rocks/smartrequest": "^2.0.15",
"@types/sharp": "^0.32.0",
"sharp": "^0.32.6"
"jimp": "^0.22.10",
"sharp": "^0.33.2"
},
"files": [
"ts/**/*",
@ -46,10 +47,23 @@
],
"repository": {
"type": "git",
"url": "git+https://gitlab.com/push.rocks/smartjimp.git"
"url": "https://code.foss.global/push.rocks/smartjimp.git"
},
"bugs": {
"url": "https://gitlab.com/push.rocks/smartjimp/issues"
},
"homepage": "https://gitlab.com/push.rocks/smartjimp#readme"
"homepage": "https://code.foss.global/push.rocks/smartjimp",
"keywords": [
"image processing",
"TypeScript",
"sharp",
"jimp",
"resize image",
"convert image format",
"image caching",
"buffer manipulation",
"AVIF",
"WEBP",
"PNG"
]
}

1929
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

1
readme.hints.md Normal file
View File

@ -0,0 +1 @@

118
readme.md
View File

@ -1,37 +1,105 @@
# @push.rocks/smartjimp
a tool fr working with images in TypeScript
## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@push.rocks/smartjimp)
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartjimp)
* [github.com (source mirror)](https://github.com/pushrocks/smartjimp)
* [docs (typedoc)](https://pushrocks.gitlab.io/smartjimp/)
## Install
## Status for master
To install `@push.rocks/smartjimp`, use the following command in your project directory:
Status Category | Status Badge
-- | --
GitLab Pipelines | [![pipeline status](https://gitlab.com/pushrocks/smartjimp/badges/master/pipeline.svg)](https://lossless.cloud)
GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/pushrocks/smartjimp/badges/master/coverage.svg)](https://lossless.cloud)
npm | [![npm downloads per month](https://badgen.net/npm/dy/@push.rocks/smartjimp)](https://lossless.cloud)
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/pushrocks/smartjimp)](https://lossless.cloud)
TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@push.rocks/smartjimp)](https://lossless.cloud)
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@push.rocks/smartjimp)](https://lossless.cloud)
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@push.rocks/smartjimp)](https://lossless.cloud)
```bash
npm install @push.rocks/smartjimp --save
```
## Usage
Use TypeScript for best in class intellisense
`@push.rocks/smartjimp` is a comprehensive TypeScript library for handling image processing with ease and efficiency. It leverages powerful image processing libraries such as `sharp` and `jimp`, providing a unified interface for performing common image manipulation tasks. This documentation aims to guide you through the installation and various functionalities of `@push.rocks/smartjimp`, ensuring you have the knowledge to integrate image processing capabilities into your TypeScript project effectively.
## Contribution
### Getting Started
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
First, import the `SmartJimp` class from the package:
For further information read the linked docs at the top of this readme.
```typescript
import { SmartJimp } from '@push.rocks/smartjimp';
```
## Legal
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
Next, create an instance of `SmartJimp` by specifying the preferred image processing mode (`sharp` or `jimp`):
```typescript
const imageProcessor = new SmartJimp({ mode: 'sharp' });
```
### Processing Images
#### From Buffer
You can process images from a buffer. This is particularly useful when you have image data already available in your application, such as uploaded images.
```typescript
import { SmartFile } from '@push.rocks/smartfile';
// Assuming you have an image buffer `imageBuffer`
let imageBuffer: Buffer;
// Convert the buffer to a SmartFile instance
const smartfileInstance = await SmartFile.fromBuffer(imageBuffer);
// Process the image, for example, resizing it to width 500px
const resizedImageBuffer = await imageProcessor.getFromSmartfile(smartfileInstance, {
width: 500,
});
```
#### Creating Specific Formats
`@push.rocks/smartjimp` supports creating images in various formats, including AVIF, WebP, and PNG.
```typescript
// For forcing a format, specify the `format` in the options, like 'png'
const pngBuffer = await imageProcessor.getFromSmartfile(smartfileInstance, {
width: 500,
format: 'png',
});
```
### Advanced Image Manipulation
#### Inversion
You can invert the colors of an image as follows:
```typescript
const invertedImageBuffer = await imageProcessor.getFromSmartfile(smartfileInstance, {
invert: true,
});
```
### Performance and Caching
`@push.rocks/smartjimp` employs caching mechanisms to enhance performance. Caching is handled internally, ensuring that repeated processing requests for the same image and settings are served faster by retrieving the processed image from the cache.
### Mode: Sharp vs. Jimp
- **Sharp**: Recommended for performance-critical applications. It is faster and supports a broader range of image formats. However, it relies on native libraries, which may need additional setup in certain environments.
- **Jimp**: A pure JavaScript image processing library. While not as fast as Sharp, it guarantees compatibility across all Node.js environments without the need for additional native dependencies.
### Conclusion
`@push.rocks/smartjimp` makes image processing in TypeScript applications highly accessible and efficient. Whether you need to resize, convert formats, or perform other image manipulations, `@push.rocks/smartjimp` provides a powerful yet simple API to accomplish these tasks with minimal effort. By abstracting away the complexities of direct interactions with libraries like `sharp` and `jimp`, it allows developers to focus more on building the core features of their applications.
## License and Legal Information
This 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.
**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.
### Trademarks
This 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.
### Company Information
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
By 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.

View File

@ -5,7 +5,7 @@ import * as smartfile from '@push.rocks/smartfile';
let testSmartJimp: smartjimp.SmartJimp;
tap.test('first test', async () => {
testSmartJimp = new smartjimp.SmartJimp();
testSmartJimp = new smartjimp.SmartJimp({ mode: 'sharp'});
expect(testSmartJimp).toBeInstanceOf(smartjimp.SmartJimp);
});
@ -15,4 +15,13 @@ tap.test('should be able to create a master', async () => {
(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.contents, {
format: 'png',
});
(await smartfile.SmartFile.fromBuffer('.nogit/result2.png', convertedAsset)).write();
})
tap.start();

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartjimp',
version: '1.0.8',
version: '1.0.18',
description: 'a tool fr working with images in TypeScript'
}

View File

@ -11,8 +11,3 @@ import * as smartpath from '@push.rocks/smartpath';
import * as smartrequest from '@push.rocks/smartrequest';
export { levelcache, smartpath, smarthash, smartfile, smartrequest };
// third party scope
import sharp from 'sharp';
export { sharp };

7
ts/plugins.typed.ts Normal file
View File

@ -0,0 +1,7 @@
import type * as sharpType from 'sharp';
export { type sharpType };
import type * as jimpType from 'jimp';
export { type jimpType };

View File

@ -1,8 +1,15 @@
import * as plugins from './smartjimp.plugins.js';
import * as plugins from './plugins.js';
import * as pluginsTyped from './plugins.typed.js';
export interface IDimensions {
export interface IAssetVariation {
format?: 'avif' | 'webp' | 'png';
width?: number;
height?: number;
invert?: boolean;
}
export interface ISmartJimpOptions {
mode: 'sharp' | 'jimp';
}
export class SmartJimp {
@ -12,53 +19,115 @@ export class SmartJimp {
maxDiskStorageInMB: 5000,
});
public options: ISmartJimpOptions;
constructor(optionsArg: ISmartJimpOptions) {
this.options = optionsArg;
}
/**
* get a key that is unique for a wanted asset variation
*/
private getCacheKey(
sourceTypeArg: 'streamfile' | 'smartfile',
sourceIdArg: string,
wantedDimensionsArg?: IDimensions
assetVariationArg?: IAssetVariation
) {
return `${sourceTypeArg}_${sourceIdArg}_${
wantedDimensionsArg
? `${wantedDimensionsArg.width || 'auto' }x${wantedDimensionsArg.height || 'auto'}`
assetVariationArg
? `${assetVariationArg.width || 'auto'}x${assetVariationArg.height || 'auto'}`
: 'original'
}`;
}
private async computeAssetVariation(assetBuffer: Buffer, wantedDimensions?: IDimensions) {
if (!wantedDimensions) {
return assetBuffer;
sharpMod: typeof pluginsTyped.sharpType.default;
public async getSharpMod(): Promise<
typeof pluginsTyped.sharpType.default
> {
if (!this.sharpMod) {
this.sharpMod = (await import('sharp')).default;
}
return this.sharpMod;
}
jimpMod: typeof pluginsTyped.jimpType;
public async getJimpMod(): Promise<typeof pluginsTyped.jimpType.default> {
if (!this.jimpMod) {
this.jimpMod = await import('jimp');
}
return this.jimpMod.default;
}
public async computeAssetVariation(assetBufferArg: Buffer, assetVariationArg: IAssetVariation) {
if (this.options.mode === 'sharp') {
const sharp = await this.getSharpMod();
if (!assetVariationArg) {
return assetBufferArg;
}
let sharpImage = sharp(assetBufferArg);
sharpImage = sharpImage.resize(assetVariationArg.width, assetVariationArg.height);
const resultResize = sharpImage.resize(assetVariationArg.width, assetVariationArg.height);
if (assetVariationArg.invert) {
// TODO: implement invert
}
switch (assetVariationArg.format) {
case 'avif':
sharpImage = resultResize.avif();
case 'webp':
sharpImage = resultResize.webp();
case 'png':
sharpImage = resultResize.png();
}
return sharpImage.toBuffer();
} else if (this.options.mode === 'jimp') {
const jimp = await this.getJimpMod();
let jimpImage = await jimp.read(assetBufferArg);
if (assetVariationArg.width || assetVariationArg.height) {
jimpImage = jimpImage.resize(assetVariationArg.width, assetVariationArg.height);
}
if (assetVariationArg.invert) {
jimpImage = jimpImage.invert();
}
switch (assetVariationArg.format) {
case 'png':
return await jimpImage.getBufferAsync(jimp.MIME_PNG);
default:
return await jimpImage.getBufferAsync(jimp.MIME_JPEG);
}
}
let sharpImage = plugins.sharp(assetBuffer);
sharpImage = sharpImage.resize(wantedDimensions.width, wantedDimensions.height);
const result = await sharpImage.resize(wantedDimensions.width, wantedDimensions.height).avif().toBuffer();
return result;
}
public async getFromSmartfile(
smartfileArg: plugins.smartfile.SmartFile,
wantedDimensionsArg?: IDimensions
wantedDimensionsArg?: IAssetVariation
) {
const cacheKey = this.getCacheKey('smartfile', await smartfileArg.getHash(), wantedDimensionsArg);
const cacheKey = this.getCacheKey(
'smartfile',
await smartfileArg.getHash(),
wantedDimensionsArg
);
const existingCacheEntry = await this.levelCache.retrieveCacheEntryByKey(cacheKey);
if (existingCacheEntry) {
return existingCacheEntry.contents;
} else {
const computedAssetBuffer = await this.computeAssetVariation(smartfileArg.contentBuffer, wantedDimensionsArg);
this.levelCache.storeCacheEntryByKey(cacheKey, new plugins.levelcache.CacheEntry({
contents: computedAssetBuffer,
ttl: 600000
}));
const computedAssetBuffer = await this.computeAssetVariation(
smartfileArg.contentBuffer,
wantedDimensionsArg
);
this.levelCache.storeCacheEntryByKey(
cacheKey,
new plugins.levelcache.CacheEntry({
contents: computedAssetBuffer,
ttl: 600000,
})
);
return computedAssetBuffer;
}
}
public async createAvifImageFromBuffer(bufferArg: Buffer) {
const sharpImage = plugins.sharp(bufferArg);
const sharp = await this.getSharpMod();
const sharpImage = sharp(bufferArg);
const result = await sharpImage.avif().toBuffer();
return result;
}
}

View File

@ -1,4 +1,4 @@
import * as plugins from './smartjimp.plugins.js';
import * as plugins from './plugins.js';
export const packageDir = plugins.path.join(plugins.smartpath.get.dirnameFromImportMetaUrl(import.meta.url), '../');
export const nogitDir = plugins.path.join(packageDir, '.nogit/')