Node. js WEB development image verification code implementation method

  • 2020-03-30 03:12:07
  • OfStack

Made web development node might face many verification code required, before the search in the making, there are some node - captcha, etc of the libraries, for example, all need to rely on third-party graphics library or software, as I previously installed cario the graphics library, is really a great fight, but in fact, we only use these graphics library to a little function, such as the size of the picture change cutting, or production verification code.

First introduce CImg this c + + graphics library, CImg is a cross-platform c + + image processing library, provides a series of functions such as load, processing, display, save, the most attractive place is the graphics library is a CImg h this file, so it is portable green environmental protection, to where all can be used to compile, don't have to install a big push. So I want to use this CImg graphics library to do a simple demo, from the implementation of captcha this function, of course, can fully use this library to do clipping pictures and other functions.

Ccap module is based on the encapsulation of CImg graphics library, so that it can be used by node. Due to the portability of CImg graphics library, ccap module can work independently from any other third-party graphics library or software. That is to say, if you just want to generate a simple verification code, just require this ccap module.

1. Installation:
General method: NPM install ccap
Or by making the download address: https://github.com/DoubleSpout/ccap
Note: there may be errors during the installation process. Please follow the error to install the corresponding dependency package

2. Performance:

On a 2-cpu Linux 64-bit server, the captcha can be generated up to 1200 times per second, the images generated by the test are BMP, and the image captcha generated by jpeg is about 600 times per 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

You can instantiate a ccap class through the code above. 1, don't pass any parameters, all using the default parameters generated authentication code 2, only pass wide, high, offset instantiated, adjust the size of the picture, and the interval of text in the images, pass an object, in addition to the width, height and migration, also pass the image quality and the method of generating random Numbers, ccap module according to the custom functions return A string as the content of the image verification code, the default is 0-9, A to Z of the six strings.

Theoretically, it is possible to produce many different instances of ccap, and they have no influence on each other, so even if the node with multiple processes is opened through the cluster and the verification code is produced at the same time, there is no influence on mutual locking.

For image quality is only valid for jpeg images, if you do not install any jpeg lib library, you can only use BMP uncompressed graphics, large size, but faster generation.

4. Use method, get() :


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, which has only one external method, get(). This method will return the captcha buffer and the corresponding text string contents in an array every time it is called.

["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/');

Note: some code parameters can be modified according to their own environment


Related articles: