@travetto/image 3.3.4 → 3.4.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +3 -3
- package/src/convert.ts +30 -3
- package/src/resource.ts +2 -2
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ yarn add @travetto/image
|
|
|
15
15
|
|
|
16
16
|
This module provides functionality for image resizing, and png optimization. This is primarily meant to be used in conjunction with other modules, like the [Asset](https://github.com/travetto/travetto/tree/main/module/asset#readme "Modular library for storing and retrieving binary assets") module or the [Email Compilation Support](https://github.com/travetto/travetto/tree/main/module/email-compiler#readme "Email compiling module") module. It can also be invoked directly as needed (as it can be very handy for batch processing images on the command line).
|
|
17
17
|
|
|
18
|
-
The utility's primary structure revolves around the [CommandOperation](https://github.com/travetto/travetto/tree/main/module/command/src/command.ts#L11) from the [Command](https://github.com/travetto/travetto/tree/main/module/command#readme "Support for executing complex commands at runtime.") module. The [CommandOperation](https://github.com/travetto/travetto/tree/main/module/command/src/command.ts#L11) allows for declaration of a local executable, and a fall-back docker container (mainly meant for development). The [ImageConverter](https://github.com/travetto/travetto/tree/main/module/image/src/convert.ts#
|
|
18
|
+
The utility's primary structure revolves around the [CommandOperation](https://github.com/travetto/travetto/tree/main/module/command/src/command.ts#L11) from the [Command](https://github.com/travetto/travetto/tree/main/module/command#readme "Support for executing complex commands at runtime.") module. The [CommandOperation](https://github.com/travetto/travetto/tree/main/module/command/src/command.ts#L11) allows for declaration of a local executable, and a fall-back docker container (mainly meant for development). The [ImageConverter](https://github.com/travetto/travetto/tree/main/module/image/src/convert.ts#L35) utilizes [ImageMagick](https://imagemagick.org/index.php), [pngquant](https://pngquant.org/), and [Jpegoptim](https://github.com/tjko/jpegoptim) as the backing for image resizing and png compression, respectively.
|
|
19
19
|
|
|
20
20
|
**Code: Simple Image Resize**
|
|
21
21
|
```typescript
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/image",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0-rc.1",
|
|
4
4
|
"description": "Image support, resizing, and optimization",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"images",
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"directory": "module/image"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@travetto/base": "^3.
|
|
27
|
-
"@travetto/command": "^3.
|
|
26
|
+
"@travetto/base": "^3.4.0-rc.1",
|
|
27
|
+
"@travetto/command": "^3.4.0-rc.1"
|
|
28
28
|
},
|
|
29
29
|
"travetto": {
|
|
30
30
|
"displayName": "Image"
|
package/src/convert.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Readable } from 'stream';
|
|
2
|
-
|
|
2
|
+
import { createReadStream } from 'fs';
|
|
3
3
|
|
|
4
4
|
import { CommandOperation } from '@travetto/command';
|
|
5
5
|
import { StreamUtil } from '@travetto/base';
|
|
@@ -20,6 +20,10 @@ export interface ImageOptions {
|
|
|
20
20
|
* Should the image be optimized?
|
|
21
21
|
*/
|
|
22
22
|
optimize?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Strict resolution
|
|
25
|
+
*/
|
|
26
|
+
strictResolution?: boolean;
|
|
23
27
|
}
|
|
24
28
|
|
|
25
29
|
type ImageType = Readable | Buffer;
|
|
@@ -58,9 +62,10 @@ export class ImageConverter {
|
|
|
58
62
|
* Resize image using imagemagick
|
|
59
63
|
*/
|
|
60
64
|
static async resize<T extends ImageType>(image: T, options: ImageOptions): Promise<T> {
|
|
65
|
+
const dims = [options.w, options.h].map(d => (d && options.strictResolution !== false) ? `${d}!` : d).join('x');
|
|
66
|
+
|
|
61
67
|
const state = await this.CONVERTER.exec(
|
|
62
|
-
'gm', 'convert', '-resize',
|
|
63
|
-
'-auto-orient',
|
|
68
|
+
'gm', 'convert', '-resize', dims, '-auto-orient',
|
|
64
69
|
...(options.optimize ? ['-strip', '-quality', '86'] : []),
|
|
65
70
|
'-', '-');
|
|
66
71
|
|
|
@@ -85,4 +90,26 @@ export class ImageConverter {
|
|
|
85
90
|
}
|
|
86
91
|
return await StreamUtil.execPipe(stream, image);
|
|
87
92
|
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Get Image Dimensions
|
|
96
|
+
* @param image
|
|
97
|
+
*/
|
|
98
|
+
static async getDimensions(image: Readable | Buffer | string): Promise<{ width: number, height: number }> {
|
|
99
|
+
const state = await this.CONVERTER.exec(
|
|
100
|
+
'gm', 'identify', '-format', '%wX%h', '-',
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
if (typeof image === 'string') {
|
|
104
|
+
image = createReadStream(image);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
await StreamUtil.execPipe(state, await StreamUtil.toStream(image));
|
|
108
|
+
|
|
109
|
+
const buf = await StreamUtil.toBuffer(state.process.stdout!);
|
|
110
|
+
const text = buf.toString('utf8');
|
|
111
|
+
const [w, h] = text.split('X').map(x => parseFloat(x));
|
|
112
|
+
|
|
113
|
+
return { width: w, height: h };
|
|
114
|
+
}
|
|
88
115
|
}
|
package/src/resource.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import fs from 'fs/promises';
|
|
2
2
|
import { Readable } from 'stream';
|
|
3
3
|
|
|
4
|
-
import { path } from '@travetto/manifest';
|
|
4
|
+
import { ManifestFileUtil, RootIndex, path } from '@travetto/manifest';
|
|
5
5
|
import { Env, FileResourceProvider, StreamUtil } from '@travetto/base';
|
|
6
6
|
|
|
7
7
|
import { ImageConverter } from './convert';
|
|
@@ -16,7 +16,7 @@ export class ImageOptimizingResourceProvider extends FileResourceProvider {
|
|
|
16
16
|
constructor(paths?: string[], cacheRoot?: string) {
|
|
17
17
|
super({ paths, includeCommon: true });
|
|
18
18
|
|
|
19
|
-
this.#cacheRoot = cacheRoot ?? path.resolve(Env.get('TRV_IMAGE_CACHE',
|
|
19
|
+
this.#cacheRoot = cacheRoot ?? path.resolve(Env.get('TRV_IMAGE_CACHE', ManifestFileUtil.toolPath(RootIndex, 'image_cache')));
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
async #openFile(pth: string): Promise<fs.FileHandle> {
|