php图像处理大全
- imagecreate()?和?imagecreatetruecolor()?函數用于創建一幅空白圖像。
- imagedestroy()?函數用于銷毀圖像資源。
imagecreate()
如果我們要對圖像進行處理,就如其它圖像處理軟件一樣,需要創建一塊畫布。imagecreate() 和 imagecreatetruecolor() 函數用于創建一幅空白圖像。
語法:
resource imagecreate( int x, int y )參數 x ,y 分別為要創建圖像的寬度和高度像素值,返回一個圖像資源。
例子:
<? header("Content-type: image/png");//創建圖像 $im = @imagecreate(200, 50) or die("創建圖像資源失敗");//圖片背景顏色 $bg = imagecolorallocate($im, 255, 255, 255);//文字顏色 $text_color = imagecolorallocate($im, 0, 0, 255);//水平畫一行字,要輸出中文等需要 TTF 字體支持的請使用 magettftext() 函數 imagestring($im, 5, 0, 0, "Hello world!", $text_color);//以PNG格式輸出圖像 imagepng($im);//銷毀圖像資源 imagedestroy($im); ?>該例子以圖像格式輸出一行文字:Hello world! 。例子中用到的其他函數,將在后面逐一介紹。
imagecreatetruecolor()
imagecreatetruecolor() 功能與 imagecreate() 類似,創建一幅真彩色的圖像,從而支持更為豐富的色彩。
語法:
resource imagecreatetruecolor( int x, int y )注意:本函數不能用于 GIF 文件格式。
imagedestroy()
圖像處理完成后,使用 imagedestroy() 指令銷毀圖像資源以釋放內存,雖然該函數不是必須的,但使用它是一個好習慣。
語法:
bool imagedestroy( resource image )具體使用可見上面創建圖像例子。
- getimagesize()?函數用于獲取圖像尺寸,類型等信息。
- imagesx()?函數用于獲取圖像的寬度。
- imagesy()?函數用于獲取圖像的高度。
getimagesize()
getimagesize() 函數用于獲取圖像大小及相關信息,成功返回一個數組,失敗則返回 FALSE 并產生一條 E_WARNING 級的錯誤信息。
語法:
array getimagesize( string filename )例子:
<?php $array = getimagesize("images/flower_1.jpg"); print_r($array); ?>瀏覽器顯示如下:
Array ([0] => 350[1] => 318[2] => 2[3] => width="350" height="318"[bits] => 8[channels] => 3[mime] => image/jpeg )返回結果說明
header("Content-type: image/jpeg");
提示
如您要在自己的電腦上運行本教程中圖像處理的例子,請將教程中用到的圖片下載到本地 images 文件夾下備用:
http://www.5idev.com/Public/Images/article/flower_1.jpg?
http://www.5idev.com/Public/Images/article/logo_mark.gif
imagesx()
imagesx() 函數用于獲取圖像的寬度,單位為像素,返回值為整型。
語法:
int imagesx( resource image )參數為如 imagecreatetruecolor()、imagecreatefromjpeg() 等函數返回的圖像資源。
imagesy()
imagesy() 函數用于獲取圖像的高度,語法及用法同 imagesx() 。
語法:
int imagesy( resource image )例子:
<?php $img = imagecreatefromjpeg("images/flower_1.jpg"); echo "圖像寬度:",imagesx( $img ),"<br />"; echo "圖像高度:",imagesy( $img ); ?>瀏覽器輸出:
圖像寬度:350 圖像高度:318
imagecreatefrom 系列函數用于從文件或 URL 載入一幅圖像。
載入圖像
imagecreatefrom 系列函數用于從文件或 URL 載入一幅圖像,成功返回圖像資源,失敗則返回一個空字符串。
該系列函數有:
- imagecreatefromgif():創建一塊畫布,并從 GIF 文件或 URL 地址載入一副圖像
- imagecreatefromjpeg():創建一塊畫布,并從 JPEG 文件或 URL 地址載入一副圖像
- imagecreatefrompng():創建一塊畫布,并從 PNG 文件或 URL 地址載入一副圖像
- imagecreatefromwbmp():創建一塊畫布,并從 WBMP 文件或 URL 地址載入一副圖像
- imagecreatefromstring():創建一塊畫布,并從字符串中的圖像流新建一副圖像
語法:
resource imagecreatefromgif( string filename ) resource imagecreatefromjpeg( string filename ) resource imagecreatefrompng( string filename ) resource imagecreatefromwbmp( string filename ) resource imagecreatefromstring( string image )例子:
<? header("Content-type: image/jpeg");//創建并載入一幅圖像 $im = @imagecreatefromjpeg("images/flower_1.jpg");//錯誤處理 if(!$im){$im = imagecreatetruecolor(150, 30);$bg = imagecolorallocate($im, 255, 255, 255);$text_color = imagecolorallocate($im, 0, 0, 255);//填充背景色imagefilledrectangle($im, 0, 0, 150, 30, $bg);//以圖像方式輸出錯誤信息imagestring($im, 3, 5, 5, "Error loading image", $text_color); } else {//輸出該圖像imagejpeg($im); } ?>在該例子中,我們載入并輸出原圖。由于 PHP 對圖像創建錯誤沒有友好的錯誤提示,因此我們自定義了錯誤處理信息。
提示
對于 PHP 生成的圖片,如果要直接在普通網頁中顯示而不是通過 header 輸出,可以通過如下的方式調用:
<img src="pic.php" />
imagegif()、imagejpeg()、imagepng() 和 imagewbmp() 函數分別允許以 GIF、JPEG、PNG 和 WBMP 格式將圖像輸出到瀏覽器或文件。
PHP 輸出圖像
PHP 允許將圖像以不同格式輸出:
- imagegif():以 GIF 格式將圖像輸出到瀏覽器或文件
- imagejpeg():以 JPEG 格式將圖像輸出到瀏覽器或文件
- imagepng():以 PNG 格式將圖像輸出到瀏覽器或文件
- imagewbmp():以 WBMP 格式將圖像輸出到瀏覽器或文件
語法:
bool imagegif ( resource image [, string filename] ) bool imagejpeg ( resource image [, string filename [, int quality]] ) bool imagepng ( resource image [, string filename] ) bool imagewbmp ( resource image [, string filename [, int foreground]] )| image | 欲輸出的圖像資源,如 imagecreate() 或 imagecreatefrom 系列函數的返回值 |
| filename | 可選,指定輸出圖像的文件名。如果省略,則原始圖像流將被直接輸出。 |
| quality | 可選,指定圖像質量,范圍從 0(最差質量,文件最小)到 100(最佳質量,文件最大),默認75 ,imagejpeg() 獨有參數 |
| foreground | 可選,指定前景色,默認前景色是黑色,imagewbmp() 獨有參數 |
繪制一個圓弧并保存到 images 目錄下:
<?php header("Content-type: image/png");$im = @imagecreate(200, 200)or die("創建圖像資源失敗");$bg = imagecolorallocate($im, 204, 204, 204); $red = imagecolorallocate($im, 255, 0, 0);imagearc($im, 100, 100, 150, 150, 0, 360, $red);imagepng($im,"images/circle.png"); imagedestroy($im); ?>在 images 目錄下就會生成一個 circle.png 文件。
- imageline()?函數用于繪制一條線段。
- imagearc()?函數用于繪制橢圓弧(包括圓弧)。
- imagesetstyle()?函數用于設定畫線風格。
imageline()
imageline() 函數用于繪制一條線段。
語法:
bool imageline( resource image, int x1, int y1, int x2, int y2, int color )用 color 顏色在圖像 image 中從坐標 x1,y1 到 x2,y2(圖像左上角坐標為 0,0)畫一條線段。
例子:
<?php header("Content-type: image/png");$im = @imagecreate(300, 300)or die("創建圖像資源失敗"); $bg = imagecolorallocate($im, 204, 204, 204); $red = imagecolorallocate($im, 255, 0, 0); imageline($im,0,30,200,30,$red);imagepng($im); imagedestroy($im); ?>瀏覽器輸出圖像如下:
參考閱讀
- imagecreate():創建一幅空白圖像。
- imagecolorallocate():為圖像分配顏色。
imagesetstyle()
imagesetstyle() 設定所有畫線的函數(例如 imageline() 和 imagepolygon())在使用特殊顏色 IMG_COLOR_STYLED 或者用 IMG_COLOR_STYLEDBRUSHED 畫一行圖像時所使用的風格。如果成功則返回 TRUE ,失敗則返回 FALSE 。
語法:
bool imagesetstyle( resource image, array style )style 參數是像素組成的數組。
imageline() 函數配合 imagesetstyle() 可以畫一條虛線段:
<?php header("Content-type: image/png"); $im = @imagecreate(300, 50)or die("創建圖像資源失敗"); $bg = imagecolorallocate($im, 204, 204, 204); $red = imagecolorallocate($im, 255, 0, 0);// 畫一條虛線,5 個紅色像素,4 個背景像素 $style = array($red, $red, $red, $red, $red, $bg, $bg, $bg, $bg); imagesetstyle($im, $style);imageline($im, 0, 20, 200, 20, IMG_COLOR_STYLED);imagepng($im); imagedestroy($im); ?>瀏覽器輸出圖像如下:
imagearc()
imagearc() 函數用于繪制橢圓弧(包括圓弧)。
語法:
bool imagearc(resource image, int cx, int cy, int w, int h, int s, int e, int color )| image | 圖像資源,欲繪制橢圓弧的圖像 |
| cx | 橢圓中心 x 坐標 |
| cy | 橢圓中心 y 坐標 |
| w | 橢圓寬度 |
| h | 橢圓高度 |
| s | 起始點,0 表示 3 點鐘方向 |
| e | 角度,360 表示完全封閉 |
| color | 圖像顏色 |
例子:
<?php header("Content-type: image/png");$im = @imagecreate(200, 200)or die("創建圖像資源失敗");$bg = imagecolorallocate($im, 204, 204, 204); $red = imagecolorallocate($im, 255, 0, 0);imagearc($im, 100, 100, 150, 150, 0, 360, $red);imagepng($im); imagedestroy($im); ?>該例子繪制一個圓圈,瀏覽器輸出如下:
- imagefill()?函數用于圖像區域填充。
- imagefilledarc()?函數畫一橢圓弧并填充。
- imagefilledrectangle()?函數畫一矩形并填充。
- imagefilledpolygon()?函數畫一多邊形并填充。
imagefill()
imagefill() 函數用于區域填充。
語法:
bool imagefill( resource image, int x, int y, int color )x,y 分別為填充的起始 x 坐標和 y 坐標,與 x, y 點顏色相同且相鄰的點都會被填充。
例子:
<?php header("Content-type: image/png");$im = @imagecreatetruecolor(200, 200); $red = imagecolorallocate($im, 255, 0, 0);//用 $red 顏色填充圖像 imagefill( $im, 0, 0, $red );imagepng($im); imagedestroy($im); ?>提示:對于用?imagecreate()?建立的圖像,第一次調用?imagecolorallocate()?會自動給圖像填充背景色。
imagefilledarc()
imagefilledarc() 函數畫一橢圓弧并填充。
語法:
bool imagefilledarc( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style )該函數參數用法可參考繪制橢圓弧函數?imagearc()?,只是本函數增加 style 參數表示填充方式。
| IMG_ARC_PIE | 普通填充,產生圓形邊界 |
| IMG_ARC_CHORD | 只是用直線連接了起始和結束點,與 IMG_ARC_PIE 方式互斥 |
| IMG_ARC_NOFILL | 指明弧或弦只有輪廓,不填充 |
| IMG_ARC_EDGED | 指明用直線將起始和結束點與中心點相連 |
例子:
<?php header('Content-type: image/png');$im = imagecreatetruecolor(100, 100); $red = imagecolorallocate($im, 255, 0, 0);imagefilledarc($im, 50, 50, 100, 50, 0, 360 , $red, IMG_ARC_PIE);imagepng($im); imagedestroy($im); ?>該函數典型應用之一是畫餅狀統計圖。
imagefilledrectangle()
imagefilledrectangle() 函數畫一矩形并填充。
語法:
bool imagefilledrectangle( resource image, int x1, int y1, int x2, int y2, int color )x1,y1為左上角左邊,x2,y2為右下角坐標。
例子:
<?php header('Content-type: image/png');$im = imagecreatetruecolor(200, 200); $yellow = imagecolorallocate($im, 255, 255, 0);imagefilledrectangle($im, 20, 150, 40, 200, $yellow); imagefilledrectangle($im, 50, 80, 70, 200, $yellow);imagepng($im); imagedestroy($im); ?>該函數典型應用之一是柱狀統計圖。
imagefilledpolygon()
imagefilledpolygon() 函數畫一多邊形并填充。
語法:
bool imagefilledpolygon( resource image, array points, int num_points, int color )| image | 圖像資源,欲繪制多邊形的圖像 |
| points | 按順序包含有多邊形各頂點的 x 和 y 坐標的數組 |
| num_points | 頂點的總數,必須大于 3 |
| color | 圖像的顏色 |
繪制一個用紅色填充的六邊形例子:
<?php header('Content-type: image/png');$points = array(50, 50, // Point 1 (x, y)100, 50, // Point 2 (x, y)150, 100, // Point 3 (x, y)150, 150, // Point 4 (x, y)100, 150, // Point 5 (x, y)50, 100 // Point 6 (x, y));$im = imagecreatetruecolor(200, 200); $red = imagecolorallocate($im, 255, 0, 0);imagefilledpolygon($im, $points, 6, $red);imagepng($im); imagedestroy($im); ?>
- imagefill()?函數用于圖像區域填充。
- imagefilledarc()?函數畫一橢圓弧并填充。
- imagefilledrectangle()?函數畫一矩形并填充。
- imagefilledpolygon()?函數畫一多邊形并填充。
imagefill()
imagefill() 函數用于區域填充。
語法:
bool imagefill( resource image, int x, int y, int color )x,y 分別為填充的起始 x 坐標和 y 坐標,與 x, y 點顏色相同且相鄰的點都會被填充。
例子:
<?php header("Content-type: image/png");$im = @imagecreatetruecolor(200, 200); $red = imagecolorallocate($im, 255, 0, 0);//用 $red 顏色填充圖像 imagefill( $im, 0, 0, $red );imagepng($im); imagedestroy($im); ?>提示:對于用?imagecreate()?建立的圖像,第一次調用?imagecolorallocate()?會自動給圖像填充背景色。
imagefilledarc()
imagefilledarc() 函數畫一橢圓弧并填充。
語法:
bool imagefilledarc( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style )該函數參數用法可參考繪制橢圓弧函數?imagearc()?,只是本函數增加 style 參數表示填充方式。
| IMG_ARC_PIE | 普通填充,產生圓形邊界 |
| IMG_ARC_CHORD | 只是用直線連接了起始和結束點,與 IMG_ARC_PIE 方式互斥 |
| IMG_ARC_NOFILL | 指明弧或弦只有輪廓,不填充 |
| IMG_ARC_EDGED | 指明用直線將起始和結束點與中心點相連 |
例子:
<?php header('Content-type: image/png');$im = imagecreatetruecolor(100, 100); $red = imagecolorallocate($im, 255, 0, 0);imagefilledarc($im, 50, 50, 100, 50, 0, 360 , $red, IMG_ARC_PIE);imagepng($im); imagedestroy($im); ?>該函數典型應用之一是畫餅狀統計圖。
imagefilledrectangle()
imagefilledrectangle() 函數畫一矩形并填充。
語法:
bool imagefilledrectangle( resource image, int x1, int y1, int x2, int y2, int color )x1,y1為左上角左邊,x2,y2為右下角坐標。
例子:
<?php header('Content-type: image/png');$im = imagecreatetruecolor(200, 200); $yellow = imagecolorallocate($im, 255, 255, 0);imagefilledrectangle($im, 20, 150, 40, 200, $yellow); imagefilledrectangle($im, 50, 80, 70, 200, $yellow);imagepng($im); imagedestroy($im); ?>該函數典型應用之一是柱狀統計圖。
imagefilledpolygon()
imagefilledpolygon() 函數畫一多邊形并填充。
語法:
bool imagefilledpolygon( resource image, array points, int num_points, int color )| image | 圖像資源,欲繪制多邊形的圖像 |
| points | 按順序包含有多邊形各頂點的 x 和 y 坐標的數組 |
| num_points | 頂點的總數,必須大于 3 |
| color | 圖像的顏色 |
繪制一個用紅色填充的六邊形例子:
<?php header('Content-type: image/png');$points = array(50, 50, // Point 1 (x, y)100, 50, // Point 2 (x, y)150, 100, // Point 3 (x, y)150, 150, // Point 4 (x, y)100, 150, // Point 5 (x, y)50, 100 // Point 6 (x, y));$im = imagecreatetruecolor(200, 200); $red = imagecolorallocate($im, 255, 0, 0);imagefilledpolygon($im, $points, 6, $red);imagepng($im); imagedestroy($im); ?>- imagecopy()?函數用于拷貝圖像或圖像的一部分。
- imagecopyresized()?函數用于拷貝部分圖像并調整大小。
imagecopy()
imagecopy() 函數用于拷貝圖像或圖像的一部分,成功返回 TRUE ,否則返回 FALSE 。
語法:
bool imagecopy( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y,int src_w, int src_h )| dst_im | 目標圖像 |
| src_im | 被拷貝的源圖像 |
| dst_x | 目標圖像開始 x 坐標 |
| dst_y | 目標圖像開始 y 坐標,x,y同為 0 則從左上角開始 |
| src_x | 拷貝圖像開始 x 坐標 |
| src_y | 拷貝圖像開始 y 坐標,x,y同為 0 則從左上角開始拷貝 |
| src_w | (從 src_x 開始)拷貝的寬度 |
| src_h | (從 src_y 開始)拷貝的高度 |
例子:
<?php header("Content-type: image/jpeg");//創建目標圖像 $dst_im = imagecreatetruecolor(150, 150);//源圖像 $src_im = @imagecreatefromjpeg("images/flower_1.jpg");//拷貝源圖像左上角起始 150px 150px imagecopy( $dst_im, $src_im, 0, 0, 0, 0, 150, 150 );//輸出拷貝后圖像 imagejpeg($dst_im);imagedestroy($dst_im); imagedestroy($src_im); ?>imagecopyresized()
imagecopyresized() 函數用于拷貝圖像或圖像的一部分并調整大小,成功返回 TRUE ,否則返回 FALSE 。
語法:
bool imagecopyresized( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y,int dst_w, int dst_h, int src_w, int src_h )本函數參數可參看?imagecopy()?函數,只是本函數增加了兩個參數(注意順序):
imagecopyresized() 的典型應用就是生成圖片的縮略圖:
<?php header("Content-type: image/jpeg");//原圖文件 $file = "images/flower_1.jpg";// 縮略圖比例 $percent = 0.5;// 縮略圖尺寸 list($width, $height) = getimagesize($file); $newwidth = $width * $percent; $newheight = $height * $percent;// 加載圖像 $src_im = @imagecreatefromjpeg($file); $dst_im = imagecreatetruecolor($newwidth, $newheight);// 調整大小 imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);//輸出縮小后的圖像 imagejpeg($dst_im);imagedestroy($dst_im); imagedestroy($src_im); ?>上面的例子將原圖縮小為原來的一半尺寸。
- imagefill()?函數用于圖像區域填充。
- imagefilledarc()?函數畫一橢圓弧并填充。
- imagefilledrectangle()?函數畫一矩形并填充。
- imagefilledpolygon()?函數畫一多邊形并填充。
imagefill()
imagefill() 函數用于區域填充。
語法:
bool imagefill( resource image, int x, int y, int color )x,y 分別為填充的起始 x 坐標和 y 坐標,與 x, y 點顏色相同且相鄰的點都會被填充。
例子:
<?php header("Content-type: image/png");$im = @imagecreatetruecolor(200, 200); $red = imagecolorallocate($im, 255, 0, 0);//用 $red 顏色填充圖像 imagefill( $im, 0, 0, $red );imagepng($im); imagedestroy($im); ?>提示:對于用?imagecreate()?建立的圖像,第一次調用?imagecolorallocate()?會自動給圖像填充背景色。
imagefilledarc()
imagefilledarc() 函數畫一橢圓弧并填充。
語法:
bool imagefilledarc( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style )該函數參數用法可參考繪制橢圓弧函數?imagearc()?,只是本函數增加 style 參數表示填充方式。
| IMG_ARC_PIE | 普通填充,產生圓形邊界 |
| IMG_ARC_CHORD | 只是用直線連接了起始和結束點,與 IMG_ARC_PIE 方式互斥 |
| IMG_ARC_NOFILL | 指明弧或弦只有輪廓,不填充 |
| IMG_ARC_EDGED | 指明用直線將起始和結束點與中心點相連 |
例子:
<?php header('Content-type: image/png');$im = imagecreatetruecolor(100, 100); $red = imagecolorallocate($im, 255, 0, 0);imagefilledarc($im, 50, 50, 100, 50, 0, 360 , $red, IMG_ARC_PIE);imagepng($im); imagedestroy($im); ?>該函數典型應用之一是畫餅狀統計圖。
imagefilledrectangle()
imagefilledrectangle() 函數畫一矩形并填充。
語法:
bool imagefilledrectangle( resource image, int x1, int y1, int x2, int y2, int color )x1,y1為左上角左邊,x2,y2為右下角坐標。
例子:
<?php header('Content-type: image/png');$im = imagecreatetruecolor(200, 200); $yellow = imagecolorallocate($im, 255, 255, 0);imagefilledrectangle($im, 20, 150, 40, 200, $yellow); imagefilledrectangle($im, 50, 80, 70, 200, $yellow);imagepng($im); imagedestroy($im); ?>該函數典型應用之一是柱狀統計圖。
imagefilledpolygon()
imagefilledpolygon() 函數畫一多邊形并填充。
語法:
bool imagefilledpolygon( resource image, array points, int num_points, int color )| image | 圖像資源,欲繪制多邊形的圖像 |
| points | 按順序包含有多邊形各頂點的 x 和 y 坐標的數組 |
| num_points | 頂點的總數,必須大于 3 |
| color | 圖像的顏色 |
繪制一個用紅色填充的六邊形例子:
<?php header('Content-type: image/png');$points = array(50, 50, // Point 1 (x, y)100, 50, // Point 2 (x, y)150, 100, // Point 3 (x, y)150, 150, // Point 4 (x, y)100, 150, // Point 5 (x, y)50, 100 // Point 6 (x, y));$im = imagecreatetruecolor(200, 200); $red = imagecolorallocate($im, 255, 0, 0);imagefilledpolygon($im, $points, 6, $red);imagepng($im); imagedestroy($im); ?>- imagecolorallocate()?函數用于為圖像分配顏色。
- imagecolordeallocate()?函數用于取消先前由 imagecolorallocate() 等函數為圖像分配的顏色。
-
imagecolorallocate()
imagecolorallocate() 函數用于為圖像分配顏色,返回一個標識符,代表了由給定的 RGB 成分組成的顏色,如果分配失敗則返回 -1 。
語法:
int imagecolorallocate( resource image, int red, int green, int blue )參數 red,green 和 blue 分別是所需要的顏色的 紅,綠,藍 成分,取值范圍 0 - 255。
例子:
<?php header("Content-type: image/png");//創建圖像 $im = @imagecreate(200, 50) or die("創建圖像資源失敗");//圖片背景顏色并填充 $bg = imagecolorallocate($im, 204, 204, 204);//設定文字顏色 $red = imagecolorallocate($im, 255, 0, 0); //水平畫一行字 imagestring($im, 5, 0, 0, "Hello world!", $red);//以PNG格式輸出圖像 imagepng($im);//銷毀圖像資源 imagedestroy($im); ?>提示
- 對于用?imagecreate()?建立的圖像,第一次調用 imagecolorallocate() 會給圖像填充背景色(如上面例子)。
- 對于用?imagecreatetruecolor()?建立的圖像,則需要使用別的指令如?imagefill()?填充背景。
imagecolorallocatealpha()
imagecolorallocatealpha() 和 imagecolorallocate() 用法相同,但多了一個額外的透明度參數 alpha,其值從 0 到 127。0 表示完全不透明,127 表示完全透明。
語法:
int imagecolorallocatealpha( resource image, int red, int green, int blue, int alpha )imagecolordeallocate()
imagecolordeallocate() 函數用于取消先前由 imagecolorallocate() 和imagecolorallocatealpha() 函數為圖像分配的顏色。
語法:
bool imagecolordeallocate( resource image, int color )例子:
<? $im = @imagecreate(200, 50) or die("創建圖像資源失敗"); $bg = imagecolorallocate($im, 255, 0, 0); imagecolordeallocate($im, $bg); ?>
- imagefill()?函數用于圖像區域填充。
- imagefilledarc()?函數畫一橢圓弧并填充。
- imagefilledrectangle()?函數畫一矩形并填充。
- imagefilledpolygon()?函數畫一多邊形并填充。
imagefill()
imagefill() 函數用于區域填充。
語法:
bool imagefill( resource image, int x, int y, int color )x,y 分別為填充的起始 x 坐標和 y 坐標,與 x, y 點顏色相同且相鄰的點都會被填充。
例子:
<?php header("Content-type: image/png");$im = @imagecreatetruecolor(200, 200); $red = imagecolorallocate($im, 255, 0, 0);//用 $red 顏色填充圖像 imagefill( $im, 0, 0, $red );imagepng($im); imagedestroy($im); ?>提示:對于用?imagecreate()?建立的圖像,第一次調用?imagecolorallocate()?會自動給圖像填充背景色。
imagefilledarc()
imagefilledarc() 函數畫一橢圓弧并填充。
語法:
bool imagefilledarc( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style )該函數參數用法可參考繪制橢圓弧函數?imagearc()?,只是本函數增加 style 參數表示填充方式。
| IMG_ARC_PIE | 普通填充,產生圓形邊界 |
| IMG_ARC_CHORD | 只是用直線連接了起始和結束點,與 IMG_ARC_PIE 方式互斥 |
| IMG_ARC_NOFILL | 指明弧或弦只有輪廓,不填充 |
| IMG_ARC_EDGED | 指明用直線將起始和結束點與中心點相連 |
例子:
<?php header('Content-type: image/png');$im = imagecreatetruecolor(100, 100); $red = imagecolorallocate($im, 255, 0, 0);imagefilledarc($im, 50, 50, 100, 50, 0, 360 , $red, IMG_ARC_PIE);imagepng($im); imagedestroy($im); ?>該函數典型應用之一是畫餅狀統計圖。
imagefilledrectangle()
imagefilledrectangle() 函數畫一矩形并填充。
語法:
bool imagefilledrectangle( resource image, int x1, int y1, int x2, int y2, int color )x1,y1為左上角左邊,x2,y2為右下角坐標。
例子:
<?php header('Content-type: image/png');$im = imagecreatetruecolor(200, 200); $yellow = imagecolorallocate($im, 255, 255, 0);imagefilledrectangle($im, 20, 150, 40, 200, $yellow); imagefilledrectangle($im, 50, 80, 70, 200, $yellow);imagepng($im); imagedestroy($im); ?>該函數典型應用之一是柱狀統計圖。
imagefilledpolygon()
imagefilledpolygon() 函數畫一多邊形并填充。
語法:
bool imagefilledpolygon( resource image, array points, int num_points, int color )| image | 圖像資源,欲繪制多邊形的圖像 |
| points | 按順序包含有多邊形各頂點的 x 和 y 坐標的數組 |
| num_points | 頂點的總數,必須大于 3 |
| color | 圖像的顏色 |
繪制一個用紅色填充的六邊形例子:
<?php header('Content-type: image/png');$points = array(50, 50, // Point 1 (x, y)100, 50, // Point 2 (x, y)150, 100, // Point 3 (x, y)150, 150, // Point 4 (x, y)100, 150, // Point 5 (x, y)50, 100 // Point 6 (x, y));$im = imagecreatetruecolor(200, 200); $red = imagecolorallocate($im, 255, 0, 0);imagefilledpolygon($im, $points, 6, $red);imagepng($im); imagedestroy($im); ?>總結
- 上一篇: PHP GD库生成图像的几个函数总结
- 下一篇: 【python】简单实现一个模板引擎