Rastgele resimv e renklerle güvenlik kodu (Captcha) oluturmaya yarayan PHP sınıfı
PHP- Kodu:
class Captcha

{

protected
$value;

protected
$image_name;



public function
__construct( $image_name )

{

$this->image_name = $image_name;

}



public static function
createNumber( $length = 4 )

{

$num = "";

for (
$i = 0; $i < $length; $i++ )

{

$r = mt_rand( 0, 9 );

$num = $num.$r;

}

return
$num;

}



public static function
createString( $length = 4 )

{

$val = "";

$values = "abcdefghijklmnopqrstuvwyz";

for (
$i = 0; $i < $length; $i++ )

{

$r = mt_rand( 0, 24 );

$val = $val.$values[$r];

}

return
$val;

}



public function
setValue( $value )

{

$this->value = $value;

}



public function
getValue( )

{

return
$this->value;

}



public function
showImage( $channel = null )

{

$im = imageCreate( 160, 60 );

$white = imagecolorallocate( $im, 255, 255, 255 );

$black = imagecolorallocate( $im, 0, 0, 0 );



$intensity = rand ( 200, 230 );



if ( !
$channel )

{

$channel = rand ( 1, 3 );

}

$ratio = rand ( 80, 100 );



switch (
$channel )

{

case
1:

$colorBgR = $intensity * $ratio / 100;

$colorBgG = $intensity * ( 100 - $ratio ) / 100;

$colorBgB = 0;



for (
$i = 0; $i < 5; $i++ )

{

$shift = rand( 100 - ( $i+1)*3, 100 + ( $i+1)*3 );

$new = $shift * $colorBgR / 100;



$bgColors[] = iMagecolorallocate( $im, $new, $intensity-$new, 0 );

}



for (
$i = 0; $i < 3; $i++ )

{

$shift = rand( 100 - ( $i+1)*7, 100 + ( $i+1)*7 );

$new = $shift * $colorBgR / 100;



$bgColors[] = iMagecolorallocate( $im, $intensity-$new, $new, 0 );

}

break;



case
2:

$colorBgG = $intensity * $ratio / 100;

$colorBgB = $intensity * ( 100 - $ratio ) / 100;

$colorBgR = 0;



for (
$i = 0; $i < 5; $i++ )

{

$shift = rand( 100 - ( $i+1)*3, 100 + ($i+1)*3 );

$new = $shift * $colorBgR / 100;



$bgColors[] = iMagecolorallocate( $im, 0, $new, $intensity-$new );

}



for (
$i = 0; $i < 3; $i++ )

{

$shift = rand( 100 - ( $i+1)*7, 100 + ( $i+1)*7 );

$new = $shift * $colorBgR / 100;



$bgColors[] = iMagecolorallocate( $im, 0 , $intensity-$new, $new );

}



case
3:

$colorBgB = $intensity * $ratio / 100;

$colorBgR = $intensity * ( 100 - $ratio ) / 100;

$colorBgG = 0;



for (
$i = 0; $i < 5; $i++ )

{

$shift = rand( 100 - ( $i+1)*3, 100 + ( $i+1)*3 );

$new = $shift * $colorBgR / 100;



$bgColors[] = iMagecolorallocate( $im, $intensity-$new, 0, $new );

}



for (
$i = 0; $i < 3; $i++ )

{

// to make it harder do symmetric range ^^

$shift = rand( 100 , 100 + ( $i+1)*20 );

$new = $shift * $colorBgR / 100;



$bgColors[] = iMagecolorallocate( $im, $new , 0, $intensity-$new );

}

break;

}









$bgColor = imagecolorallocate( $im, $colorBgR, $colorBgG, $colorBgB );





for (
$i = 0; $i < strlen( $this->value ); $i++ )

{

$rotate = rand(-15, 15);



$fontSize = rand(28, 36);

imagettftext($im, $fontSize ,$rotate , 10+$i*31, 50, $black, "impact.ttf",$this->value[$i] );

}









for (
$i = 0; $i < 160; $i++ )

{

for (
$j = 0; $j < 60; $j++ )

{

$pos = rand( 0, 7 );



if (
imagecolorat( $im, $i, $j ) == $black )

{

$pos = rand( 6, 7 );







imagesetpixel( $im, $i, $j, $bgColors[$pos] );

}

else

{

imagesetpixel( $im, $i, $j, $bgColors[$pos] );

}

}

}



imagejpeg( $im, "images/".$this->image_name.".jpg" );

imagedestroy( $im );

echo
" <img src='/images/".$this->image_name.".jpg' style='border: 1px solid #000;' width='160' >";

}

}

Alıntı