dongxiezhuo8852 2013-10-28 05: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-28 06: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条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题