Many places where verification codes are needed when using node for web development are encountered. Previously, searched on github, there were some class libraries such as node-captcha, which all rely on third-party graphics processing libraries or software. For example, when I installed the graphics library cario, it was a lot of effort, but in fact we only used a little bit of the small functions of these graphics libraries, such as modifying and cropping the size of the picture, or producing verification codes.
Let’s first introduce the CImg C++ graphics library. CImg is a cross-platform C++ image processing library that provides a series of functions such as loading, processing, displaying, and saving. The most attractive thing is that the entire graphics library is only one CImg.h file, so it is very portable, green and environmentally friendly. It can be compiled and used anywhere you go without installing a big push dependency. So I wanted to use this CImg graphics library to make a simple demo, starting with the function of implementing the verification code, and of course I can completely use this library to do other functions such as cropping pictures.
The ccap module is based on the encapsulation of the CImg graphics library, allowing it to be used by node. Due to the portability of the CImg graphics library, the ccap module can work independently without relying on any other third-party graphics library or software. In other words, if you just want to generate a simple verification code, just require the ccap module.
1. Installation:
General method: npm install ccap
Or download it through github, address: https://github.com/DoubleSpout/ccap
Note: There may be errors during the installation process. Please install the corresponding dependency package according to the error prompt.
2. Performance:
The speed of generating verification codes on a 2cpu linux 64-bit server can reach 1200 times/sec. The image generated by the test is BMP. The jpeg image verification code generation speed is about 600 times/sec.
3. Declaration method:
The code copy is as follows:
var ccap = require('ccap');
var captcha1 = ccap();
var captcha2 = ccap(width, height, offset);
var captcha3 = ccap({
width:256,//set width,default is 256
A ccap class can be instantiated through the above code. 1. No parameters are passed, all the default parameters are used to generate verification codes. 2. Only pass width, height, and offset to instantiate, adjust the size of the picture, and the interval between the characters in the picture 3. Pass an object. In addition to width, height and offset, it also passes the image quality and the method of generating random numbers. The ccap module will use the string returned by the custom function as the content of the image verification code. The default is 0-9, and the 6-bit string of AZ will be followed.
In theory, many different ccap instances can be produced, and they have no influence on each other, so even if you start multi-process nodes through cluster and produce verification codes at the same time, there is no impact of locking each other.
The image quality is only valid for jpeg images. If no jpeg lib library is installed, you can only use bmp uncompressed graphics, which is relatively large in size, but the generation speed is relatively fast.
4. Use method, get():
The code copy is as follows:
height:60,//set height,default is 60
offset:40,//set text spacing,default is 40
quality:100,//set pic quality,default is 50
generate:function(){//Custom the function to generate captcha text
//generate captcha text here
return text;//return the captcha text
}
});
After instantiating the ccap class, you will get the captcha object. This object has only one external method, get(). Each time this method is called, it will return the verification code buffer and the corresponding text string content, and save it in an array, with a structure similar to this:
The code copy is as follows:
["captcha text","picture buffer"]
5. A simple web example:
The code copy is as follows:
var http = require('http');
var ccap = require('ccap')();//Instantiated ccap class
http.createServer(function (request, response) {
if(request.url == '/favicon.ico')return response.end('');//Intercept request favicon.ico
var ary = ccap.get();
var txt = ary[0];
var buf = ary[1];
response.end(buf);
console.log(txt);
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
Note: Some code parameters can be modified according to their own environment.