了解了gd庫函數的用途後,用它們來處理圖檔是很容易的,下面就是一個應用很廣泛的執行個體,在圖檔上添加字元,類似水印,代碼和詳解如下:
<?
/*
參數:
1.$bgimg : 背景圖檔
2.$x : 距離左邊的距離
3.$topy : 距離頂部的距離
4.$font : 要添加的字元串
5.$size : 字型的大小
6.$fonttype : 字型
7.$color : 字型顔色
8.$tofile: 生成的圖檔
9.$nameyycolor : 字元串的陰影顔色
10.$nameyysize : 陰影的寬度
11.$fontfile : 字型檔案
*/
function addchartopic( $bgimg, $x, $topy, $font, $size, $fonttype, $color, $tofile, $nameyycolor, $nameyysize, $fontfile )
{
$r = hexdec( substr( $color, 0, 2 ) );
/*hexdec()函數把十六進制轉換為十進制。*/
$g = hexdec( substr( $color, 2, 2 ) );
$b = hexdec( substr( $color, 4, 2 ) );
$im = imagecreatefrompng( $bgimg );
/*ImageCreateFromPNG本函數用來取出一張 PNG格式圖形,通當取出當背景或者基本的畫布樣本使用。參數 filename 可以是本地端的檔案,也可以是網絡的 URL位址。ImageCreateFromPNG傳回值為 PNG 的檔案代碼,可供其它的函數使用。本函數在 PHP 3.0.13 版之後才支援。
*/
$fontcolor = imagecolorallocate( $im, $r, $g, $b );
/*int imagecolorallocate ( resource image, int red, int green, int blue)
imagecolorallocate() 傳回一個辨別符,代表了由給定的 RGB 成分組成的顔色。image 參數是 imagecreate() 函數的傳回值。red,green 和 blue 分别是所需要的顔色的紅,綠,藍成分。這些參數是 0 到 255 的整數或者十六進制的 0x00 到 0xFF。imagecolorallocate() 必須被調用以建立每一種用在 image 所代表的圖像中的顔色。 */
$sy = imagesy( $im ) / $topy;
/*imagesx與imagesy-- 分别取得圖像寬度與高度。*/
$array = imagettfbbox( $size, 0, $fontfile, $font );
/*array imagettfbbox (int size, int angle, string fontfile, string text)
說明 :
此函式計算并傳回TrueType文字的區塊坐标。
text : 要被測量的字串。
size : 字型大小。
fontfile : TrueType字型檔的名稱(也可以是URL)。
angle : text将要測量的角度度數。
ImageTTFBBox( )傳回的陣列有8個元素,代表文字區塊的四個頂點。
0 左下角X坐标
1 左下角Y坐标
2 右下角X坐标
3 右下角Y坐标
4 右上角X坐标
5 右上角Y坐标
6 左上角X坐标
7 左上角Y坐标
頂點是相對于text,是以不管角度。"左上角"的意思是,以水準的方向看文字時的左上角。
*/
$fontsy = ( $array[0] - $array[5] ) / 2;
$jiaozhen = $fontsy * 0.3;
$y = $sy + $fontsy - $jiaozhen;
if ( $x == "-1" )
{
$sx = imagesx( $im ) / 2;
$array = imagettfbbox( $size, 0, $fontfile, $font );
$fontsx = ( $array[0] - $array[2] ) / 2;
$x = $sx + $fontsx;
}
$r1 = hexdec( substr( $nameyycolor, 0, 2 ) );
$g1 = hexdec( substr( $nameyycolor, 2, 2 ) );
$b1 = hexdec( substr( $nameyycolor, 4, 2 ) );
$yycolor = imagecolorallocate( $im, $r1, $g1, $b1 );
if ( 2 < $nameyysize )
{
imagettftext( $im, $size, 0, $x + 3, $y + 3, $yycolor, $fontfile, $font );
/*imagettftext (image,size,angle, x, y,color,fontfile,text)
意思是 imagettftext() 将字元串 text 畫到 image 所代表的圖像上,從坐标 x,y(左上角為 0, 0)開始,角度為 angle,顔色為 color,使用 fontfile 所指定的 TrueType 字型檔案。根據 PHP 所使用的 GD 庫的不同,如果 fontfile 沒有以 '/'開頭,則 '.ttf' 将被加到檔案名之後并且會搜尋庫定義字型路徑。
由 x,y 所表示的坐标定義了第一個字元的基本點(大概是字元的左下角)。這和 imagestring() 不同,其 x,y 定義了第一個字元的右上角。
angle 以角度表示,0 度為從左向右閱讀文本(3 點鐘方向),更高的值表示逆時針方向(即如果值為 90 則表示從下向上閱讀文本)。
fontfile 是想要使用的 TrueType 字型的檔案名。
text 是文本字元串,可以包含 UTF-8 字元序列(形式為:{)來通路字型中超過前 255 個的字元。
color 是顔色的索引值。使用某顔色索引值的負值具有關閉防混色的效果*/
}
if ( 1 < $nameyysize )
{
imagettftext( $im, $size, 0, $x + 2, $y + 2, $yycolor, $fontfile, $font );
}
if ( 0 < $nameyysize )
{
imagettftext( $im, $size, 0, $x + 1, $y + 1, $yycolor, $fontfile, $font );
}
imagettftext( $im, $size, 0, $x, $y, $fontcolor, $fontfile, $font );
imagepng( $im, $tofile );/*本函數用來建立一張 PNG 格式圖形。參數 im 為使用 ImageCreate() 所建立的圖檔代碼。參數 filename 可省略,若無本參數 filename,則會将圖檔指接送到浏覽器端,記得在送出圖檔之前要先送出使用 Content-type: image/png 的标頭字元串 (header) 到浏覽器端,以順利傳輸圖檔。本函數在 PHP 3.0.13 版之後才支援。 */
imagedestroy( $im );
/*bool imagedestroy ( resource image )
imagedestroy() 釋放與 image 關聯的記憶體
*/
chmod( $tofile, 438 );
}
addchartopic( "test.png",50,20,"I'm here as always",15, "arial", "ffffff", "test1.png", "cccccc",5, "arial.ttf" );
echo "<h2>*This is the original png image:</h2>";
echo "<hr><center><img src=test.png ;
echo "<h2>*This is the new png image:</h2>";
echo "<hr><center><img src=test1.png ;
?>