Posts Easy PHP Captcha
Post
Cancel

Easy PHP Captcha

This class can generate images for user CAPTCHA validation.

It can generate a random alphanumeric text of random length between 4 and 8 characters for CAPTCHA validation.

The class can also generate a JPEG image with the random validation text displayed in it. The generated image saved to a server file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//https://www.phpclasses.org/package/10210-PHP-Generate-images-for-user-CAPTCHA-validation.html

<?php class="" easycaptcha="" {="" private="" $chars="abcdefghijklmnopqrstuvwxyz0123456789" ;="" private="" $finalchar;="" public="" function="" generatestring()="" {="" $size="rand(4," 8);="" echo="" $size;="" $temp="" ;="" for="" ($i="0;" $i=""?>< $size;="" $i++)="" {="" $temp.="$this-">chars[rand(0, strlen($this->chars) - 1)];
        }
        $this->finalChar = $temp;
        return $this->finalChar;
    }

    public function generateImage($urlImage=null) {
        $im = imagecreatetruecolor(120, 40);
        $val = (string) $this->finalChar;
        $text_color = imagecolorallocate($im, 255, 255, 255);
        imagestring($im, 15, 15, 15, $val, $text_color);
		if($urlImage == null){
			imagejpeg($im, 'captcha.jpg', 100);
			imagedestroy($im);
			echo "![](captcha.jpg)";	
		}
		else{
			imagejpeg($im, $urlImage.'captcha.jpg', 100);
			imagedestroy($im);
			echo "![](".$urlImage."captcha.jpg)";
			//  assets/uploadimg/captcha.jpg  ---- example of url
		}

    }

}

/* way to use */
  //  assets/uploadimg/captcha.jpg  ---- example of url
/*   session_start();
  $obj = new EasyCaptcha();
  $_SESSION["captcha"] =$obj->generateString();
  $obj->generateImage();
  echo $_SESSION["captcha"]; */

?>

origin - http://www.pipiscrew.com/?p=7239 easy-php-captcha

This post is licensed under CC BY 4.0 by the author.
Contents

Trending Tags