jrutter
jrutter

Reputation: 3213

How to install imagemagick inside of electron app

We have an electron app that uses Imagemagick on OSX, we have pre-installed that with brew install. It works fine in development, but when we package the app - it cant find imagemagick.

Can we brew install imagemagick before setting up the app? How would we go about doing this?

Upvotes: 9

Views: 2650

Answers (2)

styks
styks

Reputation: 3461

It appears /usr/local/bin is not part of the path when electron builds the package.

I added it to my PATH in the script that uses imagemagick.

process.env.PATH += ':/usr/local/bin';

This still assumes the user has it installed on their computer.

Or you can specify the direct path.

const {identify} = require('imagemagick');
identify.path = '/usr/local/Cellar/imagemagick/7.0.8-11_2/bin/identify';

But identify relies on gs (Ghost Script) which in my case was also in /usr/local/bin. So I had to add that to my path anyway.

Also, I came across this package which I have yet to try.

https://github.com/joeherold/imagemagick-darwin-static

const os = require('os');
const path = require('path');

const graphicsmagick = require('graphicsmagick-static');
const imagemagick = require('imagemagick-darwin-static');

const {subClass} = require('gm');
let gm;

if (os.platform() == 'win32') {
    gm = subClass({
        appPath: path.join(graphicsmagick.path, '/')
    });
} else {
    gm = subClass({
        imageMagick: true,
        appPath: path.join(imagemagick.path, '/')
    });
}

Then you can call identify or convert as needed

    const getData = new Promise((resolve, reject) => {
        gm(filepath).identify({}, (err, features) => {
            if (err) {
                reject(err);
            }
            resolve(features);
        });
    });

Upvotes: 0

AlienHoboken
AlienHoboken

Reputation: 2810

If using electron-builder (which I recommend) you can simply add a postinstall script to your package.json to install Imagemagick

In package.json

"scripts": {
    "postinstall": "brew install imagemagick"
}

Alternatively if you don't want to install it, or brew might not already be available on target machines, you can install imagemagick into a folder within the app then add that to the extraResources key of package.json

"extraResources": ["imagemagick/"]

This will tell electron-builder to bundle this folder into the archive. Then just reference imagmagick from that folder.

Upvotes: 6

Related Questions