nodejs 生成验证码
此方法需要nodejs 安装canvas 扩展
准备工作
以Linux为例
1、服务器gcc版本需4.8以上
2、安装所需扩展
yum install cairo cairo-devel cairomm-devel libjpeg-turbo-devel pango pango-devel pangomm pangomm-devel giflib-devel
3、安装canvas 扩展
npm install canvas
使用方法
示例代码
nodejs代码
/**
* 生成验证码
*/ var MdCode = module.exports;
var Consts = require('../comm/consts.js');
var Canvas = require('canvas'); function randomNum(min,max){
return Math.floor(Math.random()*(max-min)+min);
} function randomColor(min,max){
var _r = randomNum(min,max);
var _g = randomNum(min,max);
var _b = randomNum(min,max);
return "rgb("+_r+","+_g+","+_b+")";
} var getRandom = function(start,end){
return start + Math.random() * (end - start);
}; MdCode.create = function(params, req, callback) {
var width = 120;
var height = 35;
var canvas = new Canvas(width, height);
var ctx = canvas.getContext('2d');
// ctx.textBaseline = 'bottom';
//** 绘制背景色 **//
//颜色若太深可能导致看不清
ctx.fillStyle = randomColor(180,250);
ctx.fillRect(0, 0, width, height);
var code = ""; //** 绘制文字 **//
var start = 10;
var font = 'bold 20px arial';
var trans = {c:[-0.108, 0.108],b:[-0.05, 0.05]};
var str = 'abcdefghijklmnpqrstuvwxyz123456789';
for(var i = 0; i < 4; i++) {
var txt = str[randomNum(0, str.length)];
code += txt;
ctx.font = font;
ctx.fillStyle = randomColor(50, 160);
ctx.fillText(txt, start, 23, 10);
ctx.fillRect();
var c = getRandom(trans['c'][0],trans['c'][1]);
var b = getRandom(trans['b'][0],trans['b'][1]);
ctx.transform(1,b, c, 1, 0, 0);
start += 28; } //*** 绘制干扰线 ***//
for(var i = 0; i < 4; i++) {
ctx.strokeStyle = randomColor(40, 180);
ctx.beginPath();
ctx.moveTo( randomNum(0,width), randomNum(0,height) );
ctx.lineTo( randomNum(0,width), randomNum(0,height) );
ctx.stroke();
}
// ** 绘制干扰点 ** //
for (var i = 0; i < 50; i++) {
ctx.fillStyle = randomColor(0,255);
ctx.beginPath();
ctx.arc(randomNum(0,width),randomNum(0,height), 1, 0, 2*Math.PI);
ctx.fill();
} var buf = canvas.toDataURL();
var result = {};
result.statusCode = 0;
// buf为主要显示图像的数据
result.data = buf;
result.code = code; // 保存到session 用来验证
req.session.code = code; // 返还客户端
callback(result); }
html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>验证码</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport" />
<script type="text/javascript" src="/js/jquery.min.js"></script>
</head>
<body>
<h3 class="loginTitle contrast-blue">验证码</h3>
<div style="height: 80px;">
<label>验证码: </label>
<input type="text" style="width: 40%;" name="code" />
<img id="img_code" src="" style="width:100px;height:38px;vertical-align:bottom;position: relative;top: -38px;left: 200px;display: none;" title="点击刷新验证码" />
</div>
</body>
</html>
<script type="text/javascript">
// 刷新验证码
function refreshCode(){
$.ajax({
type: "get",
dataType: "json",
url: "/code",
success: function(msg){
$("#img_code").css('display', 'block');
$("#img_code").attr('src', msg.data); }
});
} $(document).ready(function() {
refreshCode();
});
$('#img_code').click(function(){
refreshCode();
}) </script>
效果预览 == 》 
nodejs 生成验证码的更多相关文章
- 动态生成验证码———MVC版
上面有篇博客也是写的验证码,但那个是适用于asp.net网站的. 今天想在MVC中实现验证码功能,弄了好久,最后还是看博友文章解决的,感谢那位博友. 首先引入生成验证码帮助类. ValidateCod ...
- laravel 生成验证码的方法
在Laravel中有很多图片验证码的库可以使用,本篇介绍其中之一:gregwar/captcha,这个库比较简单,在Laravel中比较常用.下面我们就来介绍下使用细节: 首先, composer.j ...
- java web学习总结(九) -------------------通过Servlet生成验证码图片
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:
- android 生成验证码图片
(转自:http://blog.csdn.net/onlyonecoder/article/details/8231373) package com.nobeg.util; import java.u ...
- PHP 动态生成验证码
……机器人会在网站中搜寻允许他们插入广告的输入表单,在虚拟世界没有什么能阻挡它们胡作非为.这些机器人效率极高,完全不关心所攻击的表单的本来用途.它们唯一的目标就是用它们的垃圾广告覆盖你的内容,残忍地为 ...
- PHP生成验证码及单实例应用
/* note: * this 指向当前对象本身 * self 指向当前类 * parent 指向父类 */ /* 验证码工具类 * @author pandancode * @date 20150- ...
- ASP.NET ashx实现无刷新页面生成验证码
现在大部分网站登陆时都会要求输入验证码,在网上也看了一些范例,现在总结一下如何实现无刷新页面生成验证码. 效果图: 实现方式: 前台: <div> <span>Identify ...
- PHP-仿ecshop生成验证码
<?php /* 生成验证码 */ // 1.创建画布(基于已有图像) $n = mt_rand(1,5); $im = imagecreatefromjpeg('./images/captch ...
- Java生成验证码原理(jsp)
验证码的作用: 验证码是Completely Automated Public Turing test to tell Computers and Humans Apart(全自动区分计算机和人类的 ...
随机推荐
- js获取dom对象style样式的值
js获取到的dom对象的style通常是没有值得,因为我们都写在外部文件中,从慕课网上见到讲师封装的一个方法,挺不错.特此记录下来. function getStyle(obj,attr){ if(o ...
- WebApi发送HTML表单数据:文件上传与多部分MIME
5.3 Sending HTML Form Data5.3 发送HTML表单数据(2) 本文引自:http://www.cnblogs.com/r01cn/archive/2012/12/20/282 ...
- vim 使用、设置笔记
一.设置.vimrc( windows下通常为_vimrc) 1.设置vim中tab的缩进 set ts=4 (注:ts是tabstop的缩写,设TAB宽4个空格) set expandtab (注 ...
- nginx: error while loading shared libraries: libGeoIP.so.1
wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz wget http://geolite.maxmind.com/do ...
- codevs 1155今明的预算方案(复习有依赖性的背包问题)
1155 金明的预算方案 [题目大意]买附件必须买主件. 在一定钱数内 求总价值最大. [题解]有依赖性的背包问题. [code] #include<iostream> #include& ...
- HDU2444(二分图判定+最大匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- java集合框架之ArrayList与LinkedList的区别
参考http://how2j.cn/k/collection/collection-arraylist-vs-linkedlist/690.html#nowhere ArrayList和LinkedL ...
- 不能支持C++11的特性~,升级到4.8.2
一.简易安装 操作环境 CentOS6.5 64bit,原版本4.4.7,不能支持C++11的特性~,希望升级到4.8.2 不能通过yum的方法升级,需要自己手动下载安装包并编译 1.1 获取安装包并 ...
- bzoj 4464: [Jsoi2013]旅行时的困惑【贪心】
据说正解是有上下界最小流,但是这种1e5的玩意问什么要跑网络流啊-- 贪心即可,注意一点是可以有多条路径经过一条边-- 以1为根,设d[u][0/1]为u到父亲的边是向下/向上,g记录这个点儿子中不能 ...
- Html5shiv ---- 让IE低版本浏览器识别并支持HTML5标签
Html5shiv.js是针对IE浏览器的 javaScript 补丁,作用如题 该脚本的下载链接 使用使在head标签中使用script标签引用即可