convert = function (options) { var deferred = Q.defer(); function imgConvert() { if (options.src === undefined || options.dst === undefined) return deferred.reject(error_messages['path']); var args = [options.src] if (options.quality) { args.push('-quality') args.push(options.quality) } if (options.flatten) { args.push('-flatten') if (options.background) { args.push('-background') args.push(options.background) } } else { if (options.background) { args.push('-background') args.push(options.background) args.push('-flatten') } } args.push(options.dst) child = exec('convert', args, function(err, stdout, stderr) { if (err) deferred.reject(err); else deferred.resolve(info(options.dst)); }); } directoryCheck(options, imgConvert) return deferred.promise; }
...
var easyimg = require('easyimage');
```
EasyImage offers these promise methods:
```
easyimg.info(<image_path>) - to retrieve information about an image. Will return an object with the following properties
- type, depth, width, height, size, density, name, and path.
easyimg.convert(<options>) - to convert an image from one format to another
.
easyimg.resize(<options>) - to resize an image.
easyimg.crop(<options>) - to crop an image.
easyimg.thumbnail(<options>) - to create square thumbnails.
easyimg.rescrop(<options>) - to resize and crop and image in one go, useful for creating customzied thumbnails.
easyimg.rotate(<options>) - to rotate an image.
easyimg.exec(<command>) - when you want to call a custom command to ImageMagick, you will need to take care of escaping
special characters etc.
```
...
crop = function (options) { var deferred = Q.defer(); function imgCrop() { if (options.src === undefined || options.dst === undefined) return deferred.reject(error_messages['path']); if (options.cropwidth === undefined) return deferred.reject(error_messages['dim']); options.cropheight = options.cropheight || options.cropwidth; options.gravity = options.gravity || 'Center'; options.x = options.x || 0; options.y = options.y || 0; var args = [options.src] if (options.flatten) { args.push('-flatten') if (options.background) { args.push('-background') args.push(options.background) } } else { if (options.background) { args.push('-background') args.push(options.background) args.push('-flatten') } } args.push('-auto-orient') args.push('-gravity') args.push(options.gravity) args.push('-crop') args.push(options.cropwidth + 'x'+ options.cropheight + '+' + options.x + '+' + options.y) if (options.quality) { args.push('-quality') args.push(options.quality) } if (options.background) { args.push('-background') args.push(options.background) } args.push(options.dst) child = exec('convert', args, function(err, stdout, stderr) { if (err) deferred.reject(err); deferred.resolve(info(options.dst)); }); } directoryCheck(options, imgCrop) return deferred.promise; }
...
EasyImage offers these promise methods:
```
easyimg.info(<image_path>) - to retrieve information about an image. Will return an object with the following properties
- type, depth, width, height, size, density, name, and path.
easyimg.convert(<options>) - to convert an image from one format to another.
easyimg.resize(<options>) - to resize an image.
easyimg.crop(<options>) - to crop an image.
easyimg.thumbnail(<options>) - to create square thumbnails.
easyimg.rescrop(<options>) - to resize and crop and image in one go, useful for creating customzied thumbnails.
easyimg.rotate(<options>) - to rotate an image.
easyimg.exec(<command>) - when you want to call a custom command to ImageMagick, you will need to take care of escaping
special characters etc.
```
**NOTE**: `easyimg.exec()` spawns a subshell.
...
exec = function (cmd) { var deferred = Q.defer(); process.nextTick(function () { child = command(cmd, function(err, stdout, stderr) { if (err) return deferred.reject(err); deferred.resolve(stdout); }); }) return deferred.promise; }
...
------------------
1. Use child_process.execFile instead of child_process.exec to prevent potential shellshock exploit
1.0.4 - 24-12-2014
------------------
1. Filenames with spaces supported in `.exec()`
...
info = function (file) { return info(file); }
...
```
var easyimg = require('easyimage');
```
EasyImage offers these promise methods:
```
easyimg.info(<image_path>) - to retrieve information about an image. Will return
an object with the following properties - type, depth, width, height, size, density, name, and path.
easyimg.convert(<options>) - to convert an image from one format to another.
easyimg.resize(<options>) - to resize an image.
easyimg.crop(<options>) - to crop an image.
easyimg.thumbnail(<options>) - to create square thumbnails.
easyimg.rescrop(<options>) - to resize and crop and image in one go, useful for creating customzied thumbnails.
easyimg.rotate(<options>) - to rotate an image.
easyimg.exec(<command>) - when you want to call a custom command to ImageMagick, you will need to take care of escaping
special characters etc.
...
rescrop = function (options) { var deferred = Q.defer(); function imgResCrop() { if (options.src === undefined || options.dst === undefined) return deferred.reject(error_messages['path']); if (options.width === undefined) return deferred.reject(error_messages['dim']); options.height = options.height || options.width; options.cropwidth = options.cropwidth || options.width; options.cropheight = options.cropheight || options.height; options.gravity = options.gravity || 'Center'; options.x = options.x || 0; options.y = options.y || 0; options.fill = options.fill ? '^' : ''; var args = [options.src] if (options.flatten) { args.push('-flatten') if (options.background) { args.push('-background') args.push(options.background) } } else { if (options.background) { args.push('-background') args.push(options.background) args.push('-flatten') } } args.push('-auto-orient') args.push('-gravity') args.push(options.gravity) args.push('-resize') args.push(options.width + 'x' + options.height + options.fill) args.push('-crop') args.push(options.cropwidth + 'x'+ options.cropheight + '+' + options.x + '+' + options.y) if (options.quality) { args.push('-quality') args.push(options.quality) } if (options.background) { args.push('-background') args.push(options.background) } args.push(options.dst) child = exec('convert', args, function(err, stdout, stderr) { if (err) deferred.reject(err); deferred.resolve(info(options.dst)); }); } directoryCheck(options, imgResCrop) return deferred.promise; }
...
```
easyimg.info(<image_path>) - to retrieve information about an image. Will return an object with the following properties
- type, depth, width, height, size, density, name, and path.
easyimg.convert(<options>) - to convert an image from one format to another.
easyimg.resize(<options>) - to resize an image.
easyimg.crop(<options>) - to crop an image.
easyimg.thumbnail(<options>) - to create square thumbnails.
easyimg.rescrop(<options>) - to resize and crop and image in one go, useful
for creating customzied thumbnails.
easyimg.rotate(<options>) - to rotate an image.
easyimg.exec(<command>) - when you want to call a custom command to ImageMagick, you will need to take care of escaping
special characters etc.
```
**NOTE**: `easyimg.exec()` spawns a subshell.
The EasyImage options object can have these properties depending on the method. Unrelated options are ignored.
...
resize = function (options) { var deferred = Q.defer(); function imgResize() { if (options.src === undefined || options.dst === undefined) return deferred.reject(error_messages['path']); if (options.width === undefined) return deferred.reject(error_messages['dim']); options.height = options.height || options.width; var args = [options.src] if (options.flatten) { args.push('-flatten') if (options.background) { args.push('-background') args.push(options.background) } } else { if (options.background) { args.push('-background') args.push(options.background) args.push('-flatten') } } args.push('-auto-orient') args.push('-resize') args.push(options.width + 'x' + options.height) if (options.ignoreAspectRatio) { args[args.length-1] += '!'; } if (options.quality) { args.push('-quality') args.push(options.quality) } if (options.background) { args.push('-background') args.push(options.background) } args.push(options.dst) child = exec('convert', args, function(err, stdout, stderr) { if (err) deferred.reject(err); deferred.resolve(info(options.dst)); }); } directoryCheck(options, imgResize) return deferred.promise; }
...
```
EasyImage offers these promise methods:
```
easyimg.info(<image_path>) - to retrieve information about an image. Will return an object with the following properties
- type, depth, width, height, size, density, name, and path.
easyimg.convert(<options>) - to convert an image from one format to another.
easyimg.resize(<options>) - to resize an image.
easyimg.crop(<options>) - to crop an image.
easyimg.thumbnail(<options>) - to create square thumbnails.
easyimg.rescrop(<options>) - to resize and crop and image in one go, useful for creating customzied thumbnails.
easyimg.rotate(<options>) - to rotate an image.
easyimg.exec(<command>) - when you want to call a custom command to ImageMagick, you will need to take care of escaping
special characters etc.
```
**NOTE**: `easyimg.exec()` spawns a subshell.
...
rotate = function (options) { var deferred = Q.defer(); function imgRotate() { if (options.src === undefined || options.dst === undefined || options.degree === undefined) return deferred.reject(error_messages ['path']); var args = [options.src] if (options.flatten) { args.push('-flatten') if (options.background) { args.push('-background') args.push(options.background) } } else { if (options.background) { args.push('-background') args.push(options.background) args.push('-flatten') } } args.push('-rotate') args.push(options.degree) if (options.background) { args.push('-background') args.push(options.background) } args.push(options.dst) child = exec('convert', args, function(err, stdout, stderr) { if (err) deferred.reject(err); else deferred.resolve(info(options.dst)); }); } directoryCheck(options, imgRotate) return deferred.promise; }
...
```
easyimg.info(<image_path>) - to retrieve information about an image. Will return an object with the following properties
- type, depth, width, height, size, density, name, and path.
easyimg.convert(<options>) - to convert an image from one format to another.
easyimg.resize(<options>) - to resize an image.
easyimg.crop(<options>) - to crop an image.
easyimg.thumbnail(<options>) - to create square thumbnails.
easyimg.rescrop(<options>) - to resize and crop and image in one go, useful for creating customzied thumbnails.
easyimg.rotate(<options>) - to rotate an image.
easyimg.exec(<command>) - when you want to call a custom command to ImageMagick, you will need to take care of escaping
special characters etc.
```
**NOTE**: `easyimg.exec()` spawns a subshell.
The EasyImage options object can have these properties depending on the method. Unrelated options are ignored.
```
...
thumbnail = function (options) { var deferred = Q.defer(); function imgThumbnail() { if (options.src === undefined || options.dst === undefined) return deferred.reject(error_messages['path']); if (options.width === undefined) return deferred.reject(error_messages['dim']); options.height = options.height || options.width; options.gravity = options.gravity || 'Center'; options.x = options.x || 0; options.y = options.y || 0; info(options.src).then(function(original) { // dimensions come as strings, convert them to number original.width = +original.width; original.height = +original.height; var resizewidth = options.width; var resizeheight = options.height; if (original.width > original.height) { resizewidth = ''; } else if (original.height > original.width) { resizeheight = ''; } var args = [options.src] if (options.flatten) { args.push('-flatten') if (options.background) { args.push('-background') args.push(options.background) } } else { if (options.background) { args.push('-background') args.push(options.background) args.push('-flatten') } } args.push('-auto-orient') args.push('-gravity') args.push(options.gravity) args.push('-interpolate') args.push('bicubic') args.push('-strip') args.push('-thumbnail') args.push(resizewidth + 'x' + resizeheight) args.push('-crop') args.push(options.width + 'x'+ options.height + '+' + options.x + '+' + options.y) if (options.quality) { args.push('-quality') args.push(options.quality) } if (options.background) { args.push('-background') args.push(options.background) } args.push(options.dst) child = exec('convert', args, function(err, stdout, stderr) { if (err) return deferred.reject(err); deferred.resolve(info(options.dst)); }); }, function (err) { deferred.reject(err); }); } directoryCheck(options, imgThumbnail) return deferred.promise; }
...
EasyImage offers these promise methods:
```
easyimg.info(<image_path>) - to retrieve information about an image. Will return an object with the following properties
- type, depth, width, height, size, density, name, and path.
easyimg.convert(<options>) - to convert an image from one format to another.
easyimg.resize(<options>) - to resize an image.
easyimg.crop(<options>) - to crop an image.
easyimg.thumbnail(<options>) - to create square thumbnails.
easyimg.rescrop(<options>) - to resize and crop and image in one go, useful for creating customzied thumbnails.
easyimg.rotate(<options>) - to rotate an image.
easyimg.exec(<command>) - when you want to call a custom command to ImageMagick, you will need to take care of escaping
special characters etc.
```
**NOTE**: `easyimg.exec()` spawns a subshell.
The EasyImage options object can have these properties depending on the method. Unrelated options are ignored.
...