zoukankan      html  css  js  c++  java
  • php简单生成验证码图片

    <?php 
    $i=imagecreatetruecolor(100, 30);
    $iColor=imagecolorallocate($i, 240, 240, 240);
    imagefill($i, 0, 0, $iColor);
    $str="1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPSDFGHJKLZXCVBNM";
    $nStr=str_shuffle($str);
    $fourStr=substr($nStr, 0,4);
    
    for ($j=0; $j < strlen($fourStr); $j++) { 
    
        $s=substr($fourStr, $j,1);
        //随机颜色,尽量不要太浅(和底色白色有差别)
        $fColor=imagecolorallocate($i, mt_rand(0,150), mt_rand(0,150), mt_rand(0,150));
        //随机倾斜
        $angle=mt_rand(0,45);
    
        $x=8+$j*25;
        $y=25;
        //SIMYOU.TTF字体文件可以在C:WINDOWSFonts下找到,多个字体也可以添加到一个数组中,然后随机提取
        imagefttext($i, 20, $angle, $x, $y, $fColor, "SIMYOU.TTF", $s);
    }
    
    //添加干扰线 (4条干扰线)
    for ($j=0; $j < 4; $j++) { 
        $lColor=imagecolorallocate($i, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
        $x1=mt_rand(0,100);
        $y1=mt_rand(0,30);
        $x2=mt_rand(0,100);
        $y2=mt_rand(0,30);
    
        imageline($i, $x1, $y1, $x2, $y2, $lColor);
    }
    
    
    //加干扰点(200个干扰点)
    for ($j=0; $j < 200; $j++) { 
        $pColor=imagecolorallocate($i, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
        $x=mt_rand(0,100);
        $y=mt_rand(0,30);
        imagesetpixel($i, $x, $y, $pColor);
    }
    header("content-type:image/png");
    imagepng($i);
    
    ?>
  • 相关阅读:
    [LeetCode] 735. Asteroid Collision
    [LeetCode] 14. Longest Common Prefix
    [LeetCode] 289. Game of Life
    [LeetCode] 73. Set Matrix Zeroes
    [LeetCode] 59. Spiral Matrix II
    [LeetCode] 54. Spiral Matrix
    [LeetCode] 48. Rotate Image
    [LeetCode] 134. Gas Station
    [LeetCode] 70. Climbing Stairs
    [LeetCode] 71. Simplify Path
  • 原文地址:https://www.cnblogs.com/xuxyblog/p/4233413.html
Copyright © 2011-2022 走看看