PHP画图基础
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);運行效果如下:?????? 畫一橢圓并填充
?????? 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); imagefill($img, 300, 200, $red_color); //imagefilledellipse($img, 300, 200, 300, 200, $red_color);??????? 畫一矩形并填充
??????? bool imagefilledrectangle (resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
類似于畫一橢圓并填充。
?
???????? 畫一橢圓弧且填充
???????? bool imagefilledarc( resource $image , int $cx , int $cy , int $w , int $h , int $s , int $e , int $color , int $style )
對最后一個參數說明下,有4種值:
IMG_ARC_PIE??? 產生圓形邊界(如果兩個都用,IMG_ARC_CHORD生效)。
IMG_ARC_CHORD? 用直線連接了起始和結束點。
IMG_ARC_NOFILL畫弧,只有輪廓,不填充。
IMG_ARC_EDGED? 指明用直線將起始和結束點與中心點相連
看下實際的效果(圓弧角度從0到210度):
下一篇將用這個函數來畫餅狀圖。
八.字符
?????? 水平地畫一個字符
?????? bool imagechar(resource $image , int $font , int $x , int $y , string $c , int $color)
????? 垂直地畫一個字符
????? bool imagecharup(resource $image , int $font , int $x , int $y , string $c , int $color)
????? 水平地畫一行字符串
????? bool imagestring(resource $image , int $font , int $x , int $y , string $s , int $col)
????? 垂直地畫一行字符串
?????? bool imagestringup(resource $image , int $font , int $x , int $y , string $s , int $col)
$font參數要注意下,要么使用內置的字體(從1到5),要么用int imageloadfont ( string $file )加載字體后再設置。
可以用輸出*來得到彩色的圣誕節雪花圖,代碼如下:
<?php // by MoreWindows( http://blog.csdn.net/MoreWindows ) $imgWidth = 300; $imgHeight = 200; $img = imagecreate($imgWidth, $imgHeight); imagecolorallocate($img, 255, 255, 255);//設置底色 $snowflake_size = 5; //可從1到5 //生成雪花 其實就是調用imagechar()輸出*號 for ($i=1; $i<=400; $i++) imagechar($img, $snowflake_size, mt_rand(0, $imgWidth),mt_rand(0, $imgHeight), "*", imagecolorallocate($img, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255))); imagepng($img); imagedestroy($img); ?>運行效果如下:
九.文本
?????? array imagettftext(resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
解釋幾個參數:
第二參數$size為字體大小。
第三參數$angle為文本旋轉角度,0度為從左向右讀的文本,更高數值表示逆時針旋轉。例如 90 度表示從下向上讀的文本。
第七個參數$fontfile表示字體文件,如"c:\\WINDOWS\\Fonts\\simhei.ttf"。
注意!使用這個函數應當配合imagecreatetruecolor(),而不是imagecreate()。
下面用這個imagettftext()來代替上面的imagechar從而生成彩色的圣誕節大雪花圖,代碼如下:<?php // by MoreWindows( http://blog.csdn.net/MoreWindows ) $imgWidth = 600; $imgHeight = 400; $img = imagecreatetruecolor($imgWidth, $imgHeight); imagefill($img, 0, 0, imagecolorallocate($img, 240, 240, 240));//設置底色 $snowflake_size = 30; $font_file = "c:\\WINDOWS\\Fonts\\simhei.ttf"; //生成大雪花 其實就是調用imagettftext()輸出*號 for ($i=1; $i<=400; $i++) {$font_color = imagecolorallocate($img, mt_rand(100,200), mt_rand(100,200), mt_rand(100,200));imagettftext($img, $snowflake_size, mt_rand(0, 180), mt_rand(0, $imgWidth),mt_rand(0, $imgHeight), $font_color, $font_file, "*"); } //水印文字 $black_color = imagecolorallocate($img, 0, 0, 0); imagettftext($img, 12, 0, $imgWidth - 200 , $imgHeight - 20, $black_color, $font_file, "大雪花圖 by MoreWindows"); imagepng($img); imagedestroy($img); ?>運行效果如下:
?
十.圖像特效
??????? bool imagefilter ( resource $src_im , int $filtertype [,int $arg1 [, int $arg2 [, int $arg3 ]]] )
這里提供了很多特效,如浮雕,反色(底片色),調節灰度、亮度,對比度,模糊化等等。這只展示幾種常用的特效,更多的請訪問http://www.php.net/manual/zh/function.imagefilter.php。
原圖:
將圖處保存到D:\\1234.png,就可以執行下面的代碼了。
IMG_FILTER_NEGATE:將圖像中所有顏色反轉(底片色)。
代碼:
<?php // by MoreWindows( http://blog.csdn.net/MoreWindows ) $img = imagecreatefrompng("D:\\1234.png"); imagefilter($img, IMG_FILTER_NEGATE); imagepng($img); imagedestroy($img); ?>IMG_FILTER_EMBOSS:使圖像浮雕化。
代碼:
<?php // by MoreWindows( http://blog.csdn.net/MoreWindows ) $img = imagecreatefrompng("D:\\1234.png"); imagefilter($img, IMG_FILTER_EMBOSS); imagepng($img); imagedestroy($img); ?>本篇就介紹到此,下一篇《PHP 畫圖應用 驗證碼 柱狀圖》將用本篇介紹的函數來繪制驗證碼和柱狀圖。
?
?
轉載請標明出處,原文地址:http://blog.csdn.net/morewindows/article/details/7274870
轉載于:https://www.cnblogs.com/long12365/p/9731294.html
總結
- 上一篇: Spring Sleuth和Zipkin
- 下一篇: hosts 持续更新 - laod