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