伺服器是ubuntu的,使用源碼的方式安裝的php,能夠看到gd庫都開啟了,
但是就是產生圖片的時候,顯示不出來,出來是錯號的小圖。同樣的代碼,在本機是沒有問題的。
兄弟們想看代碼。我貼上,但是這個代碼問題的可能性不大。同樣的代碼,在我開發環境中,沒有問題可以出來圖片的。我最大的問題,就是看不到出錯日誌?到底是什麼錯誤都不清楚。只是感覺是gd庫的問題。
/* 圖片驗證碼 Powered By KASON test http://www.jbxue.com */ session_start(); $num=4;//驗證碼個數 $width=80;//驗證碼寬度 $height=20;//驗證碼高度 $code=' '; for($i=0;$i<$num;$i++)//產生驗證碼 { switch(rand(0,2)) { case 0:$code[$i]=chr(rand(48,57));break;//數字 case 1:$code[$i]=chr(rand(65,90));break;//大寫字母 case 2:$code[$i]=chr(rand(97,122));break;//小寫字母 } } $_SESSION["VerifyCode"]=$code; $image=imagecreate($width,$height); imagecolorallocate($image,255,255,255); for($i=0;$i<80;$i++)//產生幹擾像素 { $dis_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255)); imagesetpixel($image,rand(1,$width),rand(1,$height),$dis_color); } for($i=0;$i<$num;$i++)//列印字元到映像 { $char_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255)); imagechar($image,60,($width/$num)*$i,rand(0,5),$code[$i],$char_color); } header("Content-type:image/gif"); //這裡換成png,下面方法換成imagepng,都是不行的。 imagegif($image);//輸出映像到瀏覽器 imagedestroy($image);//釋放資源 ?>
代碼沒有問題,有問題就是php與gd的配置了。這個問題最大的問題是沒有解決問題的思路了。
回複內容:
伺服器是ubuntu的,使用源碼的方式安裝的php,能夠看到gd庫都開啟了,
但是就是產生圖片的時候,顯示不出來,出來是錯號的小圖。同樣的代碼,在本機是沒有問題的。
兄弟們想看代碼。我貼上,但是這個代碼問題的可能性不大。同樣的代碼,在我開發環境中,沒有問題可以出來圖片的。我最大的問題,就是看不到出錯日誌?到底是什麼錯誤都不清楚。只是感覺是gd庫的問題。
/* 圖片驗證碼 Powered By KASON test http://www.jbxue.com */ session_start(); $num=4;//驗證碼個數 $width=80;//驗證碼寬度 $height=20;//驗證碼高度 $code=' '; for($i=0;$i<$num;$i++)//產生驗證碼 { switch(rand(0,2)) { case 0:$code[$i]=chr(rand(48,57));break;//數字 case 1:$code[$i]=chr(rand(65,90));break;//大寫字母 case 2:$code[$i]=chr(rand(97,122));break;//小寫字母 } } $_SESSION["VerifyCode"]=$code; $image=imagecreate($width,$height); imagecolorallocate($image,255,255,255); for($i=0;$i<80;$i++)//產生幹擾像素 { $dis_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255)); imagesetpixel($image,rand(1,$width),rand(1,$height),$dis_color); } for($i=0;$i<$num;$i++)//列印字元到映像 { $char_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255)); imagechar($image,60,($width/$num)*$i,rand(0,5),$code[$i],$char_color); } header("Content-type:image/gif"); //這裡換成png,下面方法換成imagepng,都是不行的。 imagegif($image);//輸出映像到瀏覽器 imagedestroy($image);//釋放資源 ?>
代碼沒有問題,有問題就是php與gd的配置了。這個問題最大的問題是沒有解決問題的思路了。
資訊還是不足以確定問題,我感覺應該是在header之前有輸出錯誤資訊,導致圖片無法顯示。你可以嘗試這樣調試一下:
- 不要放到img標籤裡面去調試,直接瀏覽器訪問請求輸出圖片的PHP,http://www.host.com/code.php
- 在
session_start();前面加個error_reporting(E_ALL);ini_set("display_errors","On");先看看有沒有錯誤
應該是你的伺服器配置問題。放在我的ubuntu伺服器上一切正常。
參考:php產生圖片縮圖
PHP 圖片處理類 phpThumb參數用法介紹
php上傳圖片功能的實現
php 上傳圖片加浮水印且支援透明的代碼
php上傳圖片並打上透明浮水印的代碼
php圖片加浮水印與上傳圖片加浮水印類
php使用GD庫產生bmp格式的圖片(imagebmp)
php GD庫上傳圖片並建立縮圖的代碼