zoukankan      html  css  js  c++  java
  • PHP 图片验证码

    1. <?php   
    2. header("Pragma: no-cache");   
    3. header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");   
    4. session_start();   
    5. unset ($_SESSION['validate']);   
    6.   
    7. $strnum = 8; //产生密码的位数   
    8. $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'//显示的字符范围   
    9.   
    10. $font_file = 'Fregne Myriad v2.ttf';//指定字体文件   
    11.   
    12. $disturbpiont = true; //是否显示干扰素   
    13. $disturbpiontnum = 600; //干扰素的点的数目   
    14. $disturbline = true; //是否有干扰条   
    15. $disturblinenum = 8; //干扰条的数目   
    16.   
    17. $l = strlen($str);   
    18.   
    19. $randStr = array ();   
    20. $validate;   
    21. for ($i = 0; $i < $strnum; ++ $i) {   
    22.     $randStr[$i] = $str[rand(0, $l)];   
    23. }   
    24.   
    25. for ($i = 0; $i < $strnum; ++ $i) {   
    26.     if ($randStr[$i] == '')   
    27.         $randStr[$i] = '*';   
    28.     $validate = $validate . $randStr[$i];   
    29. }   
    30.   
    31.   
    32. function RgbToHsv($R$G$B) {   
    33.     $tmp = min($R$G);   
    34.     $min = min($tmp$B);   
    35.     $tmp = max($R$G);   
    36.     $max = max($tmp$B);   
    37.     $V = $max;   
    38.     $delta = $max - $min;   
    39.   
    40.     if ($max != 0)   
    41.         $S = $delta / $max// s   
    42.     else {   
    43.         $S = 0;   
    44.         return;   
    45.     }   
    46.     if ($R == $max)   
    47.         $H = ($G - $B) / $delta// between yellow & magenta   
    48.     else  
    49.         if ($G == $max)   
    50.             $H = 2 + ($B - $R) / $delta// between cyan & yellow   
    51.     else  
    52.         $H = 4 + ($R - $G) / $delta// between magenta & cyan   
    53.   
    54.     $H *= 60; // degrees   
    55.     if ($H < 0)   
    56.         $H += 360;   
    57.     return array (   
    58.         $H,   
    59.         $S,   
    60.         $V  
    61.     );   
    62. }   
    63.   
    64. function HsvToRgb($H$S$V) {   
    65.     if ($S == 0) {   
    66.         // achromatic (grey)   
    67.         $R = $G = $B = $V;   
    68.         return;   
    69.     }   
    70.   
    71.     $H /= 60; // sector 0 to 5   
    72.     $i = floor($H);   
    73.     $f = $H - $i// factorial part of h   
    74.     $p = $V * (1 - $S);   
    75.     $q = $V * (1 - $S * $f);   
    76.     $t = $V * (1 - $S * (1 - $f));   
    77.   
    78.     switch ($i) {   
    79.         case 0 :   
    80.             $R = $V;   
    81.             $G = $t;   
    82.             $B = $p;   
    83.             break;   
    84.         case 1 :   
    85.             $R = $q;   
    86.             $G = $V;   
    87.             $B = $p;   
    88.             break;   
    89.         case 2 :   
    90.             $R = $p;   
    91.             $G = $V;   
    92.             $B = $t;   
    93.             break;   
    94.         case 3 :   
    95.             $R = $p;   
    96.             $G = $q;   
    97.             $B = $V;   
    98.             break;   
    99.         case 4 :   
    100.             $R = $t;   
    101.             $G = $p;   
    102.             $B = $V;   
    103.             break;   
    104.         default : // case 5:   
    105.             $R = $V;   
    106.             $G = $p;   
    107.             $B = $q;   
    108.             break;   
    109.     }   
    110.     return array (   
    111.         $R,   
    112.         $G,   
    113.         $B  
    114.     );   
    115. }   
    116.   
    117. $size = 30;   
    118. $width = $size * $strnum +10;   
    119. $height = $size +10;   
    120. $degrees = array ();   
    121. for ($i = 0; $i < $strnum; ++ $i) {   
    122.     $degrees[$i] = rand(0, 35);   
    123. // 生成数字旋转角度   
    124.   
    125. for ($i = 0; $i < $strnum; ++ $i) {   
    126.     if (rand() % 2);   
    127.     else  
    128.         $degrees[$i] = - $degrees[$i];   
    129. }   
    130.   
    131. $image = imagecreatetruecolor($size$size); // 数字图片画布   
    132. $validatepic = imagecreatetruecolor($width$height); // 最终验证码画布   
    133. $back = imagecolorallocate($image, 255, 255, 255); // 背景色   
    134. $border = imagecolorallocate($image, 0, 0, 0); // 边框   
    135. imagefilledrectangle($validatepic, 0, 0, $width$height$back); // 画出背景色   
    136.   
    137. // 数字颜色   
    138. for ($i = 0; $i < $strnum; ++ $i) {   
    139.     // 考虑为使字符容易看清使用颜色较暗的颜色   
    140.     $temp = RgbToHsv(rand(0, 255), rand(0, 255), rand(0, 255));   
    141.   
    142.     if ($temp[2] > 60)   
    143.         $temp[2] = 60;   
    144.   
    145.     $temp = HsvToRgb($temp[0], $temp[1], $temp[2]);   
    146.     $textcolor[$i] = imagecolorallocate($image$temp[0], $temp[1], $temp[2]);   
    147. }   
    148.   
    149. for ($i = 0; $i < $disturbpiontnum && $disturbpiont; ++ $i//加入干扰象素   
    150.     {   
    151.     $randpixelcolor = ImageColorallocate($validatepic, rand(0, 255), rand(0, 255), rand(0, 255));   
    152.     imagesetpixel($validatepic, rand(1, $width -1), rand(1, $height -1), $randpixelcolor);   
    153. }   
    154.   
    155. // 干扰线使用颜色较明亮的颜色   
    156. $temp = RgbToHsv(rand(0, 255), rand(0, 255), rand(0, 255));   
    157.   
    158. if ($temp[2] < 200)   
    159.     $temp[2] = 255;   
    160.   
    161. $temp = HsvToRgb($temp[0], $temp[1], $temp[2]);   
    162. $randlinecolor = imagecolorallocate($image$temp[0], $temp[1], $temp[2]);   
    163.   
    164. // 画干扰线   
    165. for ($i = 0; $i < $disturblinenum && $disturbline$i++)   
    166.     imageline($validatepic, rand(1, 239), rand(1, 39), rand(1, 239), rand(1, 39), $randpixelcolor);   
    167.   
    168. for ($i = 0; $i < $strnum; ++ $i) {   
    169.     $image = imagecreatetruecolor($size$size); // 刷新画板   
    170.     imagefilledrectangle($image, 0, 0, $size$size$back); // 画出背景色    
    171.     imagefttext($image, 13, 0, 5, 20, $textcolor[$i], $font_file$randStr[$i]);   
    172.     $image = imagerotate($image$degrees[$i], $back);   
    173.     imagecolortransparent($image$back);   
    174.     imagecopymerge($validatepic$image, 5 + 30 * $i, 5, 0, 0, imagesx($image), imagesy($image), 100);   
    175. }   
    176. imagerectangle($validatepic, 0, 0, $width -1, $height -1, $border); // 画出边框   
    177.   
    178. header('Content-type: image/png');   
    179. imagepng($validatepic);   
    180. imagedestroy($validatepic);   
    181. imagedestroy($image);   
    182. $_SESSION['code'] = $validate;//将验证码存入session,如果实际应用,请md5.   
    183. ?>  

     字体文件和该文件放在同一目录下即可,如果不是请设置

    Php代码 复制代码
    1. $font_file  

    需要设置背景颜色请修改

    Php代码 复制代码
    1. $back = imagecolorallocate($image, 255, 255, 255); // 背景色  

    显示图框的大小会随着设置显示的字符数变化。使用起来还是比较方便的。

  • 相关阅读:
    【问题:SSH】win10使用SSH链接服务器时,提示:Host key verification failed
    【深度学习大讲堂】首期第三讲:深度学习基础 第一部分:基础结构单元
    【深度学习大讲堂】首期第二讲:深度学习简史
    【深度学习大讲堂】首期第一讲:人工智能的ABCDE 第二部分:简谈当前AI技术与发展趋势
    【深度学习大讲堂】首期第一讲:人工智能的ABCDE 第一部分:从人工智能和计算机视觉说起
    实验吧-隐写-so beautiful so white WP
    实验吧-隐写 最低位的亲吻 WP
    jarvis OJ basic (一) WP
    实验吧web-登入一下好吗
    实验吧web-oncemore
  • 原文地址:https://www.cnblogs.com/bicabo/p/1880679.html
Copyright © 2011-2022 走看看