• Hesap Oluştur
  • Oturum Aç

GÜVENLIK KODU (CAPTCHA) OLUŞTURMA SINIFI

kara_kutu    0

    • avatar

    kara_kutu  04-01-2016, Saat: 00:31

    Kredi:
    #1
    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' >";
        }
    }
     
    Beğeni


Konu ile Alakalı Benzer Konular
Konular Yazar Yorumlar Okunma Son Yorum
  Sürekli Değişen Arka Plan Kodu kara_kutu 0 834 05-02-2016, Saat: 10:57
Son Yorum: kara_kutu
  PHP Ile Veritabanında Tablo Oluşturma kara_kutu 0 700 05-02-2016, Saat: 10:49
Son Yorum: kara_kutu
  MYSQL VERITABANI BAĞLANTISI SINIFI kara_kutu 0 673 04-01-2016, Saat: 00:38
Son Yorum: kara_kutu
  PHP DE DIZIN OLUŞTURMA kara_kutu 0 608 04-01-2016, Saat: 00:04
Son Yorum: kara_kutu
  PHP DE DOSYALAR OLUŞTURMA VE SILME kara_kutu 0 617 03-01-2016, Saat: 23:58
Son Yorum: kara_kutu
Konuyu Okuyanlar: 1 Ziyaretçi

Hızlı Menü: