The realization method of picture verification code in Node.js Web development _node.js

Source: Internet
Author: User
Tags image processing library require

Use node to do a lot of web development can encounter the need to verify the code of the place, before the search on the GitHub, there are some such as Node-captcha class library, all need to rely on Third-party graphics processing library or software, as I installed Cario This graphics library, really cost a great deal of effort, But in fact we only use a little bit of these graphics library features, such as the size of the image to modify the crop, or production verification code.

First introduce cimg this C + + graphics library, cimg is a cross-platform C + + image processing library, providing loading, processing, display, save a series of functions, The most attractive place is the entire graphics library on a CImg.h this file, so very portable green environmental protection, where can be compiled to use, do not install a big push dependency. So I want to use this CIMG graphics library to do a simple demo, from the implementation of the function of verification code, of course, can completely use this library to do other functions such as cropping pictures.

CCAP module is based on the Cimg graphics library encapsulation, so that it can be used for node, because the Cimg graphics library portability, so the CCAP module can not rely on any other Third-party graphics library or software to work independently, also said if just want to generate a simple authentication code, Just require this CCAP module.

1. Installation:
General method: NPM Install Ccap
or download via GitHub, Address: Https://github.com/DoubleSpout/ccap
Note: Errors may occur during the installation process, please install the corresponding dependency pack according to the error prompts

2, Performance:

On 2CPU Linux 64-bit server generated verification code speed can reach 1200 times/sec, the test generated images are BMP, JPEG image verification code generation speed of approximately 600 times/sec

3, the Declaration method:

Copy Code code 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


You can instantiate a CCAP class by using the code above. 1, do not pass any parameters, all use the default parameters to generate authentication code 2, only pass the width, high, offset for instantiation, resize the picture, and the text in the picture interval 3, pass an object, in addition to wide, high and offset, but also passed the picture quality and generate random number of methods, The CCAP module will be the content of the picture verification code according to the string of the custom function return, by default the 0-9,a-z 6-bit string.

It is theoretically possible to produce many different instances of CCAP, and they have no effect on each other, so there is no interlocking effect on the production of verification codes even though the cluster node is opened by multiple processes.

For image quality is only valid for JPEG pictures, if you do not install any JPEG Lib library, you can only use BMP uncompressed graphics, the volume is relatively large, but the production speed is relatively fast.

4, use method, get ():

Copy Code code 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, it gets the Captcha object, which has only one external method, get (), which returns the verification code buffer and the corresponding text string content in each call, stored in the array, similar to the structure:
Copy Code code as follows:

["Captcha text", "Picture buffer"]

5, a simple Web example:
Copy Code code 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.