« 67. PHP 的 MySQL 函數群整理 | (回到Blog入口) | 69. PHP 的檔案處理函數群整理 »

68. PHP 的影像繪圖函數群整理

jollen 發表於 October 27, 2006 7:19 PM

getimagesize() 函數

array getimagesize(string filename, array [imageinfo]);

getimagesize() 可取得下列圖檔的大小:

1. GIF
2. JPG
3. PNG
4. SWF
5. SWC
6. PSD
7. TIFF
8. BMP
9. IFF
10. JP2
11. JPX
12. JB2
13. JPC
14. XBM
15. WBMP

getimagesize() 所傳回的值是一個包含 4 個元素的陣列,每個元素所代表的意義如下:

1. key 0 - 圖檔的寬度 (pixels)
2. key 1 - 圖檔的高度 (pixels)
3. key 2 - 圖檔的格式 (如表 3.1)
4. key 3 - "height=xxx width=xxx" 的字串

getimagesize() 傳回的圖檔格式:

1 = GIF 2 = JPG 3 = PNG
4 = SWF 5 = PSD 6 = BMP
7 = TIFF
(intel byte order) 8 = TIFF
(motorola byte order) 9 = JPC
10 = JP2 11 = JPX 12 = JB2
13 = SWC 14 = IFF

可以用在 <IMG> 的標籤裡,例如:

<?php $size = getimagesize("image/flag.jpg"); ?> <IMG SRC="img/flag.jpg" <?=$size[3];?>>

加上 imageinfo 則可以讀取圖檔的 marker 資訊,傳回來的是一個 associative array,例如讀取最常使用的 APP13 marker:

<?php $size = getimagesize("flag.jpg", &$info); if (isset($info["APP13"])) { $iptc = iptcparse($info["APP13"]); echo $iptc; } ?>

iptcparse() 函數用來將陣列裡的二進位資料轉換成可讀取的型式。要注意,$info 是採用 call by reference 的方式來接受傳回值。

imagearc() 函數

int imagearc(int im, int cx, int cy, int w, int h, int s, int e, int col);

畫一段曲線。(cx,cy) 為曲線的中心,w 與 h 分別為曲線的寬度與長度,s 與 e 則分別為起點與終點的角度。

imagechar() 函數

int imagechar(int im, int font, int x, int y, string c, int col);

水平畫出指定的字元,參數 font 指定字元的字型,必須是安裝在系統裡並且有定義的字型。font 可以是 1-5 的數字,表示使用內建字型,1-5 則指定字型的大小。(x,y) 則是字元的右上角座標。

相關函數:imageloadfont()

imagecharup() 函數

int imagecharup(int im, int font, int x, int y, string c, int col);

垂直畫出指定的字元,參數 font 指定字元的字型,必須是安裝在系統裡並且有定義的字型。font 可以是 1-5 的數字,表示使用內建字型,1-5 則指定字型的大小。(x,y) 則是字元的右上角座標。

相關函數:imageloadfont()

imagecolorallocate() 函數

int imagecolorallocate(int im, int red, int green, int blue);

配置 image 的顏色,所有在 image 函數群裡用到的顏色都必須事先利用 imagecolorallocate() 函數配置過。例如:

<?php $im = imageCreate(400,30);
$red = imageColorAllocate($im, 255, 0, 0); $white = ImageColorAllocate($im, 255,255,255); $black = ImageColorAllocate($im, 0,0,0); ?>

imagecolortransparent() 函數

int imagecolortransparent(int im, int [col]);

定義 transparent 顏色 (透明色)。參數 col 則是利用 imagecolorallocate() 配置的顏色。

imagecopyresized() 函數

int imagecopyresized(int dst_im, int src_im, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH);

拷貝 image 的某一部份,並且可以重新指定其大小。拷貝的目地也可以是同一個 image 的其它地方。

imagecreate() 函數

int imagecreate(int x_size, int y_size);

建立一個新的空白 image。傳回一個 image 的指標,以後對這個 image 做任何動作時,都會使用到這個指標。

imagecreatefromgif()、imagecreatefromjpeg()、imagecreatefrompng() 函數

int ImageCreateFromGIF(string filename);

由 GIF 圖檔建立一個新的 image,該 GIF 圖檔必須是一個已存在的圖檔,檔名除了本地端檔案外也可以指定 URL 位置。GIF 檔可以當成新 image 的背景圖,利用 imageCreateFromGIF() 所建立的 image 其繪圖方式和利用 imagecreate() 建立的 image 完全相同。

相對於 ImageCreateFromGIF() 函數的還有 ImageCreateFromJPEG() 與 ImageCreateFromPNG() 兩個函數。

另外,PHP 4 還新增了底下 7 個相同系列的函數:

1. ImageCreateFromGD() 函數
2. ImageCreateFromGD2() 函數
3. ImageCreateFromGD2Part() 函數
4. ImageCreateFromWBMP() 函數
5. ImageCreateFromString() 函數
6. ImageCreateFromXBM() 函數
7. ImageCreateFromXPM() 函數

imageDashedLine() 函數

int imagedashedline(int im, int x1, int y1, int x2, int y2, int col);

畫一條 (x1, y1) 到 (x2, y2) 的虛線。

相關函數:imageline()

imageDestroy() 函數

int imagedestroy(int im);
歸還 image 所佔的記億體空間。

imageFill() 函數

int imagefill(int im, int x, int y, int col);

以 col 顏色填滿 (x, y) 裡的封閉區域。

imageFilledPolygon() 函數

int imagefilledpolygon(int im, array points, int num_points, int col);

繪製多邊形,並以 col 顏色填滿。參數 points 為多邊形的頂點座點,以陣列形式存放,格式如下:

points[0] = x0 points[1] = y0 points[2] = x1 points[3] = y1 . . .

參數 num_points 則是頂點的個數,例如 num_points = 10,則頂點存放於 points[0]~points[9]。

imageFilledRectangle() 函數

int imagefilledrectangle(int im, int x1, int y1, int x2, int y2, int col);

繪製一個長方形,並以 col 顏色填滿。(x1, y1) 為左上角座標,(x2, y2) 為右下角座標。

imageFillToBorder() 函數

int imagefilltoborder(int im, int x, int y, int border, int col);

填滿 (x,y) 裡以 col 顏色為邊緣的封閉區域。

imageFontHeight() 函數

int imagefontheight(int font);

取得指定字形的高度,傳回值的單位為 pixels。

相關函數:imagefontwidth(), imageloadfont()

imageFontWidth() 函數

int imagefontwidth(int font);

取得指定字形的寬度,傳回值的單位為 pixels。

相關函數:imagefontheight(), imageloadfont()

imageGIF()、ImagePNG()、ImageJPEG()、ImageWBMP()、ImageGD()、ImageGD2() 函數

int imagegif(int im, string filename);

輸出 im 參數所指定 image 到瀏覽器或檔案。

對 ImageGIF() 函數而言,如果 image 沒有指定 transparent,則輸出的圖檔格式為 GIF87a,否則輸出 GIF98a 格式的圖檔。

如果沒有指定檔案名稱,則直接輸出圖檔給瀏覽器。要注意一點,如果要直接輸出圖檔給瀏覽器,則必須先輸出圖檔的 MIME types 檔頭,例如 "Content type: image/gif" 的檔頭 (header) 代表 GIF 的影像。

其它相對於 ImageGIF() 的函數還有:ImagePNG()、ImageJPEG()、ImageWBMP()、ImageGD()、ImageGD2() 五個參數。

WBMP 是給 WAP 使用的圖檔格式。

imageInterlace() 函數

int imageinterlace(int im, int [interlace]);

打開或關閉 interlance。imageinterlance() 用來將 image 的 interlance bit 打開或關閉。如果 傳入的 interlance = 1,表示打開 interlance;如果 interlance = 0,表示關閉 interlance。

如果成功打開或關閉 interlance bit,則傳回 true。

imageLine() 函數

int imageline(int im, int x1, int y1, int x2, int y2, int col);

用 col 顏色,由 (x1, y1) 到 (x2, y2) 畫一條線。

相關函數:imagecreate(), imagecolorallocate()

imageLoadFont() 函數

int imageloadfont(string file);

載入字型。傳回 font 的指標,1-5 為內建字型的指標,因此傳回的指標值由 6 開始。imageloadfont() 只能載入 bimap 字型,而且字型格式有特別規定。如果要畫字,建議使用支援 TrueType 字型的 imagettftext() 函數。

相關函數:ImageFontWidth(), ImageFontHeight()

imagePolygon() 函數

int imagepolygon(int im, array points, int num_points, int col);

繪製多邊形。參數 points 為多邊形的頂點座點,以陣列形式存放,格式如下:

points[0] = x0 points[1] = y0 points[2] = x1 points[3] = y1 . . .

參數 num_points 則是頂點的個數,例如 num_points = 10,則頂點存放於 points[0]~points[9]。

相關函數:imagecreate()

imageRectangle() 函數

int imagerectangle(int im, int x1, int y1, int x2, int y2, int col);

繪製一個長方形,(x1, y1) 為左上角座標,(x2, y2) 為右下角座標。

imageSetPixel() 函數

int imagesetpixel(int im, int x, int y, int col);

畫點,以 col 顏色在 (x,y) 的地方畫一個點。

相關函數:imagecreate(), imagecolorallocate()

imageString() 函數

int imagestring(int im, int font, int x, int y, string s, int col);

水平繪製字串,與 ImageChar 不同的是,ImageString 可用來畫字串,而 ImageChar 只能畫字元,但兩個函數的參數所代表的意義是相同的。

font 也和 ImageChar 一樣,可使用 1-5 的內建字型。(x,y) 則是第一個字元的右上角座標。

相關函數:imageloadfont()

imageStringUp() 函數

int imagestringup(int im, int font, int x, int y, string s, int col);

垂直繪製字串,與 ImageChar 不同的是,ImageString 可用來畫字串,而 ImageChar 只能畫字元,但兩個函數的參數所代表的意義是相同的。

font 也和 ImageChar 一樣,可使用 1-5 的內建字型。(x,y) 則是第一個字元的右上角座標。

相關函數:imageloadfont()

imageSX() 函數

int imagesx(int im);

取得 image 的寬度,傳回值單位為 pixel。

相關函數:imagecreate(), imagesy()

imageSY() 函數

int imagesy(int im);

取得 image 的高度,傳回值單位為 pixel。

相關函數:imagecreate(), imagesx()

imageTTFBBox() 函數

array ImageTTFBBox(int size, int angle, string fontfile, string text);

計算繪製利用指定 TrueType 字型的字串所佔用的區塊大小。傳入參數如下:

1. text - 要計算的字串。
2. size - 字型大小。
3. angle - 指定畫字時的走向,單位為角度 (degree),例如 0 表示字串的走向為 3 點鐘方向。angle 以順時鐘方向漸增。
4. fontfile - TrueType 字型檔的檔名與路徑。

ImageTTFBBox() 傳回包含八個元素的陣列,元素存放的值依序為左上角座標、右上角座標、右下角座標與左下角座標,這四個座標不受 angle 參數影響,也就是 angle = 0 時的座標值。圖塊只有在畫出時才會受 angle 參數的影響。每個元素所代表的意義如下:

1. 0 - 左下角 X 座標
2. 1 - 左下角 Y 座標
3. 2 - 右下角 X 座標
4. 3 - 左下角 Y 座標
5. 4 - 右上角 X 座標
6. 5 - 右上角 Y 座標
7. 6 - 左上角 X 座標
8. 7 - 左上角 Y 座標

使用 ImageTTFBBox() 函數必須安裝 GD 與 FreeType 函式庫。

相關函數:ImageTTFText()

imageTTFText() 函數

array ImageTTFText(int im, int size, int angle, int x, int y, int col, string fontfile, string text);

使用 TrueType 字形畫字。ImageTTFText() 函數所接受的參數如下:

1. size - 字型大小。
2. (x,y) - 指定畫字的起始位置,即第一個字的左下角座標。
3. angle - 指定畫字時的走向,單位為角度 (degree),例如 0 表示字串的走向為 □ 3 點鐘方向。angle 以順時鐘方向漸增。
4. fontfile - TrueType 字型檔的檔名與路徑。

其他參數,text 傳入要畫的字串,col 則是畫字的顏色。ImageTTFText() 傳回一個包含 8 個元素的陣列,存放準備要畫出圖塊的四個角落的座標。元素存放的值依序為左上角座標、右上角座標、右下角座標與左下角座標,這 4 個座標不受 angle 參數影響,也就是 angle = 0 時的座標值。圖塊只有在畫出時才會受 angle 參數的影響。每個元素所代表的意義如下:

1. 0 - 左下角 X 座標
2. 1 - 左下角 Y 座標
3. 2 - 右下角 X 座標
4. 3 - 左下角 Y 座標
5. 4 - 右上角 X 座標
6. 5 - 右上角 Y 座標
7. 6 - 左上角 X 座標
8. 7 - 左上角 Y 座標

使用範例:

<?php Header("Content-type: image/jpg");
$im = imagecreate(500,30);
$black = ImageColorAllocate($im, 0,0,0); $white = ImageColorAllocate($im, 255,255,255);
//本範例使用 Windows 的 Arial 字型檔 ImageTTFText($im, 20, 0, 0, 20, $white, "C:/WINNT/Fonts/arial.ttf", "Hello World");
ImageJPEG($im, "test.jpg"); ImageDestroy($im); ?>

使用 ImageTTFText() 函數必須安裝 GD 與 FreeType 函式庫。

相關函數:ImageTTFBBox().

imageColorAt() 函數

int imagecolorat(int im, int x, int y);

取得點 (x, y) 顏色在調色盤裡的 index 值。

相關函數:imagecolorset(), imagecolorsforindex()

imageColorClosest() 函數

int imagecolorclosest(int im, int red, int green, int blue);

取得在 image 調色盤裡,與指定顏色最接近顏色的 index。

相關函數:imagecolorexact()

imageColorExact() 函數

int imagecolorexact(int im, int red, int green, int blue);

取回在 image調色盤裡指定顏色的 index。如果沒有這個顏色,則傳回 false。

相關函數:imagecolorclosest()

imageColorResolve() 函數

int imagecolorresolve(int im, int red, int green, int blue);

取得指定顏色的 index,如果沒有指定的顏色,則傳回最接近的一個。

相關函數:imagecolorclosest()

imageColorSet() 函數

bool imagecolorset(int im, int index, int red, int green, int blue);

設定調色盤 index 的顏色。

相關函數:imagecolorat()

imageColorsForIndex() 函數

array imagecolorsforindex(int im, int index);

取得調色盤 index 的顏色定義。傳回一個 associative array,包含 "red"、"green" 與 "blue" 三個 key,分別存放 index 顏色所表示的值。

相關函數:imagecolorat(), imagecolorexact()

imageColorsTotal() 函數

int imagecolorstotal(int im);

取得 image 調色盤的顏色數。

相關函數:imagecolorat() and imagecolorsforindex().

jpeg2wbmp()、png2wbmp() 函數

int jpeg2wbmp(string jpegname, string wbmpname, int d_height, int d_width, int threshold);

PHP 4 新增了 2 個用來設計 WAP 網頁很好的函數。

jpeg2wbmp() 函數將 JPEG 圖檔 (jpegname) 轉換成 WBMP 格式,存成 wbmpname 檔案,並改變大小為 d_height、d_width。png2wbmp() 則是相對於 jpeg2wbmp() 的函數。

PHP 4.0.5 以上版本才支援 jpeg2wbmp()、png2wbmp() 函數。並且需要 GD 1.8 以上的版本。

--jollen

版權聲明

請參閱頁面底部的 CC 授權條款。此外,Jollen's PHP 專欄禁止引用、修改與轉貼,如果您想與朋友分享的話,我們只允許網站連結(hyperlink)的形式。

發表一個評論

(如果你此前從未在此 Blog 上發表過評論,則你的評論必須在 Blog 主人驗證後才能顯示,請你耐心等候。)

| Top | Jollen's Forum |
Jollen's PHP 專欄採用 Attribution-NonCommercial-NoDerivs 2.5 授權條款
Copyright(c) 2001-2007,2010 www.jollen.org