|
| 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 | + |
0 commit comments