玩轉映像函數庫—常見圖形操作(PHP)

來源:互聯網
上載者:User
函數|圖形

我盡量不說大理論,諸如什麼是png,自己查解決.

PHP自4.3版本開始,捆綁了自己的GD2庫,使用者可以自己下載並設定.如果要查看自己的php版本是否支援gd模組(支援JPEG,PNG,WBMP但不再支援GIF),如下方式是一種方法:

if(!function_exists('imagecreate')) {
die('本伺服器不支援GD模組');
}

如果不支援的話,如何配置 ? 下載gd模組的dll檔案,修改php.ini,重啟伺服器即可.

以下簡稱PHP作圖為PS.

當您打算 PS的話,應該完成如下如下步驟,這是必經的.

1:建立基本PS對象(我假設為$image),填充背景(預設黑),以後的全部ps操作都是基於這個背景映像的.
2:在$image上作圖.
3:輸出這個映像.
4:銷毀對象,清除使用記憶體.

首先,我們來認識幾個常用的函數,這些函數在php手冊裡面都有詳細介紹,此處大體引用下.

resource imagecreate ( int x_size, int y_size )
imagecreate() 返回一個映像標識符,代表了一幅大小為 x_size 和 y_size 的空白映像。
此函數基本同imagetruecolor($width,$height).

int imagecolorallocate ( resource image, int red, int green, int blue )
imagecolorallocate() 返回一個標識符,代表了由給定的 RGB 成分組成的顏色。image 參數是 imagecreatetruecolor() 函數的傳回值。red,green 和 blue 分別是所需要的顏色的紅,綠,藍成分。這些參數是 0 到 255 的整數或者十六進位的 0x00 到 0xFF。imagecolorallocate() 必須被調用以建立每一種用在 image 所代表的映像中的顏色。

bool imagefill ( resource image, int x, int y, int color )
imagefill() 在 image 映像的座標 x,y(映像左上方為 0, 0)處用 color 顏色執列區域填充(即與 x, y 點顏色相同且相鄰的點都會被填充)。


bool imageline ( resource image, int x1, int y1, int x2, int y2, int color )
imageline() 用 color 顏色在映像 image 中從座標 x1,y1 到 x2,y2(映像左上方為 0, 0)畫一條線段。

bool imagestring ( resource image, int font, int x, int y, string s, int col )
imagestring() 用 col 顏色將字串 s 畫到 image 所代表的映像的 x,y 座標處(這是字串左上方座標,整幅映像的左上方為 0,0)。如果 font 是 1,2,3,4 或 5,則使用內建字型。

array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text )
本函數比較重要,參數較多,此處不再列出,它主要是寫字到映像上,和上面的函數類似,但必前者強大.

bool imagefilltoborder ( resource image, int x, int y, int border, int color )
imagefilltoborder() 從 x,y(映像左上方為 0, 0)點開始用 color 顏色執列區域填充,直到碰到顏色為 border 的邊界為止。【註:邊界內的所有顏色都會被填充。如果指定的邊界色和該點顏色相同,則沒有填充。如果映像中沒有該邊界色,則整幅映像都會被填充。】

bool imagefilledellipse ( resource image, int cx, int cy, int w, int h, int color )
imagefilledellipse() 在 image 所代表的映像中以 cx,cy(映像左上方為 0, 0)為中心畫一個橢圓。w 和 h 分別指定了橢圓的寬和高。橢圓用 color 顏色填充。如果成功則返回 TRUE,失敗則返回 FALSE。

輸出映像資料:imagepng($image[,$filename])

例一:輸出藍色背景和交叉白線的圖形

<?php
$width=35;
$height=35;
//建立對象
$image=imagecreate($width,$height);
//提取顏色
$color_white=imagecolorallocate($image,255,255,255);//白色
$color_blue=imagecolorallocate($image,0,0,108);//藍色
imagefill($image,0,0,$color_blue);
//作圖
 //線寬
imagesetthickness($image,3);
imageline($image,0,0,$width,$height ,$color_white);
imageline($image,$width,0,0,$height ,$color_white);

//發送對象至頭
header('content-type:image/png');
imagepng($image);
 /*
 //發送對象至檔案
 $filename="ex1.png";
 imagepng($image,$filename);
 */
//銷毀對象
imagedestroy($image);
?>

輸出圖象:

線上示範: http://www.phzzy.org/temp/5do8/ex1.php

[1] [2] [3] 下一頁  



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.