node 图片合成

本篇关于两个工具模块imagesgm的介绍与问题处理

一、本地最简单的方式 - images模块

1
2
3
4
5
const images = require('images')

images(distPath)
.draw(images(tag), 0, 0)
.save(distPath);

mac 上运行正常,centos上需要自己编译各种不同的文件。

1
Error: Can't load the addon. Issue to: https://github.com/zhangyuanwei/node-images/issues Error: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by /data/webroot/www/wx_edit/node_modules/images/bindings/linux/x64/6.0.0/binding.node)

具体见 https://github.com/zhangyuanwei/node-images/issues/45

结论

操作太复杂成本太高

二、gm模块

推荐使用

https://github.com/aheckmann/gm

调用的相关其他工具,GraphicsMagick、ImageMagick

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function comImage(distPath, tag) {
return new Promise((resolve, reject) => {
gm(distPath)
.command('composite')
.in('-gravity', 'NorthWest')
.in(tag)
.write(distPath, function(err) {
if (!err) {
resolve(distPath);
} else {
reject(err);
}
});
});
}

mac 上运行正常,centos上通过yum install imagemagickyum install graphicsmagick没法玩。

需要执行安装工具,见

  1. How to install GraphicsMagick on CentOS 6.4
  2. Install Graphics Magick on CentOS 7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash

# Install build dependencies
yum install -y gcc libpng libjpeg libpng-devel libjpeg-devel ghostscript libtiff libtiff-devel freetype freetype-devel

# Get GraphicsMagick source
wget ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/1.3/GraphicsMagick-1.3.9.tar.gz
tar zxvf GraphicsMagick-1.3.9.tar.gz

# Configure and compile
cd GraphicsMagick-1.3.9
./configure --enable-shared
make
make install

# Ensure everything was installed correctly
gm version

# If you are going to use this with PHP you can also instal the pecl extension
pecl install gmagick-1.0.8b2

继续坑 - 如果报错

1
gm: error while loading shared libraries: libGraphicsMagick.so.3: cannot open shared object file: No such file or directory

解决方式

1
2
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig

  1. GraphicsMagick
  2. How to install GraphicsMagick with GMagick PHP extension

最终建议

centOS上的依赖安装,让专业的运维操作解决