This article mainly introduces the node-ccap module to generate captchaVerification code. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.
Preparation work is as follows:
This library depends on python2.7.X and node-gyp, please install
1 and Python
first. After installation, add the installation directory such as "C:\Python27" to the system environment variable PATH.
2. node-gyp installation
Install node-gyp globally. Execute npm install -g node-gyp.
3. Install ccap
npm install ccap
Note here, do not upload the node_modules folder in the project directory, windows and Linux Not the same.
When using node for web development, you may encounter areas that require verification codes. I searched on github before and found that there are some class libraries such as node-captcha, which all need to rely on third-party graphics processing libraries or software. , like when I installed the graphics library cario before, it took a lot of effort, but in fact we only used a few small functions of these graphics libraries, such as modifying and cropping the image size, or generating verification codes.
Let’s first introduce CImg, the C++ graphics library. CImg is a cross-platform C++ image processing library that provides a series of functions such as loading, processing, display, and saving. The attractive part is that the entire graphics library only contains one CImg.h file, so it is very portable, green and environmentally friendly. It can be compiled and used wherever you take it, without having to install a lot of dependencies. So I wanted to use this CImg graphics library to make a simple demo, starting with the function of implementing the verification code. Of course, I can fully use this library to do other functions such as cropping pictures.
The ccap module is a package based on 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 libraries or software. In other words, if you just want to generate a simple verification code, just require the ccap module.
Generated image example:
1. Installation: General method: npm install ccap or download through github, address: https:// github.com/DoubleSpout/ccap
2. Performance: The verification code generation speed can reach 1200 times/second on a 2cpu Linux 64-bit server. The pictures generated in the test are BMP and jpeg pictures. The verification code generation speed is approximately 600 times/second.
3. Declaration method:
var ccap = require('ccap'); var captcha1 = ccap(); var captcha2 = ccap(width, height, offset); var captcha3 = ccap({ width:256,//set width,default is 256 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 } });
You can instantiate a ccap class through the above code. 1. Do not pass any parameters, use all default parameters to generate the verification code. 2. Pass only the width, height, and offset for instantiation, adjust the size of the image, and the spacing between the text in the image. 3. Pass an object. In addition to the width, The height and offset also pass the image quality and the method of generating random numbers. The ccap module will use the string returned by custom function as the content of the image verification code. The default is 0-9, and 6 for A-Z. Bit random string.
Theoretically, many different ccap instances can be produced, and they have no influence on each other, so even if a multi-process node is started through the cluster and the verification code is produced at the same time, there will be no mutual locking effect.
The picture quality is only valid for jpeg pictures. If no jpeg lib library is installed, you can only use bmp uncompressed graphics. The size is larger, but the generation speed is faster.
4. Usage method, get():
var ccap = require('ccap'); var captcha = ccap(); var ary = captcha.get();//ary[0] is captcha's text,ary[1] is captcha picture buffer. var text = ary[0]; var buffer = ary[1];
After instantiating the ccap class, you will get the captcha object. This object has only one external method, get (), each call of this method will return the verification code buffer and the corresponding text string content, which are stored in an array, similar to this structure:
["captcha text","picture buffer"]
5, a Simple web example:
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/');
The above example will return the verification code to the client, output the text of the corresponding verification code, and intercept favicon.ico request.
The result is as shown below:
If you are interested, you can download the TX and try it. If the jpeg library is installed, you can put the binding in the root directory. Overwrite jpeg.gyp and rename it to binding.gyp and then rebuild to use jpeg images as verification codes, and the size will be much smaller. In addition, the ccap module has some caching mechanisms to try to achieve better performance.
Currently ccap has supported jpeg verification code for Linux systems, and the size has been reduced from 45kb to 6kb.
The above is the detailed content of How does the node-ccap module generate captcha verification code?. For more information, please follow other related articles on the PHP Chinese website!