Skip to content

Commit 480df87

Browse files
committed
add imagemagick-natives type definition file
1 parent b0b2e98 commit 480df87

File tree

2 files changed

+224
-0
lines changed

2 files changed

+224
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/// <reference path="imagemagick-native.d.ts" />
2+
/// <reference path="../node/node.d.ts" />
3+
4+
import imagemagick = require('imagemagick-native');
5+
import fs = require('fs');
6+
7+
8+
// Examples
9+
10+
// * Convert formats
11+
// Convert from one format to another with quality control:
12+
fs.writeFileSync('after.png', imagemagick.convert({
13+
srcData: fs.readFileSync('before.jpg'),
14+
format: 'PNG',
15+
quality: 100 // (best) to 1 (worst)
16+
}));
17+
18+
// * Blur
19+
// Blur image:
20+
fs.writeFileSync('after.jpg', imagemagick.convert({
21+
srcData: fs.readFileSync('before.jpg'),
22+
blur: 5
23+
}));
24+
25+
// * Resize
26+
// Resized images by specifying width and height. There are three resizing styles:
27+
// - aspectfill: Default. The resulting image will be exactly the specified size, and may be cropped.
28+
// - aspectfit: Scales the image so that it will not have to be cropped.
29+
// - fill: Squishes or stretches the image so that it fills exactly the specified size.
30+
fs.writeFileSync('after_resize.jpg', imagemagick.convert({
31+
srcData: fs.readFileSync('before_resize.jpg'),
32+
width: 100,
33+
height: 100,
34+
resizeStyle: 'aspectfill', // is the default, or 'aspectfit' or 'fill'
35+
gravity: 'Center' // optional: position crop area when using 'aspectfill'
36+
}));
37+
38+
// * Rotate, flip, and mirror
39+
// Rotate and flip images, and combine the two to mirror:
40+
fs.writeFileSync('after_rotateflip.jpg', imagemagick.convert({
41+
srcData: fs.readFileSync('before_rotateflip.jpg'),
42+
rotate: 180,
43+
flip: true
44+
}));
45+
46+
47+
// API Reference
48+
49+
// * convert(options, [callback])
50+
// Convert a buffer provided as options.srcData and return a Buffer.
51+
var options = {
52+
srcData: fs.readFileSync('source.jpg'),
53+
srcFormat: 'jpeg',
54+
quality: 90,
55+
trim: true,
56+
trimFuzz: 0.25,
57+
width: 100,
58+
height: 100,
59+
density: 96,
60+
resizeStyle: 'aspectfill',
61+
gravity: 'NorthWest',
62+
format: 'png',
63+
filter: 'Lnaczos',
64+
blur: 3,
65+
strip: true,
66+
rotate: 30,
67+
flip: true,
68+
debug: true,
69+
ignoreWarnings: false
70+
};
71+
72+
imagemagick.convert(options, (err: any, buffer: Buffer) => {
73+
// check err, use buffer
74+
});
75+
fs.createReadStream('input.png')
76+
.pipe(imagemagick.streams.convert(options))
77+
.pipe(fs.createWriteStream('output.png'));
78+
79+
80+
// * identify(options, [callback])
81+
// Identify a buffer provided as srcData and return an object.
82+
imagemagick.identify({
83+
srcData: fs.readFileSync('target.jpg'),
84+
debug: true,
85+
ignoreWarnings: false
86+
}, (err: any, result: imagemagick.IIdentifyResult) => {
87+
// check err, use result
88+
});
89+
90+
// * quantizeColors(options)
91+
// Quantize the image to a specified amount of colors from a buffer provided as srcData and return an array.
92+
var colors = imagemagick.quantizeColors({
93+
srcData: fs.readFileSync('target.jpg'),
94+
colors: 3,
95+
debug: true,
96+
ignoreWarnings: false
97+
});
98+
99+
// * composite(options, [callback])
100+
// Composite a buffer provided as options.compositeData on a buffer provided as options.srcData with gravity specified by options.gravity and return a Buffer
101+
imagemagick.composite({
102+
srcData: fs.readFileSync('target.jpg'),
103+
compositeData: fs.readFileSync('composite.jpg'),
104+
gravity: 'NorthWestGravity',
105+
debug: true,
106+
ignoreWarnings: false
107+
}, (err: any, buffer: Buffer) => {
108+
// check err, use buffer
109+
});
110+
111+
// * getConstPixels(options)
112+
// Get pixels of provided rectangular region.
113+
var pixels = imagemagick.getConstPixels({
114+
srcData: fs.readFileSync('target.jpg'),
115+
x: 0,
116+
y: 0,
117+
columns: 1,
118+
rows: 1
119+
});
120+
121+
// * quantumDepth
122+
// Return ImageMagick's QuantumDepth, which is defined in compile time.
123+
var depth: number = imagemagick.quantumDepth();
124+
125+
// * version
126+
// Return ImageMagick's version as string.
127+
var version: string = imagemagick.version();
128+
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Type definitions for imagemagick-native 1.7.0
2+
// Project: https://www.npmjs.org/package/imagemagick-native
3+
// Definitions by: Hiroki Horiuchi <https://github.com/horiuchi>
4+
// Definitions: https://github.com/borisyankov/DefinitelyTyped
5+
6+
declare module "imagemagick-native" {
7+
8+
import stream = require('stream');
9+
export module streams {
10+
export function convert(options: IConvertOptions): stream.Transform;
11+
}
12+
13+
function convert(options: IConvertOptions): Buffer;
14+
function convert(options: IConvertOptions, callback: (err: any, result: Buffer) => void): void;
15+
function identify(options: IIdentifyOptions): IIdentifyResult;
16+
function identify(options: IIdentifyOptions, callback: (err: any, result: IIdentifyResult) => void): void;
17+
function quantizeColors(options: IQuantizeColorsOptions): IQuantizeColorsItem[];
18+
function composite(options: ICompositeOptions): Buffer;
19+
function composite(options: ICompositeOptions, callback: (err: any, result: Buffer) => void): void;
20+
function getConstPixels(options: IConstPixelsOptions): IConstPixelsItem[];
21+
function quantumDepth(): number;
22+
function version(): string;
23+
24+
export interface IConvertOptions {
25+
srcData: Buffer;
26+
srcFormat?: string;
27+
quality?: number;
28+
trim?: boolean;
29+
trimFuzz?: number;
30+
width?: number;
31+
height?: number;
32+
density?: number;
33+
resizeStyle?: string;
34+
gravity?: string;
35+
format?: string;
36+
filter?: string;
37+
blur?: number;
38+
strip?: boolean;
39+
rotate?: number;
40+
flip?: boolean;
41+
debug?: boolean;
42+
ignoreWarnings?: boolean;
43+
}
44+
export interface IIdentifyOptions {
45+
srcData: Buffer;
46+
debug?: boolean;
47+
ignoreWarnings?: boolean;
48+
}
49+
export interface IIdentifyResult {
50+
format: string;
51+
width: number;
52+
height: number;
53+
depth: number;
54+
density : {
55+
width : number;
56+
height : number;
57+
};
58+
exif: {
59+
orientation: number; // 0 if none exists or e.g. 3 (portrait iPad pictures)
60+
};
61+
}
62+
export interface IQuantizeColorsOptions {
63+
srcData: Buffer;
64+
colors: number;
65+
debug?: boolean;
66+
ignoreWarnings?: boolean;
67+
}
68+
export interface IQuantizeColorsItem {
69+
r: number;
70+
g: number;
71+
b: number;
72+
hex: string;
73+
}
74+
export interface ICompositeOptions {
75+
srcData: Buffer;
76+
compositeData: Buffer;
77+
gravity?: string;
78+
debug?: boolean;
79+
ignoreWarnings?: boolean;
80+
}
81+
export interface IConstPixelsOptions {
82+
srcData: Buffer;
83+
x: number;
84+
y: number;
85+
columns: number;
86+
rows: number;
87+
}
88+
export interface IConstPixelsItem {
89+
red: number;
90+
green: number;
91+
blue: number;
92+
opacity: number;
93+
}
94+
95+
}
96+

0 commit comments

Comments
 (0)