nodejs 基于gm模块生成验证码
var gm = require('gm').subClass({ imageMagick: true }) exports.auto_captcha = function(opt ,cb){ var a = Math.floor(Math.random()*10); var b = Math.floor(Math.random()*10); opt.text = a+"+"+b+"="; console.log('opt.test is ',opt.text); opt.captcha_value =a+b; exports.captcha(opt,cb); } exports.captcha = function(opt ,cb ){ var width =opt.width||60; var height = opt.height|| 40; var font_size = opt.font_size ||20; var x = 6; var y = (height- font_size)/2+font_size; return gm(width,height, opt.bg_color ||"#eeeeee") .setFormat('png') .font(__dirname + "/DejaVuLGCSans.ttf") //解决容器中不能找不到字体的问题。导致图片可以生成,但是图片上面的数字内容显示不出来 .fontSize(font_size) .drawLine(parseInt(Math.random()*10+50), parseInt(Math.random()*5), parseInt(Math.random()*60), parseInt(Math.random()*60)) .drawLine(parseInt(Math.random()*20)+40, parseInt(Math.random()*10), parseInt(Math.random()*60), parseInt(Math.random()*60)) .drawLine(parseInt(Math.random()*30)+30, parseInt(Math.random()*15), parseInt(Math.random()*60), parseInt(Math.random()*60)) .drawLine(parseInt(Math.random()*10), parseInt(Math.random()*20), parseInt(Math.random()*60), parseInt(Math.random()*60)) .drawLine(parseInt(Math.random()*20), parseInt(Math.random()*25), parseInt(Math.random()*60), parseInt(Math.random()*60)) .drawText(x, y, opt.text) .noise("Impulse") .implode(0.3) .stream(function(e,so ,se){ cb(e, so,se); }); }
@南非波波
github:https://github.com/swht