@openfn/language-image-utils 1.0.0

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.
@@ -0,0 +1,110 @@
1
+ /**
2
+ * State object
3
+ * @typedef {Object} ImageState
4
+ * @property data - the result of the image operation
5
+ * @property references - an array of all previous data objects used in the Job
6
+ * @private
7
+ **/
8
+ /**
9
+ * Resize an image to the given dimensions.
10
+ * Writes `{ buffer, width, height }` to `state.data`.
11
+ * @example
12
+ * resize(state.data.photoBase64, { width: 1200, height: 1600 })
13
+ * @function
14
+ * @param {string|Buffer|Function} base64ImgOrBuffer - Base64 string, data URL, Buffer, or resolver fn
15
+ * @param {object} [options={}]
16
+ * @param {number} [options.width] - Output width in pixels
17
+ * @param {number} [options.height] - Output height in pixels
18
+ * @param {'buffer'|'base64'} [options.parseAs='buffer'] - Return format: `'buffer'` (default) or `'base64'`
19
+ * @state {ImageState}
20
+ * @returns {Operation}
21
+ */
22
+ export function resize(base64ImgOrBuffer: string | Buffer | Function, options?: {
23
+ width?: number;
24
+ height?: number;
25
+ parseAs?: 'buffer' | 'base64';
26
+ }): Operation;
27
+ /**
28
+ * Compress an image by reducing image quality until it reaches the criteria.
29
+ * Writes `{ buffer, size, quality }` to `state.data`.
30
+ * @example
31
+ * compress(state.data.buffer, { maxBytes: 700 * 1024, minQuality: 20 })
32
+ * @function
33
+ * @param {string|Buffer|Function} base64ImgOrBuffer - Base64 string, data URL, Buffer, or resolver fn
34
+ * @param {object} [options={}]
35
+ * @param {number} [options.maxBytes=716800] - Maximum output file size in bytes
36
+ * @param {number} [options.minQuality=20] - JPEG quality floor (1–100); compression stops here even if maxBytes is not met
37
+ * @param {'buffer'|'base64'} [options.parseAs='buffer'] - Return format: `'buffer'` (default) or `'base64'`
38
+ * @state {ImageState}
39
+ * @returns {Operation}
40
+ */
41
+ export function compress(base64ImgOrBuffer: string | Buffer | Function, options?: {
42
+ maxBytes?: number;
43
+ minQuality?: number;
44
+ parseAs?: 'buffer' | 'base64';
45
+ }): Operation;
46
+ /**
47
+ * Strip all EXIF metadata from an image. Output is always a JPEG buffer.
48
+ * Writes `{ buffer }` to `state.data`.
49
+ * @example
50
+ * stripMetadata(state.data.photoBase64)
51
+ * @function
52
+ * @param {string|Buffer|Function} base64ImgOrBuffer - Base64 string, data URL, Buffer, or resolver fn
53
+ * @param {object} [options={}]
54
+ * @param {'buffer'|'base64'} [options.parseAs='buffer'] - Return format: `'buffer'` (default) or `'base64'`
55
+ * @state {ImageState}
56
+ * @returns {Operation}
57
+ */
58
+ export function stripMetadata(base64ImgOrBuffer: string | Buffer | Function, options?: {
59
+ parseAs?: 'buffer' | 'base64';
60
+ }): Operation;
61
+ /**
62
+ * Embed EXIF metadata into a JPEG image.
63
+ * Writes `{ buffer }` to `state.data`.
64
+ * @example
65
+ * embedMetadata(state.data.buffer, { UserComment: 'patient-id=42' })
66
+ * embedMetadata(state.data.buffer, { UserComment: 'patient-id=42', Make: 'OpenFn' }, { parseAs: 'base64' })
67
+ * @function
68
+ * @param {string|Buffer|Function} base64ImgOrBuffer - Base64 string, data URL, Buffer, or resolver fn
69
+ * @param {object} exifObj - EXIF key-value pairs; keys must be valid EXIF tag names (e.g. UserComment, Make, Model)
70
+ * @param {object} [options={}]
71
+ * @param {'buffer'|'base64'} [options.parseAs='buffer'] - Return format: `'buffer'` (default) or `'base64'`
72
+ * @state {ImageState}
73
+ * @returns {Operation}
74
+ */
75
+ export function embedMetadata(base64ImgOrBuffer: string | Buffer | Function, exifObj: object, options?: {
76
+ parseAs?: 'buffer' | 'base64';
77
+ }): Operation;
78
+ /**
79
+ * Read image metadata without modifying the image.
80
+ * Writes `{ width, height, orientation, size, exif }` to `state.data`.
81
+ * `exif` is a flat object of human-readable EXIF tag names (e.g. `{ Make, Model, GPSLatitude, UserComment }`).
82
+ * `UserComment` has the `ASCII\0\0\0` encoding prefix stripped. Images with no EXIF return `exif: {}`.
83
+ * @example
84
+ * metadata(state.data.photoBase64)
85
+ * fn(state => {
86
+ * if (state.data.size > 700 * 1024) { ... }
87
+ * return state;
88
+ * })
89
+ * @function
90
+ * @param {string|Buffer|Function} base64ImgOrBuffer - Base64 string, data URL, Buffer, or resolver fn
91
+ * @state {ImageState}
92
+ * @returns {Operation}
93
+ */
94
+ export function metadata(base64ImgOrBuffer: string | Buffer | Function): Operation;
95
+ export { getExifData };
96
+ /**
97
+ * State object
98
+ */
99
+ export type ImageState = {
100
+ /**
101
+ * - the result of the image operation
102
+ */
103
+ data: any;
104
+ /**
105
+ * - an array of all previous data objects used in the Job
106
+ */
107
+ references: any;
108
+ };
109
+ import { getExifData } from "./Utils.js";
110
+ export { as, combine, cursor, dataPath, dataValue, dateFns, each, field, fields, fn, fnIf, group, lastReferenceValue, map, merge, scrubEmojis, sourceValue, util, http } from "@openfn/language-common";
@@ -0,0 +1,26 @@
1
+ export function decodeBase64Image(base64ImgStr: any): Buffer;
2
+ export function injectExif(jpegBuffer: any, kvPairs: any): Buffer;
3
+ export function resizeImage(inputBuffer: any, options?: {}): Promise<{
4
+ buffer: Buffer;
5
+ width: any;
6
+ height: any;
7
+ }>;
8
+ export function embedImageMetadata(inputBuffer: any, kvPairs: any): {
9
+ buffer: Buffer;
10
+ };
11
+ export function compressImage(inputBuffer: any, options?: {}): Promise<{
12
+ buffer: Buffer;
13
+ size: number;
14
+ quality: number;
15
+ }>;
16
+ export function stripImage(inputBuffer: any): Promise<{
17
+ buffer: Buffer;
18
+ }>;
19
+ export function getImageMetadata(inputBuffer: any): Promise<{
20
+ width: number;
21
+ height: number;
22
+ orientation: string;
23
+ size: any;
24
+ exif: {};
25
+ }>;
26
+ export function getExifData(inputBuffer: any, options?: {}): {};
@@ -0,0 +1,3 @@
1
+ export default Adaptor;
2
+ export * from "./Adaptor";
3
+ import * as Adaptor from "./Adaptor";