Heim  >  Artikel  >  Backend-Entwicklung  >  Einfacher PHP-Bestätigungscode

Einfacher PHP-Bestätigungscode

巴扎黑
巴扎黑Original
2016-11-24 10:36:05878Durchsuche



class VerifyImg {

public $fontSize = 15; //Schriftgröße definieren

public $length = 4; //Stringlänge definieren                                                                                                                                                                   Definieren Sie die Bildbreite

public $height = 30 ; //Definieren Sie die Bildhöhe

public $im = null; //Erzeugen Sie ein Bild mit einer angegebenen Breite und Höhe

public $ font = 'C:/Windows/Fonts/Arial. TTF';

public $strNum = "";

public function Build() {

$strings = Array ('1', '2','3' , '4', '5', '6', '7', 'a', 'b', 'c', 'd', 'e', ​​'f', 'h', 'i' , 'j', 'k', 'm', 'n', 'p', 'r', 's', 't', 'u', 'v' , 'w', 'x', ' y' );

// $strNum = "";

$count = count ( $strings );

for ($i = 1; $i < = $this->length; $i ++) { //Durchlaufe zufällige Zeichen, um eine Zeichenfolge zu generieren

$strNum .= $strings [rand ( 0, $count - 1 )];

}

session_start ();

$_SESSION ["verifycode"] = $strNum;

$this- >im = imagecreate ( $this-> ;width, $this->height );

$backgroundcolor = imagecolorallocate ( $this->im, 255, 255, 255 ); //Hintergrundfarbe generieren

$frameColor = imageColorAllocate ( $this->im, 0, 255, 0 );

for($i = 0; $i < $this->length; $i ++) {

$charY = ($this->height + 9) / 2 + rand ( - 1, 1 ); //Zeichen Y-Koordinate definieren

$charX = $i * 15 + 8; Definieren Sie das Zeichen. Generieren Sie die Zeichenfarbe

$text_color = imagecolorallocate ( $this->im, mt_rand ( 50, 255 ), mt_rand ( 50, 128 ), mt_rand ( 50, 200 ) );

$angle = rand ( - 20, 20 ); //Zeichenwinkel generieren

//Zeichen schreiben

imagettftext ( $this->im, $this->fontSize, $angle, $charX, $charY, $text_color, $this->font, $strNum [$i] );

}

for($i = 0; $i < ;= 5; $i ++) { //Schleife zum Zeichnen von Hintergrundlinien

$linecolor = imagecolorallocate ( $this->im, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );

$linex = mt_rand ( 1, $this->width - 1 );

$liney = mt_rand ( 1, $this->height - 1 );

imageline ( $this->im, $linex, $liney, $linex + mt_rand ( 0, 4 ) - 2, $liney + mt_rand ( 0, 4 ) - 2, $linecolor );

}

for($i = 0; $i <= 32; $i ++) { //Schleife zum Zeichnen von Hintergrundpunkten, um Lochfraßeffekt zu erzeugen

$pointcolor = imagecolorallocate ( $this- >im, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );

imagesetpixel ( $this->im , mt_rand ( 1, $this-> width - 1 ), mt_rand ( 1, $this->height - 1 ), $pointcolor );

}

imagerectangle ( $this ->im, 0, 0, $ this->width - 1, $this->height - 1, $frameColor ); //Zeichne einen Rahmen

ob_clean ();

header ( 'Content-type:image/png' );

imagepng ( $this->im );

imagedestroy ( $this->im );

}

}

$img = new VerifyImg ();

$img->Build ();

?>

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn