kaptcha
Advanced tools
Comparing version 1.0.2 to 1.0.3
var Canvas = require('canvas'); | ||
module.exports = function(params){ | ||
if(typeof params == 'string') | ||
params = { url: params }; | ||
params.color = params.color || 'rgb(0,100,100)'; | ||
params.background = params.background || 'rgb(255,200,150)'; | ||
function generateCode() { | ||
return ('' + Math.random()).substr(3, 6); | ||
} | ||
function generateImage(req, res, params) { | ||
params.color = params.color || 'rgb(0, 0, 0)'; | ||
params.background = params.background || 'rgb(255, 255, 255)'; | ||
params.width = params.width || 250; | ||
@@ -15,53 +17,51 @@ params.height = params.height || 150; | ||
return function(req, res, next){ | ||
if(req.url != params.url) | ||
return next(); | ||
var offset = params.width * 0.4 * Math.random(); | ||
var offset = params.width * 0.4 * Math.random(); | ||
var canvas = new Canvas(params.width, params.height); | ||
var ctx = canvas.getContext('2d'); | ||
ctx.antialias = 'gray'; | ||
ctx.fillStyle = params.background; | ||
ctx.fillRect(0, 0, params.width, params.height); | ||
ctx.fillStyle = params.color; | ||
ctx.lineWidth = params.fontHeight / 10; | ||
ctx.strokeStyle = params.color; | ||
ctx.font = params.fontHeight + 'px sans'; | ||
var canvas = new Canvas(params.width, params.height); | ||
var ctx = canvas.getContext('2d'); | ||
ctx.antialias = 'gray'; | ||
ctx.fillStyle = params.background; | ||
ctx.fillRect(0, 0, params.width, params.height); | ||
ctx.fillStyle = params.color; | ||
ctx.lineWidth = params.fontHeight / 10; | ||
ctx.strokeStyle = params.color; | ||
ctx.font = params.fontHeight + 'px sans'; | ||
for (var i = 0; i < 2; i++) { | ||
ctx.moveTo(offset, Math.random() * params.innerWidth); | ||
ctx.bezierCurveTo( | ||
params.width * 0.32, | ||
Math.random() * params.height, | ||
params.width * 0.64, | ||
Math.random() * params.height, | ||
params.width * 0.92, | ||
Math.random() * params.height); | ||
ctx.stroke(); | ||
} | ||
for (var i = 0; i < 2; i++) { | ||
ctx.moveTo(offset, Math.random() * params.innerWidth); | ||
ctx.bezierCurveTo( | ||
//params.width * 0.24 + offset, | ||
params.width * 0.32, | ||
Math.random() * params.height, | ||
params.width * 0.64, | ||
//params.width * 0.58 + offset, | ||
Math.random() * params.height, | ||
params.width * 0.92, | ||
//params.width * 0.86 + offset, | ||
Math.random() * params.height); | ||
ctx.stroke(); | ||
} | ||
var text = params.text || generateCode(); | ||
var text = ('' + Math.random()).substr(3, 6); | ||
for (i = 0; i < text.length; i++) { | ||
ctx.setTransform(Math.random() * 0.5 + 1, Math.random() * 0.4, Math.random() * 0.4, Math.random() * 0.5 + 1, params.fontWidth * i + offset, params.height * 2 / 3); | ||
ctx.fillText(text.charAt(i), 0, 0); | ||
} | ||
for (i = 0; i < text.length; i++) { | ||
ctx.setTransform(Math.random() * 0.5 + 1, Math.random() * 0.4, Math.random() * 0.4, Math.random() * 0.5 + 1, params.fontWidth * i + offset, params.height * 2 / 3); | ||
ctx.fillText(text.charAt(i), 0, 0); | ||
} | ||
canvas.toBuffer(function(err, buf) { | ||
if(req.session) | ||
req.session.captcha = text; | ||
res.end(buf); | ||
}); | ||
} | ||
// ctx.setTransform(1, 0, 0, 1, 0, 0); | ||
// ctx.font = '25px sans'; | ||
// ctx.fillStyle = "rgb(255,255,255)"; | ||
// ctx.fillText(text, 70, 145); | ||
canvas.toBuffer(function(err, buf) { | ||
if (params.save) | ||
params.save(text); | ||
else if(req.session) | ||
req.session.captcha = text; | ||
res.end(buf); | ||
}); | ||
module.exports = function(params){ | ||
if (params.hasOwnProperty('text')) | ||
delete params.text; | ||
return function(req, res, next){ | ||
generateImage(req, res, params); | ||
}; | ||
}; | ||
module.exports.generateImage = generateImage; | ||
module.exports.generateCode = generateCode; |
{ | ||
"name": "kaptcha", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "simple 6 digit captcha image middleware for express, modified from captcha middleware.", | ||
"main": "kaptcha.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha ./test/test.js" | ||
}, | ||
@@ -25,3 +25,6 @@ "repository": { | ||
"canvas": "^1.2.9" | ||
}, | ||
"devDependencies": { | ||
"should": "^7.1.0" | ||
} | ||
} |
@@ -5,3 +5,3 @@ # kaptcha | ||
## Installation | ||
Canvas is required, please follow canvas instruction to install canvase dependencies. [Canvas@npmjs](https://www.npmjs.com/package/canvas) | ||
Canvas is required, please follow canvas instruction to install canvase dependencies. [Canvas Wiki](https://www.npmjs.com/package/canvas) | ||
@@ -14,23 +14,29 @@ Once canvas is installed successfully, run npm command to install kaptcha. | ||
## Usage | ||
Mount kaptcha middleware: | ||
As middleware (express-session middleware is required): | ||
``` | ||
var kaptcha = require('kaptcha'); | ||
app.use(kaptcha({ | ||
url: '/kaptcha.jpg', | ||
app.get('/kaptcha.png', kaptcha({ | ||
color: 'rgb(0, 0, 0)', | ||
background: 'rgb(255, 255, 255)', | ||
width: 100, | ||
height: 30, | ||
save: function(code) { | ||
console.log(code); | ||
} | ||
})); | ||
height: 30 | ||
})) | ||
app.post('/authenticate', function(req, res) { | ||
console.log(req.session.captcha == req.body.captcha); | ||
}) | ||
``` | ||
Customize: | ||
``` | ||
var kaptcha = require('kaptcha'); | ||
app.get('/captcha', function(req, res) { | ||
var code = kaptcha.generateCode(); | ||
kaptcha.generateImage(req, res, { width: 100, height: 30, text: code }); | ||
}) | ||
``` | ||
## Parameters | ||
### url | ||
url to render captcha image. | ||
### color | ||
@@ -48,3 +54,3 @@ font color | ||
### save | ||
Optional callback to receive generated random digit, or kaptcha will try to save it to req.session.captcha . | ||
### text | ||
Optional captcha random code for generateImage(), and will be ignored in kaptcha middleware function. |
Sorry, the diff of this file is not supported yet
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
16028
6
81
0
54
1