標籤:header com ubi imagej code lin type info with
首先得確定php.ini設定有沒有開啟GD擴充功能,測試例如以下
print_r(gd_info());
假設有列印出內容例如以下,則說明GD功能有開啟:
Array( [GD Version] => bundled (2.0.34 compatible) [FreeType Support] => 1 [FreeType Linkage] => with freetype [T1Lib Support] => 1 [GIF Read Support] => 1 [GIF Create Support] => 1 [JPG Support] => 1 [PNG Support] => 1 [WBMP Support] => 1 [XPM Support] => [XBM Support] => 1 [JIS-mapped Japanese Font Support] => )
GD繪圖一般過程例如以下:
1.建立一張畫布資源
2.建立顏色畫筆
3.繪圖
4.儲存圖片或輸出圖片
5.銷毀記憶體畫布資源
測試代碼例如以下:
<?phpheader("Content-type: image/jpeg");$width = 400; //寬。高$height = 400; $image = imagecreate($width, $height); //第一步:建立空白映像$white = imagecolorallocate($image, 0, 0, 0); //第一次對 imagecolorallocate() 的調用會給基於調色盤的映像填充背景色,即用 imagecreate() 建立的映像。 $green = imagecolorallocate($image, 0, 255, 0); //第二步:為映像分配顏色imageline($image, 0, 20, 400, 20, $green); //第三步:畫線imagerectangle($image,100,40,300,100,$green); //畫矩形imagearc($image, 200, 150, 90, 90, 0, 360, $green); //畫圓imagestring($image, 14, 100, 240, "PHP is NiuBi HongHong!", $green); //寫字串$str="abcdefghjklmnpqrstuvwxyz23456789";$randstr = substr(str_shuffle($str), 0,4);imagestring($image, 14, 100, 260, $randstr, $green); //驗證碼imagettftext($image, 14, 0, 100, 300, $green, ‘./MSJHBD.TTF‘, "中文vsEnglish"); //中文驗證// imagejpeg($image,‘./test.jpg‘); //在當前路徑下儲存圖片為test.jpgimagejpeg($image); //第四步:不加檔案名稱。直接輸出到網頁 imagedestroy($image); //第五步:銷毀,回收資源?>
測試圖片例如以下:
註:GD庫強大的能夠畫各種報表(如柱狀圖,餅狀圖等)、縮圖、加浮水印圖和股票走勢圖
縮圖功能範例:
<?phpheader("Content-type: image/png");$width = 300; //原圖寬。高$height = 210; $thumb_width = (int)$width/2;$thumb_height = (int)$height/2;$dst = imagecreate($thumb_width,$thumb_height); //建立縮圖畫布$gray = imagecolorallocate($dst, 100, 100, 100);$src = imagecreatefrompng(‘./me.png‘); //讀取原圖//把原圖copy到縮圖畫布上imagecopyresampled($dst, $src, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); imagepng($dst,‘./me_thumb.png‘);imagedestroy($dst);imagedestroy($src);?>
PHP利用GD庫繪圖和產生驗證碼圖片