Title: PHP畫圖基礎
Author: MoreWindows
Blog: http://blog.csdn.net/MoreWindows
KeyWord: PHP繪圖 畫點、線、弧線 繪制和填充區域 圖片特效 彩色圣誕節大雪花圖
本篇對PHP常用的繪圖函數進行總結。內容有建立圖像,為圖像分配顏色,畫點,畫線,畫弧線,繪制和填充區域,輸出字符和漢字及一些常見的圖片特效如反色和浮雕。此外還給出一些有趣的實例,如繪制彩色的圣誕節大雪花圖。
一.新建圖像
resource imagecreate( int $x_size , int $y_size )
imagecreate()返回一個圖像標識符,代表了一幅大小為 x_size 和y_size 的空白圖像。
resource imagecreatetruecolor( int $x_size , int $y_size )
imagecreatetruecolor() 返回一個圖像標識符,代表了一幅大小為 x_size 和y_size 的黑色圖像。PHP手冊上推薦盡量使用imagecreatetruecolor()函數。
還有根據.gif、.png、.jpg等文件來創建圖像的函數。
resource imagecreatefromgif( string $filename )
resource imagecreatefrompng ( string $filename )
resource imagecreatefromjpeg( string $filename )
二.為圖像分配顏色
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
imagecolorallocate() 返回一個標識符,代表了由給定的 RGB 成分組成的顏色。red,green 和 blue 分別是所需要的顏色的紅,綠,藍成分。這些參數是 0 到 255 的整數或者十六進制的 0x00 到 0xFF。第一次圖像調用 imagecolorallocate()表示設置圖像背景色。
int imagecolorallocatealpha( resource $image , int $red , int $green , int $blue , int $alpha )
imagecolorallocatealpha() 的行為和imagecolorallocate()相同,但多了一個額外的透明度參數alpha,其值從 0 到127。0表示完全不透明,127 表示完全透明。
三.畫點
bool imagesetpixel( resource $image , int $x , int $y , int $color )
注:圖像左上角為(0,0)
四.畫線
bool imageline( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
從(x1, y1)到(x2,y2)。線的風格可以由bool imagesetstyle( resource $image , array $style )來控制。寬度由bool imagesetthickness ( resource $image , int $thickness )控制,注意這個寬度在畫矩形、弧線時也生效。
五.畫橢圓弧
bool imagearc(resource $image , int $cx , int $cy , int $w , int $h , int $s , int $e , int $color)
imagearc()以cx,cy(圖像左上角為 0, 0)為中心在 image 所代表的圖像中畫一個橢圓弧。w和h 分別指定了橢圓的寬度和高度,起始和結束點以 s 和e參數以角度指定。0度位于三點鐘位置,以順時針方向繪畫。如:
$black = imagecolorallocate($img, 0, 0, 0);
imagearc($img, 100, 100, 150, 180, 0, 90,$black);
將在(100,100)處畫一段寬150高180的從0到90度的弧,如下圖所示(作為參照,右邊是全圖):
六.繪制區域
矩形
bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )
橢圓
bool imageellipse ( resource $image , int $cx , int $cy , int $w , int $h , int $color )
多邊形
bool imagepolygon ( resource $image , array $points , int $num_points , int $color )
七.填充區域
填充區域
bool imagefill( resource $image , int $x , int $y , int $color )
imagefill()在image圖像的(x,y)處用 color顏色執行區域填充(即與 (x, y) 點顏色相同且相鄰的點都會被填充)。如以下代碼片段會先畫出藍色的橢圓,然后用紅色填充橢圓內部。
$blue_color = imagecolorallocate($img, 0, 0, 255);
$red_color = imagecolorallocate($img, 255, 0, 0);
imageellipse($img, 300, 200, 300, 200, $blue_color);
imagefill($img, 300, 200, $red_color);
$blue_color = imagecolorallocate($img, 0, 0, 255);
$red_color = imagecolorallocate($img, 255, 0, 0);
imageellipse($img, 300, 200, 300, 200, $blue_color);
imagefill($img, 300, 200, $red_color);
運行效果如下:
畫一橢圓并填充
bool imagefilledellipse( resource $image , int $cx , int $cy , int $w , int $h , int $color )
這種畫法橢圓是沒有邊框的,當然也可以如下實現:
$lucency_color = imagecolorallocatealpha($img, 0, 0, 0, 126);//127為全透明 0全不透明
$red_color = imagecolorallocate($img, 255, 0, 0);
imageellipse($img, 300, 200, 300, 200, $lucency_color);
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答