PHP之GD2使用

我們的php和java一樣,有自己的圖像處理函數,下面我們就介紹一下php中的繪製圖像的大致流程

1.創建一個畫布
2.開始繪畫
3.輸出圖像
4.銷燬圖片(釋放內存)

接下來我們介紹幾個php中常用的GD2函數

創建一個基於真彩的畫布
	imagecreatetruecolor(int x_size,int y_size);
	分配一個顏色
	imagecolorallocate(resource image,int red,int green,int blue);//紅綠藍
	區域填充
	imagefill(resource image,int x,int y,int color);
	矩形框
	imagerectangle(resource image,int x1,int y1,int x2,int y2,int color);
	畫一個像素點
	imagesetpixel(resource image,int x,int y,int color);
	畫一條線段
	imageline(resource image,int x1,int y1,int x2,int y2,int color);
	繪製文本內容
	imagettftext(resource image,float size,float angle,int x,int y,int color,string content);
	以png格式將圖像輸出到瀏覽器或文件
	imagepng(resource image[,string filename]);
	銷燬一個圖像
	imagedestroy(resource image);

	int rand([int min,int max]);//產生一個隨機數
	strlen();//獲取字符串的長度
	header();//設置相應頭信息

PS:我自己的理解,就是我們先創建一個畫布,然後就像我們畫畫一樣,利用自己喜歡的顏色,去繪製圖像,有時不要將問題想得太複雜,不然會適得其反,

同時,我們一定要注意代碼的順序,不然我們會得到意想不到的效果,

1.畫布:imagecreate(int width,int height);

2.顏色:imagecolorallocate(resource image,int red,int green,int blue);

3.利用顏色我們在畫布上進行繪製圖像

imagefill,imagerectangle等

4.輸出圖像(向瀏覽器輸出)

imagepng,imagegif,imagejpeg,imageline等

5.銷燬圖片,釋放內存

imagedeatroy();


demo:

<?php
$im=imagecreate(200,200);
$color=imagecolorallocate($im,8,2,133);
imagefill($im,$color);
header("content-type:image/png");
imagepng($im);
imagedeatroy($im);
?>

希望大家多多動手,當你運行出結果後,你收穫的不只是知識
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章