PHP implementation of color-changing verification code example _php skills

Source: Internet
Author: User
Tags rand
Copy Code code as follows:

<?php
Header ("content-type:image/png,charset= ' Utf-8 '");
$im = Imagecreatetruecolor (400, 30);
White
$white = Imagecolorallocate ($im, 255, 255, 255);
Red
$red = Imagecolorallocate ($im, 255, 0, 0);
Black
$black =imagecolorallocate ($im, 0, 0, 0);
Green
$green =imagecolorallocate ($im, 0, 255, 0);
Blue
$blue =imagecolorallocate ($im, 0, 0, 255);
$color _arr=array ($green, $blue, $red);
$color =array_rand ($color _arr);
$text = ' I am so sick of this verification code ';
$textlen =iconv_strlen ($text, ' utf-8 ');//Calculate string length
Randomly intercept two characters, color display
$p 1=rand (1, $textlen)-1;
while (($p 2=rand (1, $textlen)-1) = = $p 1);
$w 1=iconv_substr ($text, $p 1,1, ' utf-8 ');
$w 2=iconv_substr ($text, $p 1,1, ' utf-8 ');
Font files (ps:t good PHP Q-buckle 峮: 276167802, Validation: CSL)
$font = ' Simkai.ttf ';
Imagefilledrectangle ($im, 0, 0, 399, $white);
for ($i =0; $i < $textlen; $i + +)
{
if ($i = = $p 1| | $i = = $p 2)
{
Imagettftext ($im, 0, 20* ($i-1) +20, $color _arr[$color], $font, Iconv_substr ($text, $i, 1, ' utf-8 '));
}
Else
{
Imagettftext ($im, 0, 20* ($i-1) +20, $black, $font, Iconv_substr ($text, $i, 1, ' utf-8 '));
}
}
Imagepng ($im);
Imagedestroy ($im);
?>

The characters in the verification code are not the same color, allowing the user to enter the validation code for the specified color, which is more secure.

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.