How to generate distorted and rotated captcha images with PHP _php tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
function Make_rand ($length = "32") {//Captcha text generation functions
$str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
$result = "";
for ($i =0; $i < $length; $i + +) {
$num [$i]=rand (0,61);
$result. = $str [$num [$i]];
}
return $result;
}
$checkcode = Make_rand (5);
$im _x=160;
$im _y=32;
function Make_crand ($length = "5") {
$string = ";
for ($i =0; $i < $length; $i + +) {
$string. = Chr (rand (0XB0,0XF7)). Chr (rand (0XA1,0XFE));
}
return $string;
}
function Getauthimage ($text, $im _x =, $im _y = 32) {
$im = Imagecreatetruecolor ($im _x, $im _y);
$text _c = imagecolorallocate ($im, Mt_rand (0,100), Mt_rand (0,100), Mt_rand (0,100));
$tmpC 0=mt_rand (100,255);
$tmpC 1=mt_rand (100,255);
$tmpC 2=mt_rand (100,255);
$buttum _c = imagecolorallocate ($im, $tmpC 0, $tmpC 1, $tmpC 2);
Imagefill ($im, $buttum _c);
Echo $text;
$font = ' C://WINDOWS//FONTS//SIMSUN.TTC ';
echo strlen ($text);
$text =iconv ("gb2312", "UTF-8", $text);
Echo Mb_strlen ($text, "UTF-8");
for ($i =0; $i {
$tmp =mb_substr ($text, $i, 1, "UTF-8");
$array = Array ( -1,0,1);
$p = Array_rand ($array);
$an = $array [$p]*mt_rand (1,9);//Angle
$size = 20;
Imagettftext ($im, $size, $an, + + $i * $size *2,25, $text _c, $font, $tmp);
}
$distortion _im = Imagecreatetruecolor ($im _x, $im _y);
Imagefill ($distortion _im, $buttum _c);
for ($i =0; $i < $im _x; $i + +) {
for ($j =0; $j < $im _y; $j + +) {
$rgb = Imagecolorat ($im, $i, $j);
if ((int) ($i +20+sin ($j/$im _y*2*m_pi) *10) <= imagesx ($distortion _im) && (int) ($i +20+sin ($j/$im _y*2*m_pi) * ) >=0) {
Imagesetpixel ($distortion _im, (int) ($i +10+sin ($j/$im _y*2*m_pi-m_pi*0.5), $j, $rgb);
}
}
}
adding interfering pixels;
$count = number of 600;//interfering pixels
for ($i =0; $i < $count; $i + +) {
$randcolor = Imagecolorallocate ($distortion _im,mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255));
Imagesetpixel ($distortion _im, Mt_rand ()% $im _x, Mt_rand ()% $im _y, $randcolor);
}
$line _c=5;
Imageline
for ($i =0; $i < $line _c; $i + +) {
$linecolor = Imagecolorallocate ($distortion _im, 17, 158, 20);
$lefty = Mt_rand (1, $im _x-1);
$righty = Mt_rand (1, $im _y-1);
Imageline ($distortion _im, 0, $lefty, Imagesx ($distortion _im), $righty, $linecolor);
}
Header ("Content-type:image/png");
Export the image to a browser or file in PNG format;
Imagepng ($im);
Imagepng ($distortion _im);
Destroys an image, releasing the memory associated with image;
Imagedestroy ($distortion _im);
Imagedestroy ($im);
}
?>

The usage examples are as follows:
Copy CodeThe code is as follows:
Getauthimage (Make_crand (5));
?>

http://www.bkjia.com/PHPjc/327501.html www.bkjia.com true http://www.bkjia.com/PHPjc/327501.html techarticle Copy the code as follows:? PHP function Make_rand ($length = "32") {//Captcha literal generation function $str = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "; $result = ""; Fo ...

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