Skip to content

Commit 15a5e59

Browse files
committed
Merge pull request DefinitelyTyped#4506 from horiuchi/imagemagick-native
add `imagemagick-native`s type definition file
2 parents b0b2e98 + a5db8a6 commit 15a5e59

File tree

2 files changed

+226
-0
lines changed

2 files changed

+226
-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: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
/// <reference path="../node/node.d.ts" />
7+
8+
declare module "imagemagick-native" {
9+
10+
import stream = require('stream');
11+
export module streams {
12+
export function convert(options: IConvertOptions): stream.Transform;
13+
}
14+
15+
function convert(options: IConvertOptions): Buffer;
16+
function convert(options: IConvertOptions, callback: (err: any, result: Buffer) => void): void;
17+
function identify(options: IIdentifyOptions): IIdentifyResult;
18+
function identify(options: IIdentifyOptions, callback: (err: any, result: IIdentifyResult) => void): void;
19+
function quantizeColors(options: IQuantizeColorsOptions): IQuantizeColorsItem[];
20+
function composite(options: ICompositeOptions): Buffer;
21+
function composite(options: ICompositeOptions, callback: (err: any, result: Buffer) => void): void;
22+
function getConstPixels(options: IConstPixelsOptions): IConstPixelsItem[];
23+
function quantumDepth(): number;
24+
function version(): string;
25+
26+
export interface IConvertOptions {
27+
srcData: Buffer;
28+
srcFormat?: string;
29+
quality?: number;
30+
trim?: boolean;
31+
trimFuzz?: number;
32+
width?: number;
33+
height?: number;
34+
density?: number;
35+
resizeStyle?: string;
36+
gravity?: string;
37+
format?: string;
38+
filter?: string;
39+
blur?: number;
40+
strip?: boolean;
41+
rotate?: number;
42+
flip?: boolean;
43+
debug?: boolean;
44+
ignoreWarnings?: boolean;
45+
}
46+
export interface IIdentifyOptions {
47+
srcData: Buffer;
48+
debug?: boolean;
49+
ignoreWarnings?: boolean;
50+
}
51+
export interface IIdentifyResult {
52+
format: string;
53+
width: number;
54+
height: number;
55+
depth: number;
56+
density : {
57+
width : number;
58+
height : number;
59+
};
60+
exif: {
61+
orientation: number; // 0 if none exists or e.g. 3 (portrait iPad pictures)
62+
};
63+
}
64+
export interface IQuantizeColorsOptions {
65+
srcData: Buffer;
66+
colors: number;
67+
debug?: boolean;
68+
ignoreWarnings?: boolean;
69+
}
70+
export interface IQuantizeColorsItem {
71+
r: number;
72+
g: number;
73+
b: number;
74+
hex: string;
75+
}
76+
export interface ICompositeOptions {
77+
srcData: Buffer;
78+
compositeData: Buffer;
79+
gravity?: string;
80+
debug?: boolean;
81+
ignoreWarnings?: boolean;
82+
}
83+
export interface IConstPixelsOptions {
84+
srcData: Buffer;
85+
x: number;
86+
y: number;
87+
columns: number;
88+
rows: number;
89+
}
90+
export interface IConstPixelsItem {
91+
red: number;
92+
green: number;
93+
blue: number;
94+
opacity: number;
95+
}
96+
97+
}
98+

0 commit comments

Comments
 (0)