The production of image verification code in PHP 5

Source: Internet
Author: User
Tags imagejpeg

First, the application scenario

In Web sites, picture verification codes are often used to prevent malicious users from registering, posting, and so on. In PHP, the image verification code is mainly through the GD library provided by the API to complete.

Ii. Methods of realization

Verification codes are generally random numbers and combinations of letters, which can be dechex by a random function and a hexadecimal function. The most critical question is how to create a picture.

<?php//generate random number-"Create picture-" random number in the picture-"output to Browser for ($i =0; $i <4; $i + +) {$rand. =  Dechex (rand (1,15));} $im = Imagecreatetruecolor (100,30);//Create a true color image x is width, y is high//Set color//For an image to assign a color (equivalent to PhotoShop's palette)//Imagecolorallocate (R esource image, int red, int green, int blue) returns an identifier that represents a color made up of a given RGB component. $BG = Imagecolorallocate ($im, 0,0,0);//The first call to Imagecolorallocate () fills the background color of the color palette-based image. Represents a color consisting of a given RGB component $te = Imagecolorallocate ($im, 225,225,225);//write the string in the upper left corner of the image//Plot function imagestring (resource image, font, in t x, int y, string s, int col)//Use Col color to draw the string s to the x, y coordinates of the image represented by image (this is the upper-left coordinate of the string, and the upper-left corner of the entire image is 0,0).  If the font is 1,2,3,4 or 5, the built-in font is used. Imagestring ($im, 5,rand (3,70), Rand (0,16), $rand, $te);//Direct output image to the browser header ("Content-type:image/jpeg");//Imagejpeg ( Resource image [, string filename [, int quality]]) The filename parameter is optional and if omitted, the original image stream will be output directly. To omit the filename parameter and provide the quality parameter, use an empty string ('). Sending Content-type:image/jpeg through the header () allows the PHP script to output JPEG images directly. Imagejpeg ($im);? 

Third, the production of complex picture verification code

Sometimes the captcha must be set up in order to prevent it from being recognized by some image recognition tools. For example, randomly add some lines or dots to the image. This is done primarily through the imageline () and Imagesetpixel () functions. Even make Chinese verification code.

<?phpfor ($i =0; $i <4; $i + +) {$rand. = Dechex (rand (1,15));}  $im = Imagecreatetruecolor (100,30), $BG = Imagecolorallocate ($im, 0,0,0); $te = Imagecolorallocate ($im, 225,225,225);//Draw line for ($i =0; $i <3; $i + +) {$te 2 = imagecolorallocate ($im, Rand (0,225), Rand (0,225), Rand (0,225)),//imageline (resource image, int x1, int y1, int x2, int y2, int color) use color color in image images from   Coordinates x1,y1 to X2,y2 (the upper left corner of the image is 0, 0) to draw a line segment. Imageline ($im, Rand (0,100), 0,rand (0,100), Rand (0,30), $te 2);} Draw a dot for ($i = 0; $i < + $i + +) {//imagesetpixel (resource image, int x, int y, int color) in image with color in X,   A point is drawn on the y-coordinate (the upper-left corner of the image is 0,0).  Imagesetpixel ($im, Rand ()%100,rand ()%30, $te 2);} $str = Iconv ("GBK", "UTF-8", "Verification Code"),//Convert GBK encoding to UTF-8, if the file encoding is UTF-8, this step is not required. Because our system (Windows) comes with fonts that are identified by default in the International Universal Encoding (UTF-8) mode, it may not be displayed properly if you do not encode the conversion. Imagettftext ($im, 12,rand (0,10), 20,20, $te, ' Msyh.ttf ', $str);//rand (3,10) tilt. Msyh.ttf is a Microsoft Jas font that can be found in C:\Windows\Fonts (Windows XP, Windows 7). Then copy the directory to the file. It is possible to display Chinese in Windows because the system has the Chinese font header ("Content-type:image/jpeg "); imagejpeg ($im);? >
  • 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.