Code Monkey home page Code Monkey logo

magick-cli's Introduction

MagickCLI

Native ImageMagick CLI for Node.js

Introduction

ImageMagick is a free and open-source software suite for displaying, converting, and editing raster and vector image files. It utilizes multiple computational threads to increase performance and can read, process, or write mega-, giga-, or tera-pixel image sizes.

More resource and info about ImageMagick

Motivations

At the time i created this module i was not able to find any module on npm that execute ImageMagick command through its C API, otherwise there were some module that call ImageMagick through the execution of the corresponding shell command. This is a good way to start using some library from node, but there are the following drawbacks:

  • Performance - The call to the shell command take more time and more resources than calling a library C or C++ API directly from Node.js environment.

  • Errror handler - Sometimes you cannot intercept and handle errors in a good and a proper way.

To fit all needs MagickCLI has sync and async methods so it could be used in a web application where it's very important to not block the event loop, so all requests will be served without any delay originated by our application.

Understanding Node.js event loop

Prerequisites

Before installing MagickCLI you need to assure you have the following prerequisites:

At the moment MagickCLI is fully compatible with ImageMagick from version 7.0.1 to 7.0.8

Linux

You can install ImageMagick from binary or directly from source:

Maybe your linux distribution has already packaged ImageMagick version 7.0.1 or above so you can install it using your package manager and all is more simple.

Windows

macOS

  • Install Homebrew following the official guide here

  • Open terminal and install ImageMagick

brew install imagemagick
  • Open terminal and install pkg-info
brew install pkg-info

Installation

If you want to use MagickCLI you have to install it. There are two methods for that:

In dependencies of your package.json add the following item:

"magick-cli": "version"

then digit

npm install

Example:

"magick-cli": "*" for the latest version
"magick-cli": "1.0.0" for the version 1.0.0

OR

launch this command:

npm install magick-cli --save

Usage

'use strict'

const MagickCLI = require('magick-cli')

try {
  // Take decision based on ImageMagick version
  const version = MagickCLI.version()
  console.log(version)
  MagickCLI.executeSync('magick input.png -resize 50% output.png')
} catch (err) {
  // Handle error
  throw err
}

API

version

version() method returns an string that represent the version of ImageMagick library installed on the system. It is important in those circumstances where you have to take decision based on different version.

Example - version

'use strict'

const MagickCLI = require('magick-cli')

try {
  const version = MagickCLI.version()
  console.log(version)
  // Take decision based on ImageMagick version
  if (version != '7.0.1') {
    // ... some stuff
  } else {
    // ... other stuff
  }
} catch (err) {
  // Handle error
  throw err
}

executeSync

executeSync(cmd) method takes the ImageMagick command parameters in input as a string and executes in a synchronous way. If something wrong happens in calling this method an Error with description and code error will be thrown.

Example - executeSync

'use strict'

const MagickCLI = require('magick-cli')

try {
  MagickCLI.executeSync('magick input.png -resize 50% output.png')
} catch (err) {
  // Handle error
  throw err
}

execute

execute(cmd, callback) method takes in input the ImageMagick command parameters as a string and an optional callback. The execution will be asynchronous so this ensure better performance especially in a web application enviroment, because it'll not block the Node.Js event loop. This method has an optional callback function as input, in that case, a possible error will be handled by this function. If noone function will be provided the method returns a Promise that will be resolved or rejected as reported in the following example.

Example - execute

'use strict'

const MagickCLI = require('magick-cli')

let cmd = 'magick input.png -resize 50% output.png'
MagickCLI.execute(cmd, function (err) {
  if (err) {
    console.log("Ooops... something wrong happened")
  }
})
'use strict'

const MagickCLI = require('magick-cli')

let cmd = '-magick input.png -resize 50% output.png'
MagickCLI.execute(cmd)
.then(() => {
  console.log("All is ok")
})
.catch((err) => {
 console.log("Ooops... something wrong happened")
})

Error

The error raised from magick-cli in all of its method is an instance of Error object that cointains a message that describes what happened and at the same time cointains the ImageMagick error code so you can inspect what happened in a better way.

Min and Max supported revision

This module was built based on ImageMagick C API that is compatible with some specifics versions. The module has two properties MIN_SUPPORTED_REVISION and MAX_SUPPORTED_REVISION which respectively indicate the minimum and maximum supported ImageMagick's version.

Example - Min and Max supported revision

'use strict'

const MagickCLI = require('magick-cli')

console.log(MagickCLI.MIN_SUPPORTED_REVISION)
console.log(MagickCLI.MAX_SUPPORTED_REVISION)

The Team

Nicola Del Gobbo

https://github.com/NickNaso/

https://www.npmjs.com/~nicknaso

https://twitter.com/NickNaso

Mauro Doganieri

https://github.com/mauro-d

https://www.npmjs.com/~mauro-d

https://twitter.com/maurodoganieri

Acknowledgements

Thank you to all people that encourage me every day.

License

Licensed under Apache license V2

magick-cli's People

Contributors

dlemstra avatar nicknaso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

dlemstra

magick-cli's Issues

Create version method

Create version method with the following signature: MagickCLI.version() - Add constant to indicate the maximum and minimum of supported version of ImageMagick

magick-cli on AWS Lambda

I am trying to resize image using native image magick-cli. It is working fine in my mac. When I upload the zipped code to AWS lambda, it is failing with error
module initialization error: Error at Object.Module._extensions..node (module.js:681:18) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at bindings (/var/task/node_modules/bindings/bindings.js:81:44) at Object.<anonymous> (/var/task/node_modules/magick-cli/lib/magick-cli.js:25:38) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10)

Does the node modules of mac binaries not sufficient for execution of lambda? How to install magick-cli on lambda.

binding.gyp options for Windows

Here the experimented binding.gyp options for Windows OS

{
    'variables': {
        'MAGICK_ROOT%': '<!(magick-cli-path.py)'
    },
    "targets": [
        {
            "target_name": "MagickCLI",
            "sources": ["src/magick-cli.cc"],
            "include_dirs": [
             "<!(node -e \"require('nan')\")",
             '<(MAGICK_ROOT)/include'
            ],
             "libraries": [
                '-l<(MAGICK_ROOT)/lib/CORE_RL_MagickCore_.lib',
                '-l<(MAGICK_ROOT)/lib/CORE_RL_MagickWand_.lib',
          ]
        }
    ]
}

npm package failing to install

Hey guys,
sadly I currently cannot install your tool via npm.
I'm running on: node v6.11.0, npm v3.10.10, node-gyp v3.6.2

The issues seem hail from ./src/magick-cli.cc, and I am thoroughly clueless in C++.

I'm getting the following error description upon npm install magick-cli:

timmimim@hal9000 ~path/to/project : npm install --save magick-cli

> [email protected] install /home/timmimim/ownCloud/o2r-data/Hilfskräfte/Kühnel/Checker/erc-checker/node_modules/magick-cli
> node-gyp rebuild

make: Entering directory `/home/timmimim/ownCloud/o2r-data/Hilfskräfte/Kühnel/Checker/erc-checker/node_modules/magick-cli/build'
  CXX(target) Release/obj.target/MagickCLI/src/magick-cli.o
../src/magick-cli.cc:27:5: error: ‘mutex’ does not name a type
     mutex mc;
     ^
../src/magick-cli.cc: In member function ‘void MagickCLIManager::Execute(int, char**)’:
../src/magick-cli.cc:63:5: error: ‘lock_guard’ was not declared in this scope
     lock_guard<mutex> lk(mc);
     ^
../src/magick-cli.cc:63:16: error: ‘mutex’ was not declared in this scope
     lock_guard<mutex> lk(mc);
                ^
../src/magick-cli.cc:63:26: error: ‘mc’ was not declared in this scope
     lock_guard<mutex> lk(mc);
                          ^
../src/magick-cli.cc:63:28: error: ‘lk’ was not declared in this scope
     lock_guard<mutex> lk(mc);
                            ^
make: *** [Release/obj.target/MagickCLI/src/magick-cli.o] Error 1
make: Leaving directory `/home/timmimim/ownCloud/o2r-data/Hilfskräfte/Kühnel/Checker/erc-checker/node_modules/magick-cli/build'
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:276:23)
gyp ERR! stack     at emitTwo (events.js:106:13)
gyp ERR! stack     at ChildProcess.emit (events.js:191:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)
gyp ERR! System Linux 4.4.0-83-generic
gyp ERR! command "/usr/bin/nodejs" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/timmimim/ownCloud/o2r-data/Hilfskräfte/Kühnel/Checker/erc-checker/node_modules/magick-cli
gyp ERR! node -v v6.11.0
gyp ERR! node-gyp -v v3.4.0
gyp ERR! not ok 
npm ERR! Linux 4.4.0-83-generic
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "install" "--save" "magick-cli"
npm ERR! node v6.11.0
npm ERR! npm  v3.10.10
npm ERR! code ELIFECYCLE

npm ERR! [email protected] install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] install script 'node-gyp rebuild'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the magick-cli package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs magick-cli
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls magick-cli
npm ERR! There is likely additional logging output above.

Sorry for the long message, but can you help me out?
Thanks!

Kind regards,
Timm

ImageMagick documentation

Here the documentation for ImageMagick
ImageMagick includes a number of ready-made interfaces. This makes it possible to modify or create images automagically and dynamically utilizing your favorite development platform.
https://www.imagemagick.org/script/develop.php
Magick++ API is the object-oriented C++ API to the ImageMagick image-processing library.
https://www.imagemagick.org/script/magick++.php
MagickCore
https://www.imagemagick.org/api/MagickCore/index.html
MagickCLI
https://www.imagemagick.org/api/MagickWand/struct__MagickCLI.html
Examples use of MagickCLI
https://github.com/ImageMagick/ImageMagick/tree/master/api_examples

Start from these documentations to create the module.

N-API porting of magick-cli

I think that we can start to port magick-cli to N-API and after that go forward to add the new requested features that end user requested us.
I will provide to create a new branch node-addon-api to start the porting.

Problem execute async method

An error ssued executing async method execute

 Promise
        .all([MagickCLI.execute(cmdAsync), MagickCLI.execute(cmdAsync)])
        .then((res) => {
            done()
        })
        .catch((err) => {
            console.log(err)
            done()
        })

The error reported is:

 MagickCore/semaphore.c:451: UnlockSemaphoreInfo: Assertion `semaphore_info != (SemaphoreInfo *) NULL' failed.
Aborted (core dumped)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.