向验证码图像添加额外的噪音?

发布于 2024-09-18 06:01:59 字数 1507 浏览 3 评论 0原文

我从网上得到了这个简单的验证码脚本。我喜欢它,并且我对其进行了编辑以适合我的网站,但想知道是否有人可以对其进行编辑以在验证码本身上添加一些随机行,因为它有点太简单了。

我确实找到了有关如何执行此操作的教程,但它对我不起作用。

这是简单的验证码脚本;我很想修改它,让验证码上出现一些随机行:

   $width    = 150;
   $height   = 24;
   $length   = 5;
   $font     = 'caviardreams.ttf';
   $font_size   = 14;
   $bg_color = array(245, 245, 245);
   $chars    = 'ABCDEFGHKMNPQRSTUVWXYZ23456789';
   session_start();
   //putenv('GDFONTPATH=' . realpath('.'));
   $img = imagecreatetruecolor($width, $height);
   $bkgr = imagecolorallocate($img, $bg_color[0], $bg_color[1], $bg_color[2]);
   imagefilledrectangle($img, 0, 0, $width, $height, $bkgr);

   $code = '';
   for($i = 0; $i < $length; $i++)
   {
      $code .= $chr = $chars[mt_rand(0, strlen($chars)-1)];
      $r = rand(0, 192);
      $g = rand(0, 192);
      $b = rand(0, 192);
      $color = imagecolorallocate($img, $r, $g, $b);
      $rotation = rand(-35, 35);
      $x = 5+$i*(4/3*$font_size+2);
      $y = rand(4/3*$font_size, $height-(4/3*$font_size)/2);
      imagettftext($img, $font_size, $rotation, $x, $y, $color, $font, $chr);
   }

   $_SESSION['random_txt'] = md5($code);

   header("Content-type: image/png");
   header("Expires: Mon, 01 Jul 1998 05:00:00 GMT");
   header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
   header("Cache-Control: no-store, no-cache, must-revalidate");
   header("Cache-Control: post-check=0, pre-check=0", false);
   header("Pragma: no-cache");

   imagepng($img);
   imagedestroy($img);

I have this simple captcha script from online. I like it, and I edited it to suit my site, but was wondering if someone could edit it to add some random lines on the captcha itself as it is somewhat too simple.

I did find tutorials on how to do it but it was not working for me.

Here is the simple captcha script; I would love to modify it to have some random lines appear on the captcha:

   $width    = 150;
   $height   = 24;
   $length   = 5;
   $font     = 'caviardreams.ttf';
   $font_size   = 14;
   $bg_color = array(245, 245, 245);
   $chars    = 'ABCDEFGHKMNPQRSTUVWXYZ23456789';
   session_start();
   //putenv('GDFONTPATH=' . realpath('.'));
   $img = imagecreatetruecolor($width, $height);
   $bkgr = imagecolorallocate($img, $bg_color[0], $bg_color[1], $bg_color[2]);
   imagefilledrectangle($img, 0, 0, $width, $height, $bkgr);

   $code = '';
   for($i = 0; $i < $length; $i++)
   {
      $code .= $chr = $chars[mt_rand(0, strlen($chars)-1)];
      $r = rand(0, 192);
      $g = rand(0, 192);
      $b = rand(0, 192);
      $color = imagecolorallocate($img, $r, $g, $b);
      $rotation = rand(-35, 35);
      $x = 5+$i*(4/3*$font_size+2);
      $y = rand(4/3*$font_size, $height-(4/3*$font_size)/2);
      imagettftext($img, $font_size, $rotation, $x, $y, $color, $font, $chr);
   }

   $_SESSION['random_txt'] = md5($code);

   header("Content-type: image/png");
   header("Expires: Mon, 01 Jul 1998 05:00:00 GMT");
   header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
   header("Cache-Control: no-store, no-cache, must-revalidate");
   header("Cache-Control: post-check=0, pre-check=0", false);
   header("Pragma: no-cache");

   imagepng($img);
   imagedestroy($img);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜无邪 2024-09-25 06:01:59

就在:之前

$_SESSION['random_txt'] = md5($code);

插入:

for ($i=0;$i<100;$i++)
  imageline($img,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),imagecolorallocate($img,rand(0,63),rand(0,63),rand(0,63)));

Right before:

$_SESSION['random_txt'] = md5($code);

Insert:

for ($i=0;$i<100;$i++)
  imageline($img,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),imagecolorallocate($img,rand(0,63),rand(0,63),rand(0,63)));
海拔太高太耀眼 2024-09-25 06:01:59

鉴于您想坚持使用该脚本,您可以查看

/* generate random lines in background */
/* generate random dots in background */

这两个项目似乎都符合您想做的事情。

Given that you want to stick with that script, you can look at this scripts source and figure out how the do the design patterns. Sorry if this is not the answer you are looking for. The sections I would highlight for you to concentrate on would be:

/* generate random lines in background */
/* generate random dots in background */

Both of those items seem to be right up your alley of what you are wanting to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文