1>GD庫簡介
GD指的是Graphic Device,PHP的GD庫是用來處理圖形的擴展庫,通過GD庫提供的一系列API,可以對圖像進行處理或者直接生成新的圖片。
PHP除了能進行文本處理以外,通過GD庫,可以對JPG、PNG、GIF、SWF等圖片進行處理。GD庫常用在圖片加水印,驗證碼生成等方面。
PHP默認已經集成了GD庫,只需要在安裝的時候開啟就行。
創建圖像的一般流程
設定標頭,告訴瀏覽器你要生成的MIME類型
創建一個圖像區域,以后的操作都將基于此圖像區域
在空白圖像區域繪制填充背景
在背景上繪制圖形輪廓輸入文本
輸出最終圖形
清除所有資源
其他頁面調用
header("content-type: image/png");$img=imagecreatetruecolor(100, 100);$red=imagecolorallocate($img, 0xFF, 0x00, 0x00); imagefill($img, 0, 0, $red); imagepng($img); imagedestroy($img);
繪制線條
imageline()
?語法:imageline(sX,
eX,
col);
繪制圓
imagearc()
?語法:imagearc (cx ,
w ,
startAngle,
color )
$img = imagecreatetruecolor(200, 200);// 分配顏色$red = imagecolorallocate($img, 255, 0, 0);$white = imagecolorallocate($img, 255, 255, 255);//背景填充白色 imagefill($img,0,0,$white);// 畫一個紅色的圓 imagearc($img, 100, 100, 150, 150, 0, 360, $red); imagepng($img);// 釋放內存 imagedestroy($img);
繪制矩形
imagerectangle()
?語法:imagerectangle (x1 ,
x2 ,
col)
$img = imagecreatetruecolor(200, 200);// 分配顏色$red = imagecolorallocate($img, 255, 0, 0);$white = imagecolorallocate($img, 255, 255, 255); imagefill($img,0,0,$white);// 畫一個紅色的矩形 imagerectangle ($img,50,50,100 ,100 ,$red); imagepng($img);// 釋放內存 imagedestroy($img);
繪制文字
?語法1:imagestring (font ,
y ,
col )
?語法2:imagettftext(size,
x,
color,
text)
header("content-type: image/png");//imagestring字體大小設置不了$img = imagecreatetruecolor(100, 100);$red = imagecolorallocate($img, 0xFF, 0x00, 0x00); imagestring($img, 5, 10, 10, "Hello world", $red); imagepng($img); imagedestroy($img);$img1=imagecreatetruecolor(200,200);$red=imagecolorallocate($img1,255,0,0);$white=imagecolorallocate($img1,255,255,255); imagefill($img1,0,0,$red);$font="C:\Windows\Fonts\simhei.ttf"; imagettftext($img1,23,0,100,100,$white,$font,"你好嗎"); imagepng($img1); imagedestroy($img1);
繪制噪點
?語法:imagesetpixel(x,
col)
//繪制10個噪點for($i=0;$i<10;$i++) { imagesetpixel($img, rand(0, 100) , rand(0, 100) , $black); imagesetpixel($img, rand(0, 100) , rand(0, 100) , $green); }
輸出圖像文件
filename)
通過imagepng可以直接輸出圖像到瀏覽器,通過指定路徑參數將圖像保存到文件中
1. imagepng()
?意義:將圖片保存成png格式
?語法:imagepng(
2. imagejpeg()
?意義:將圖片保存成jpeg格式
?語法:imagepng(filename,$quality)
3. imagegif()
?意義:將圖片保存成gif格式
?語法:imagegif(filename)案例:
1. 隨機產生驗證碼(php)
2. 給圖片添加水印
相關推薦:
GD庫生成水印亂碼的解決辦法
詳解PHP如何使用GD庫完成驗證碼效果教程
什么是GD庫?在PHP中加載GD庫的具體介紹
以上就是php圖像圖形的操作GD庫的使用基礎教程的詳細內容,更多請關注php中文網其它相關文章!