diff --git a/imagemagick-native/imagemagick-native-tests.ts b/imagemagick-native/imagemagick-native-tests.ts new file mode 100644 index 0000000000..a1e42c6a9b --- /dev/null +++ b/imagemagick-native/imagemagick-native-tests.ts @@ -0,0 +1,128 @@ +/// +/// + +import imagemagick = require('imagemagick-native'); +import fs = require('fs'); + + +// Examples + +// * Convert formats +// Convert from one format to another with quality control: +fs.writeFileSync('after.png', imagemagick.convert({ + srcData: fs.readFileSync('before.jpg'), + format: 'PNG', + quality: 100 // (best) to 1 (worst) +})); + +// * Blur +// Blur image: +fs.writeFileSync('after.jpg', imagemagick.convert({ + srcData: fs.readFileSync('before.jpg'), + blur: 5 +})); + +// * Resize +// Resized images by specifying width and height. There are three resizing styles: +// - aspectfill: Default. The resulting image will be exactly the specified size, and may be cropped. +// - aspectfit: Scales the image so that it will not have to be cropped. +// - fill: Squishes or stretches the image so that it fills exactly the specified size. +fs.writeFileSync('after_resize.jpg', imagemagick.convert({ + srcData: fs.readFileSync('before_resize.jpg'), + width: 100, + height: 100, + resizeStyle: 'aspectfill', // is the default, or 'aspectfit' or 'fill' + gravity: 'Center' // optional: position crop area when using 'aspectfill' +})); + +// * Rotate, flip, and mirror +// Rotate and flip images, and combine the two to mirror: +fs.writeFileSync('after_rotateflip.jpg', imagemagick.convert({ + srcData: fs.readFileSync('before_rotateflip.jpg'), + rotate: 180, + flip: true +})); + + +// API Reference + +// * convert(options, [callback]) +// Convert a buffer provided as options.srcData and return a Buffer. +var options = { + srcData: fs.readFileSync('source.jpg'), + srcFormat: 'jpeg', + quality: 90, + trim: true, + trimFuzz: 0.25, + width: 100, + height: 100, + density: 96, + resizeStyle: 'aspectfill', + gravity: 'NorthWest', + format: 'png', + filter: 'Lnaczos', + blur: 3, + strip: true, + rotate: 30, + flip: true, + debug: true, + ignoreWarnings: false +}; + +imagemagick.convert(options, (err: any, buffer: Buffer) => { + // check err, use buffer +}); +fs.createReadStream('input.png') + .pipe(imagemagick.streams.convert(options)) + .pipe(fs.createWriteStream('output.png')); + + +// * identify(options, [callback]) +// Identify a buffer provided as srcData and return an object. +imagemagick.identify({ + srcData: fs.readFileSync('target.jpg'), + debug: true, + ignoreWarnings: false +}, (err: any, result: imagemagick.IIdentifyResult) => { + // check err, use result +}); + +// * quantizeColors(options) +// Quantize the image to a specified amount of colors from a buffer provided as srcData and return an array. +var colors = imagemagick.quantizeColors({ + srcData: fs.readFileSync('target.jpg'), + colors: 3, + debug: true, + ignoreWarnings: false +}); + +// * composite(options, [callback]) +// Composite a buffer provided as options.compositeData on a buffer provided as options.srcData with gravity specified by options.gravity and return a Buffer +imagemagick.composite({ + srcData: fs.readFileSync('target.jpg'), + compositeData: fs.readFileSync('composite.jpg'), + gravity: 'NorthWestGravity', + debug: true, + ignoreWarnings: false +}, (err: any, buffer: Buffer) => { + // check err, use buffer +}); + +// * getConstPixels(options) +// Get pixels of provided rectangular region. +var pixels = imagemagick.getConstPixels({ + srcData: fs.readFileSync('target.jpg'), + x: 0, + y: 0, + columns: 1, + rows: 1 +}); + +// * quantumDepth +// Return ImageMagick's QuantumDepth, which is defined in compile time. +var depth: number = imagemagick.quantumDepth(); + +// * version +// Return ImageMagick's version as string. +var version: string = imagemagick.version(); + diff --git a/imagemagick-native/imagemagick-native.d.ts b/imagemagick-native/imagemagick-native.d.ts new file mode 100644 index 0000000000..c69cd6d8d1 --- /dev/null +++ b/imagemagick-native/imagemagick-native.d.ts @@ -0,0 +1,96 @@ +// Type definitions for imagemagick-native 1.7.0 +// Project: https://www.npmjs.org/package/imagemagick-native +// Definitions by: Hiroki Horiuchi +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module "imagemagick-native" { + + import stream = require('stream'); + export module streams { + export function convert(options: IConvertOptions): stream.Transform; + } + + function convert(options: IConvertOptions): Buffer; + function convert(options: IConvertOptions, callback: (err: any, result: Buffer) => void): void; + function identify(options: IIdentifyOptions): IIdentifyResult; + function identify(options: IIdentifyOptions, callback: (err: any, result: IIdentifyResult) => void): void; + function quantizeColors(options: IQuantizeColorsOptions): IQuantizeColorsItem[]; + function composite(options: ICompositeOptions): Buffer; + function composite(options: ICompositeOptions, callback: (err: any, result: Buffer) => void): void; + function getConstPixels(options: IConstPixelsOptions): IConstPixelsItem[]; + function quantumDepth(): number; + function version(): string; + + export interface IConvertOptions { + srcData: Buffer; + srcFormat?: string; + quality?: number; + trim?: boolean; + trimFuzz?: number; + width?: number; + height?: number; + density?: number; + resizeStyle?: string; + gravity?: string; + format?: string; + filter?: string; + blur?: number; + strip?: boolean; + rotate?: number; + flip?: boolean; + debug?: boolean; + ignoreWarnings?: boolean; + } + export interface IIdentifyOptions { + srcData: Buffer; + debug?: boolean; + ignoreWarnings?: boolean; + } + export interface IIdentifyResult { + format: string; + width: number; + height: number; + depth: number; + density : { + width : number; + height : number; + }; + exif: { + orientation: number; // 0 if none exists or e.g. 3 (portrait iPad pictures) + }; + } + export interface IQuantizeColorsOptions { + srcData: Buffer; + colors: number; + debug?: boolean; + ignoreWarnings?: boolean; + } + export interface IQuantizeColorsItem { + r: number; + g: number; + b: number; + hex: string; + } + export interface ICompositeOptions { + srcData: Buffer; + compositeData: Buffer; + gravity?: string; + debug?: boolean; + ignoreWarnings?: boolean; + } + export interface IConstPixelsOptions { + srcData: Buffer; + x: number; + y: number; + columns: number; + rows: number; + } + export interface IConstPixelsItem { + red: number; + green: number; + blue: number; + opacity: number; + } + +} +