Question

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);
Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top