node.js下的canvas,可加水印,可验证码,可裁剪图片

系统环境:centos6.2

1.安装cario

  • sudo yum erase cairo

因为centos6.2的二进制源有问题,先卸载原有的,没则跳过


安装需要的环境包

  • yum -y install automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig libpng-devel

导入环境变量

  • export PKG_CONFIG_PATH='/usr/local/lib/pkgconfig'  
  • export LD_LIBRARY_PATH='/usr/local/lib':$LD_LIBRARY_PATH  

安装应用

  • curl http://www.cairographics.org/releases/pixman-0.22.0.tar.gz -o pixman.tar.gz  
  • tar -zxf pixman.tar.gz && cd pixman-0.22.0/  
  •  ./configure --prefix=/usr/local   
  • make install 

  • wget http://www.ijg.org/files/jpegsrc.v8c.tar.gz
  • ./configure --prefix=/usr/local   
  • make 
  • make install  

  • wget http://cairographics.org/releases/cairo-1.10.2.tar.gz
  • ./configure --prefix=/usr/local   
  • make 
  • make install  


安装成功后,安装node.js的canvas插件
  • npm install canvas

2.测试安装

var Canvas = require('canvas')
  , canvas = new Canvas(200,200)
  , ctx = canvas.getContext('2d');

ctx.font = '30px Impact';
ctx.rotate(.1);
ctx.fillText("Awesome!", 50, 100);

var te = ctx.measureText('youudehexie!');
ctx.strokeStyle = 'rgba(0,0,0,0.5)';
ctx.beginPath();
ctx.lineTo(50, 102);
ctx.lineTo(50 + te.width, 102);
ctx.stroke();

console.log('<img src="' + canvas.toDataURL() + '" />');


基于这么模块,我们可以做很多应用

如验证码:http://jack.datamm.com/post/nodejs/node-canvas-gererate.html

水印:待大家弄了,其实也不难。不过还是推荐大家先在前端加好水印,如flash上传,先加上水印,再上传。


你可能感兴趣的:(canvas,canvas,centos,centos,centos,node.js,node.js,node.js,canvas安装)