PHP驗證碼圖片顯示不了
PHP驗證碼圖片顯示不了,顯示的是一個紅色的叉叉。代碼如下:
image.php
PHP code
session_start();
if(isset($_POST['submit'])){
if(trim($_POST["test"])==$_SESSION['validationcode']){
echo '提交成功
';
}else{
echo ' 驗證碼輸入錯誤!!
';
}
}
?>
Image
imgcode.php
PHP code
showImage(); $_SESSION['validationcode'] =$image->getCheckCode(); ?>
ValidationCode.php
PHP code
width=$width; //為成員屬性width初使化 $this->height=$height; //為成員屬性height初使化 $this->codeNum=$codeNum; //為成員屬性codeNum初使化 $this->checkCode=$this->createCheckCode(); //為成員屬性checkCode初使化 } function showImage(){ //通過訪問該方法向瀏覽器中輸出映像 $this->getCreateImage(); //調用內部方法建立畫布並對其進行初使化 $this->outputText(); //向映像中輸出隨機的字串 $this->setDisturbColor(); //向映像中設定一些幹擾像素 $this->outputImage(); //產生相應格式的映像並輸出 } function getCheckCode(){ //訪問該方法擷取隨機建立的驗證碼字串 return $this->checkCode; //返回成員屬性$checkCode儲存的字串 } private function getCreateImage(){ //用來建立映像資源,並初使化背影 $this->image=imageCreate($this->width,$this->height); $back=imageColorAllocate($this->image, 255, 255, 255); $border=imageColorAllocate($this->image, 0, 0, 0); imageRectangle($this->image,0,0,$this->width-1,$this->height-1,$border); } private function createCheckCode(){ //隨機產生使用者指定個數的字串 for($i=0;$i<$this->codeNum;$i++) { $number=rand(0,2); switch($number){ case 0 : $rand_number=rand(48,57);break; //數字 case 1 : $rand_number=rand(65,90);break; //大寫字母 case 2 : $rand_number=rand(97,122);break; //小寫字母 } $ascii=sprintf("%c",$rand_number); $ascii_number=$ascii_number.$ascii; } return $ascii_number; } private function setDisturbColor() { //設定幹擾像素,向映像中輸出不同顏色的100個點 for ($i=0;$i<=100;$i++) { $color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255)); imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color); } } private function outputText() { //隨機顏色、隨機擺放、隨機字串向映像中輸出 for ($i=0;$i<=$this->codeNum;$i++) { $bg_color = imagecolorallocate($this->image, rand(0,255), rand(0,128), rand(0,255)); $x = floor($this->width/$this->codeNum)*$i+3; $y = rand(0,$this->height-15); imagechar($this->image, 5, $x, $y, $this->checkCode[$i], $bg_color); } } private function outputImage(){ //自動檢測GD支援的映像類型,並輸出映像 if(imagetypes() & IMG_GIF){ //判斷產生GIF格式映像的函數是否存在 header("Content-type: image/gif"); //發送標題資訊設定MIME類型為image/gif imagegif($this->image); //以GIF格式將映像輸出到瀏覽器 }elseif(imagetypes() & IMG_JPG){ //判斷產生JPG格式映像的函數是否存在 header("Content-type: image/jpeg"); //發送標題資訊設定MIME類型為image/jpeg imagejpeg($this->image, "", 0.5); //以JPEN格式將映像輸出到瀏覽器 }elseif(imagetypes() & IMG_PNG){ //判斷產生PNG格式映像的函數是否存在 header("Content-type: image/png"); //發送標題資訊設定MIME類型為image/png imagepng($this->image); //以PNG格式將映像輸出到瀏覽器 }elseif(imagetypes() & IMG_WBMP){ //判斷產生WBMP格式映像的函數是否存在 header("Content-type: image/vnd.wap.wbmp"); //發送標題為image/wbmp imagewbmp($this->image); //以WBMP格式將映像輸出到瀏覽器 }else{ //如果沒有支援的映像類型 die("PHP不支援映像建立!"); //不輸出映像,輸出一錯誤訊息,並退出程式 } } function __destruct(){ //當對象結束之前銷毀映像資源釋放記憶體 imagedestroy($this->image); //調用GD庫中的方法銷毀映像資源 } }?>