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条)

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来