如何在电子应用程序内安装imagemagick

时间:2017-07-11 16:15:52

标签: imagemagick electron

我们有一个在OSX上使用Imagemagick的电子应用程序,我们已经预先安装了brew install。它在开发中运行良好,但是当我们打包应用程序时 - 它无法找到imagemagick。

我们可以在设置应用程序之前自行安装imagemagick吗?我们将如何做到这一点?

2 个答案:

答案 0 :(得分:3)

如果使用electron-builder(我推荐),您只需在package.json中添加postinstall脚本即可安装Imagemagick

在package.json

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

或者,如果您不想安装它,或者brew可能尚未在目标计算机上使用,您可以将imagemagick安装到应用程序中的文件夹中,然后将其添加到package.json的extraResources键中

"extraResources": ["imagemagick/"]

这将告诉电子工程师将此文件夹捆绑到存档中。然后只需从该文件夹中引用imagmagick。

答案 1 :(得分:0)

当电子构建封装时,/usr/local/bin似乎不是路径的一部分。

我将其添加到使用imagemagick的脚本中的PATH中。

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

这仍然假定用户已在计算机上安装了它。

或者您可以指定直接路径。

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

但是identity依赖于gs(Ghost脚本),在我的情况下,该脚本也位于/usr/local/bin中。因此,无论如何,我必须将其添加到我的路径中。

此外,我遇到了尚未尝试的这个软件包。

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, '/')
    });
}

然后您可以根据需要致电identifyconvert

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