(Advanced article) PHP generated CAPTCHA code program

Source: Internet
Author: User
The following text:

Create a new PHP file captcha_code_file.php

First open Sessionsession_start ();//define the foreground display verification code length & Width $image_width = +; $image _height = +; $characters _on_image = 6; $font = './monofont.ttf ';//the characters that can is used in the CAPTCHA code.//avoid confusing characters (l 1 and I for example ) $possible _letters = ' 23456789bcdfghjkmnpqrstvwxyz '; $random _dots = ten; $random _lines = $captcha _text_color= " 0x142864 "; $captcha _noice_color =" 0x142864 ";//define the string to generate the verification code $code = '; $i = 0;while ($i < $characters _on_image) {$code. = substr ($possible _letters, Mt_rand (0, strlen ($possible _letters)-1), 1); $i + +;} $font _size = $image _height * 0.75; $image = @imagecreate ($image _width, $image _height);/* Setting the background, text and n Oise colours here */$background _color = imagecolorallocate ($image, 255, 255, 255); $arr _text_color = Hexrgb ($captcha _text _color); $text _color = Imagecolorallocate ($image, $arr _text_color[' Red '], $arr _text_color[' green ', $arr _text_color [' Blue ']); $arr _noice_color = Hexrgb ($captcha _noice_color); $image _noise_color = ImagecoLorallocate ($image, $arr _noice_color[' red '), $arr _noice_color[' green '], $arr _noice_color[' Blue ');/* Generating th e dots randomly in background */for ($i =0; $i < $random _dots; $i + +) {imagefilledellipse ($image, Mt_rand (0, $image _width) , Mt_rand (0, $image _height), 2, 3, $image _noise_color);} /* generating lines randomly in background of image */for ($i =0; $i < $random _lines; $i + +) {imageline ($image, Mt_rand (0, $image _width), Mt_rand (0, $image _height), Mt_rand (0, $image _width), Mt_rand (0, $image _height), $image _noise_color);} /* Create a text box and add 6 letters code in IT */$textbox = Imagettfbbox ($font _size, 0, $font, $code); $x = ($image _width-$textbox [4])/2; $y = ($image _height-$textbox [5])/2;imagettftext ($image, $font _size, 0, $x, $y, $text _color, $font, $code);/* Show captcha image in the page HTML page */header (' content-type:image/jpeg ');//Defining the IM Age type to being shown in browser widowimagejpeg ($image);//showing the Imageimagedestroy ($image);//destroying the image instance//set SESSION, do verify $_session[' 6_letters_code '] = $code; function Hexrgb ($hexstr) {$int = Hexdec ($HEXSTR);               Return Array ("Red" = 0xFF & ($int >> 0x10), "green" + 0xFF & ($int >> 0x8), "Blue" = 0xFF & $int);}

The above is (Advanced article) PHP code to generate verification code content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.