安裝imagemagickhtml
sudo apt-get install imagemagick
ornode
wget http://www.imagemagick.org/download/ImageMagick.tar.gz tar zxvf ImageMagick.tar.gz cd ImageMagick ./configure --prefix=/usr/local make sudo make install
安裝GraphicsMagick for node http://aheckmann.github.com/gm/git
npm install gm
node.js代碼github
var fs, gm; gm = require('gm'); gm('./img/before.jpg').options({ imageMagick: true }).resize(130, 105).write('./img/after.jpg', function(err) { if (err) { return console.error(err); } return console.log('success'); });
使用文件流:npm
var fs, gm; gm = require('gm'); fs = require('fs'); gm(fs.createReadStream('./img/before.jpg')).options({ imageMagick: true }).resize(130, 105).stream().pipe(fs.createWriteStream('./img/after.jpg'));
處理網上圖片:ui
var fs, gm, request; gm = require('gm'); fs = require('fs'); request = require('request'); gm(request('http://url_to_img.jpg')).options({ imageMagick: true }).resize(130, 105).stream().pipe(fs.createWriteStream('./img/after.jpg'));
如出現如下錯誤,請參考:http://www.xgezhang.com/imagemagick_convert_problem.htmlthis
convert: no decode delegate for this image format `JPEG' @ error/constitute.c/ReadImage/501. convert: no images defined `img/after.jpg' @ error/convert.c/ConvertImageCommand/3212.
若是configure提示「configure: error: libltdl is required for modules build」,則yum search ltdl,而後(以CentOS 6.3 64bit 爲例)url
yum install libtool-ltdl.x86_64 yum install libtool-ltdl-devel.x86_64