Example of cloning ecshop Captcha in PHP

Source: Internet
Author: User
Tags imagejpeg
This article mainly introduces the PHP imitation Ecshop Verification code example, very good, with reference value, the need for friends can refer to the next

The code for the Clone Ecshop Verification code is as follows:

<?php//Imitation Ecshop Verification code (four-bit capital letters and numbers, backgrounds)//processing code values (four-bit uppercase letters and numbers)//all possible character sets $chars = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 '; $chars _len = strlen ($chars); Set length//random selection $code_len = 4;//Verification Code length $code= ";  Verification Code value initialization for ($i =0; $i < $code _len;++ $i) {//randomly get a character subscript $rand _index = Mt_rand (0, $chars _len-1); Use the subscript of the string to get the selected character $code. = $chars [$rand _index];} echo $code;//stored in session (for verification) session_start (); $_session[' code ' = $code;//CAPTCHA image (known background picture)//Handle background $bg_file= '. Captcha/captcha_bg '. Mt_rand (1,5). '. jpg ';//based on the picture, create a canvas $image = Imagecreatefromjpeg ($BG _file);//simple function to write a string on the canvas (Imagestring ();)//imagestring (canvas, Font, Position x, position y, string content, color); Font: imagestring function, using the built-in font. represented by 1-5. The position is determined by the coordinates of the upper-left corner of the string. Colors also need to be pre-allocated. Imagecolorallocate ();//Assign font color (randomly assigned black or white) if (Mt_rand (0,1) ==1) {$str _color = imagecolorallocate ($image, 0,0,0);//Black} else{$str _color = imagecolorallocate ($image, 255,0xff,255);//white}//built-in 5th number font $font = 5;//position//canvas Size $image_w = Imagesx ($image ); $image _h = Imagesy ($image);//Get font width and height $font_w = imagefontwidth ($font); $font _h = ImagefonTheight ($font);//Gets the width of the string $str_w = $font _w * $code _len; $str _h = $font _h;//calculate position $str_x = ($image _w-$str _w)/2; $str _y = ($ima ge_h-$str _h)/2;//string imagestring ($image, $font, $str _x, $str _y, $code, $str _color);//output and destroy the canvas header ("Content-type: Image/jpeg "); imagejpeg ($image); Imagedestroy ($image);

Encapsulation Captcha Tool Class:

CAPTCHA Tool Class (all packages related to Authenticode operations are encapsulated into the Class) class captcha{/* Generate Verification code */Public Function makeimage ($code _len=4) {//clone Ecshop validation    Code (four-bit uppercase letters and numbers, backgrounds)//processing code values (four-bit uppercase letters and numbers)//all possible character sets $chars = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 '; $chars _len = strlen ($chars); Set length//random selection $code = ';      Verification Code value initialization for ($i =0; $i < $code _len;++ $i) {//randomly get a character subscript $rand _index = Mt_rand (0, $chars _len-1);    Use the subscript of the string to get the selected character $code. = $chars [$rand _index];    }//echo $code;    stored in session (for validation) @session_start ();    $_session[' Code ' = $code; Captcha image (known background picture)//processing background $BG _file= TOOL. './captcha/captcha_bg '. Mt_rand (1,5).    '. jpg ';    Based on the picture, create a canvas $image = Imagecreatefromjpeg ($bg _file);     A simple function that writes a string on the canvas (Imagestring ();)//imagestring (canvas, font, position x, position y, string content, color); Font: imagestring function, using the built-in font. represented by 1-5. The position is determined by the coordinates of the upper-left corner of the string. Colors also need to be pre-allocated.    Imagecolorallocate ();   Assign font color (randomly assigned black or white) if (Mt_rand (0,1) ==1) {$str _color = imagecolorallocate ($image, 0,0,0);//Black}else{     $str _color = imagecolorallocate ($image, 255,0xff,255),//white}//Built-in 5th font $font = 5;    Location//Canvas size $image _w = Imagesx ($image);    $image _h = Imagesy ($image);    Get the width and height of the font $font _w = imagefontwidth ($font);    $font _h = Imagefontheight ($font);    Gets the width of the string $str _w = $font _w * $code _len;    $str _h = $font _h;    Calculated position $str _x = ($image _w-$str _w)/2;    $str _y = ($image _h-$str _h)/2;    String imagestring ($image, $font, $str _x, $str _y, $code, $str _color);    Output and destroy the canvas header ("Content-type:image/jpeg");    Imagejpeg ($image);  Imagedestroy ($image); }}

The above is the whole content of this article, I hope that everyone's study has helped.


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.