modules/ImageMagick/ImageMagick.js

/**
 * Sample of usage {@link ffi} module with ImageMagick library.
 *
 * Since <a href="http://www.imagemagick.org/">ImageMagick®</a> is a HUGE library, you can export function from there using this sample.
 * We already done most types definition inside ImageMagickAPI.js
 *
 *
 *      var im = require("ImageMagick");
 *      im.convertImage('d:\\1.jpg', 'd:\\2.bmp');
 *      im.convertImage('d:\\1.jpg', 'd:\\2.bmp', {width: 50, height: 50});
 *
 * @module ImageMagick
 */
var ImageMagickAPI = require("./ImageMagickAPI");
/**
 * Convert image file `source` to file `dest`. If assigned newSize then resize image.
 *
 * @param {string} source source file name
 * @param {string} dest destination file name
 * @param {Object} [newSize] new size of image
 * @param {number} newSize.width
 * @param {number} newSize.height
 */
module.exports.convertImage= function(source, dest, newSize) {
    var wand = ImageMagickAPI.api.NewMagickWand(),
        fn = new ImageMagickAPI.PChar(),
        ET = new ImageMagickAPI.PExceptionType(1),
        checkError = function(val, method){
            if (val  !== ImageMagickAPI.MagickBooleanType.MagickTrue) {
                var errMsg = ImageMagickAPI.api.MagickGetException(wand,ET);
                throw new Error(method+' '+errMsg.getStr()+' '+ET.get());
            }
        };

    fn.setStr(source);
    checkError(ImageMagickAPI.api.MagickReadImage(wand, fn), 'MagickReadImage');
    if (newSize) {
        checkError(ImageMagickAPI.api.MagickResizeImage(wand,newSize.width,newSize.height,ImageMagickAPI.FilterTypes.CatromFilter, 1), 'MagickResizeImage')
    }
    fn.setStr(dest);
    checkError(ImageMagickAPI.api.MagickWriteImage(wand, fn), 'MagickWriteImage');
};