Implementation of image verification codes in node. js WEB development

Source: Internet
Author: User
Tags image processing library

Many of the scenarios where node is used for web development may encounter verification codes. Previously, I searched on github and found some class libraries such as node-captcha, they all need to rely on third-party graphics processing libraries or software. When I installed the cario graphics library, it was a great deal of effort, but we actually only used a little small feature of these graphics libraries, you can modify the cropping or production verification code for the slice size.

Let's first introduce CImg's c ++ graphics library. CImg is a cross-platform C ++ image processing library that provides loading, processing, display, and storage functions, the most attractive part is that the entire image library has a CImg. h this file, so it is very portable and green, where it can be compiled and used, without installing a large push dependency. So I want to use this CImg graphics library for a simple demo, starting with the implementation of the verification code function, of course, you can use this library to crop images and other features.

The ccap module is encapsulated based on the CImg graphics library so that it can be used by node. Due to the portability of the CImg graphics library, the ccap module can work independently without relying on any third-party graphics library or software, that is to say, if you only want to generate a simple verification code, you only need to require the ccap module.

1. installation:
General method: npm install ccap
Or download it through github at: https://github.com/DoubleSpout/ccap
Note: An error may occur during the installation process. Install the corresponding dependency package according to the error prompt.

2. Performance:

On a 64-bit linux server with 2 CPUs, the verification code generation speed can reach 1200 times/Second, the generated image is BMP, And the jpeg image verification code generation speed is about 600 times/second.

3. Declaration method:
Copy codeThe Code 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
You can use the above Code to instantiate a ccap class. 1. If no parameters are passed, use the default parameters to generate the verification code. 2. pass only the width, height, and offset to instantiate the verification code and adjust the image size, and text in the image. 3. transfer an object. In addition to width, height, and offset, the image quality and random number generation methods are also transmitted, the ccap module uses the return string of the custom function as the content of the image verification code. The default value is 0-9, and the 6-digit A-Z is then the string.

In theory, many different ccap instances can be produced, and they have no influence on each other. Therefore, even if a multi-process node is enabled through the cluster, generating verification codes at the same time does not affect mutual locks.

The image quality is only valid for jpeg images. If you have not installed any jpeg lib library, you can only use bmp uncompressed images. The size is relatively large, but the generation speed is relatively high.

4. usage, get ():
Copy codeThe Code 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 the ccap class is instantiated, the captcha object is obtained. This object has only one external method, get (). Each call to this method will return the verification code buffer and the corresponding text string content, save it in an array, with a structure similar to this:
Copy codeThe Code is as follows:
["Captcha text", "picture buffer"]
5. A simple web example:
Copy codeThe Code 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 your own environment.

Related Article

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.