-
/**
- * Captchaimage ()-Create a captcha picture of distorted characters
- * $session the variable name of the session required to create the _name string verification code picture
- * $width int to verify the width of the picture, default 120, note: The picture height and width ratio is relatively fixed
- * $noise int interferon points, default 0
- * $disturb int number of interfering characters, default 0
- * $curve bool Increases interference curve, default ture
- * */
- function Captchaimage ($session _name = ", $width = +, $noise = 0, $disturb = 0, $curve = True) {
- $im _x = $width;
- $im _y = ceil (0.25 * $width);
- $im = Imagecreatetruecolor ($im _x, $im _y);
- $text _c = imagecolorallocate ($im, mt_rand (0, 0), Mt_rand (0, +), Mt_rand (+ 100));
- $gray _rand = Mt_rand (160, 220);
- $buttum _c = imagecolorallocate ($im, $gray _rand, $gray _rand, $gray _rand);
- Imagefill ($im, 0, 0, $buttum _c);
-
Session_Start ();
- $text = ";
- $characters = ' abcefghjklmnpqrstuvwxyz ';
- for ($i = 0, $len = strlen ($characters), $i < 4; $i + +) {
- $text. = $characters {rand (0, $len-1)};
- if (Isset ($session _name{0}) && session_start ()) {
- $_session[$session _name] = $text;
- $_session[' captchasessiontime '] = time ();
- }
- }
-
$font = ' Arial.ttf ';
- $size = Floor (0.2 * $width);
-
$ttfbox = Imagettfbbox ($size, 0, $font, $text);
- $text _width = ABS ($ttfbox [2]-$ttfbox [0]);
- $side = Floor (($im _x-$text _width)/2);
-
$array = Array (-1, 1);
- for ($i = 0; $i < strlen ($text); $i + +) {
- $p = Array_rand ($array);
- $an = $array [$p] * Mt_rand (5, 10); Character Tilt Angle
- Imagettftext ($im, $size, $an, $side + $i * ($size-1), $im _y-3, $text _c, $font, $text {$i});
- }
-
$distortion _im = Imagecreatetruecolor ($im _x, $im _y);
- Imagefill ($distortion _im, 0, 0, $buttum _c);
- $distortion = Floor (0.04 * $width); Degree of distortion
- for ($i = 0; $i < $im _x; $i + +) {
- for ($j = 0; $j < $im _y; $j + +) {
- $rgb = Imagecolorat ($im, $i, $j);
- if ((int) ($i + sin ($j/$im _y * 2 * m_pi) * $distortion) <= $im _x && (int) ($i + sin ($j/$im _y * 2 * m_pi) * $d Istortion) >= 0) {
- Imagesetpixel ($distortion _im, (int) ($i + sin ($j/$im _y * 2 * M_PI-M_PI * 0.1) * $distortion), $j, $rgb);
- }
- }
- }
-
if ($disturb > 0) {
- $chars = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ';
- for ($i = 0; $i < $disturb; $i + +) {
- Imagestring ($distortion _im, 3, Mt_rand ( -10, $im _x), Mt_rand ( -10, $im _y), $chars [Mt_rand (0, a)], $text _c);
- }
- }
-
adding interfering pixels;
- if ($noise > 0) {
- for ($i = 0; $i < $noise; $i + +) {
- $randcolor = Imagecolorallocate ($distortion _im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255));
- Imagesetpixel ($distortion _im, Mt_rand ()% $im _x, Mt_rand ()% $im _y, $randcolor);
- }
- }
-
Add Interference curve
- if ($curve) {
- $rand = Mt_rand (5, 30);
- $rand 1 = Mt_rand (15, 25);
- $rand 2 = Mt_rand (5, 10);
- for ($yy = $rand; $yy <= $rand + 2; $yy + +) {
- for ($px = -60; $px <=; $px + +) {
- $x = $px/$rand 1;
- $y = ($x! = 0)? Sin ($x): $y;
- $py = $y * $rand 2;
- Imagesetpixel ($distortion _im, $px +, $py + $yy, $text _c);
- }
- }
- }
-
Set the file header;
- Header ("Content-type:image/jpeg");
-
Export the image to a browser or file in PNG format;
- Imagepng ($distortion _im);
-
Destroys an image, releasing the memory associated with image;
- Imagedestroy ($distortion _im);
- Imagedestroy ($im);
- }
-
Captchaimage (' Code ', 100); Generate a picture and store random numbers in a session with key code
Copy Code |