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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
<?php
class Mathcaptcha{
#session_start();
public function image(){
/*===============================================================
General captcha settings
===============================================================*/
// captcha width
$captcha_w = 150;
// captcha height
$captcha_h = 35 ;
// minimum font size; each operation element changes size
$min_font_size = 12;
// maximum font size
$max_font_size = 18;
// rotation angle
$angle = 20;
// background grid size
$bg_size = 13;
// path to font - needed to display the operation elements
$font_path = '/font/courbd.ttf';
// array of possible operators
$operators=array('+','-','*');
// first number random value; keep it lower than $second_num
$first_num = rand(1,5);
// second number random value
$second_num = rand(6,11);
/*===============================================================
From here on you may leave the code intact unless you want
or need to make it specific changes.
===============================================================*/
shuffle($operators);
$expression = $second_num.$operators[0].$first_num;
#echo "=";
/*
operation result is stored in $session_var
*/
eval("\$session_var=".$second_num.$operators[0].$first_num.";");
/*
save the operation result in session to make verifications
*/
$_SESSION['Captcha'] = $session_var;
#$this->Session->write('security_number',$session_var);
/*
start the captcha image
*/
$img = imagecreate( $captcha_w, $captcha_h );
/*
Some colors. Text is $black, background is $white, grid is $grey
*/
$black = imagecolorallocate($img,0,0,0);
$white = imagecolorallocate($img,255,255,255);
$grey = imagecolorallocate($img,215,215,215);
/*
make the background white
*/
imagefill( $img, 0, 0, $white );
/* the background grid lines - vertical lines */
for ($t = $bg_size; $t<$captcha_w; $t+=$bg_size){
imageline($img, $t, 0, $t, $captcha_h, $grey);
}
/* background grid - horizontal lines */
for ($t = $bg_size; $t<$captcha_h; $t+=$bg_size){
imageline($img, 0, $t, $captcha_w, $t, $grey);
}
/*
this determinates the available space for each operation element
it's used to position each element on the image so that they don't overlap
*/
$item_space = $captcha_w/3;
/* first number */
imagettftext(
$img,
rand(
$min_font_size,
$max_font_size
),
rand( -$angle , $angle ),
rand( 10, $item_space-20 ),
rand( 25, $captcha_h-25 ),
$black,
$font_path,
$second_num);
/* operator */
imagettftext(
$img,
rand(
$min_font_size,
$max_font_size
),
rand( -$angle, $angle ),
rand( $item_space, 2*$item_space-20 ),
rand( 25, $captcha_h-25 ),
$black,
$font_path,
$operators[0]);
/* second number */
imagettftext(
$img,
rand(
$min_font_size,
$max_font_size
),
rand( -$angle, $angle ),
rand( 2*$item_space, 3*$item_space-20),
rand( 25, $captcha_h-25 ),
$black,
$font_path,
$first_num);
/* image is .jpg */
header("Content-type:image/jpeg");
/* name is secure.jpg */
header("Content-Disposition:inline ; filename=secure.jpg");
/* output image */
return imagejpeg($img);
}
}
?> |
Partager