dongxiezhuo8852 2013-10-27 21:33
浏览 44
已采纳

PHP中的验证码验证

I used the following code to create Captcha to my form. Captcha creating well. Now I want to change font-size, and Font character spaces. I don't know how to change in the following code.

      <?php
             session_start();
             $code= substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, 6);
             $_SESSION["code"]=$code;
             $im = imagecreatetruecolor(150, 35);
             $bg = imagecolorallocate($im, 255, 255, 255);
             $fg = imagecolorallocate($im, 0, 0, 0);
             imagefill($im, 5, 5, $bg);
             imagestring($im, 5, 8, 8,  $code, $fg);
             header("Cache-Control: no-cache, must-revalidate");
             header('Content-type: image/png');
             imagepng($im);
             imagedestroy($im);
        ?>
  • 写回答

2条回答 默认 最新

  • dsj1961061 2013-10-27 22:09
    关注

    You can use the code below to implement a simple captcha using gd library of PHP. As you are beginner, here is a sample code for quick testing, it covers font-sizing also:

    <?php
    session_start();
    header('Content-type: image/jpeg');
    
    $text = rand(1000, 9999);
    $font_size = 30;
    
    $image_width = 200;
    $image_height = 40;
    
    $image = imagecreate($image_width, $image_height);
    imagecolorallocate($image, 255, 255, 255);
    $text_color = imagecolorallocate($image, 0, 0, 0);
    
    for ($x=1; $x<=40; $x++) {
         $x1 = rand(1, 100);
         $y1 = rand(1, 100);
         $x2 = rand(1, 100);
         $y2 = rand(1, 100);
    
     imageline($image, $x1, $y1, $x2, $y2, $text_color);
    }
    
    imagettftext($image, $font_size, 0, 15, 30, $text_color, 'FREESCPT.ttf', $text);
    imagejpeg($image);
    
    ?>
    

    In imagettftext function:

    imagettftext($image, $font_size, $angle, $x, $y, $text_color, '$font-family', $text);
    
      $image is the $imagecreate function
      $font_size is the size of font you want.
      $angle is the angle of the fonts tilted
      $x and $y are coordinates.
      $text_color is the imagecolorallocate function
      $font-family is the family of font you want to use
      $text is the text or random text to be displayed
    

    Here is the good tutorial on how to build captcha in php -> link

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部