另一個用於置中文字字串的函式。
它的作用:產生一個文字字串的 truecolor .png 圖像。圖像的大小將足以包含文字和 2 像素的邊框,並且文字將在其內置中。
它是從任何其他頁面呼叫的,如下所示...
<img src="linkImg.php?text=php.net is great&border=out" /> // 帶有 #&+"'\<> 的文字需要被跳脫,但我發現空格不會導致錯誤。我還沒有用其他任何語言測試過。
以下程式碼是名為 linkImg.php 的檔案
該檔案的設計目的不是在此處包含任何其他內容,並且開頭和結尾的 php 標籤應該分別是頁面的第一個和最後一個字元,因為外部的空白有時可能會有點問題。
<?php
$text = $_GET['text'];
$border = $_GET['border'];
$font = "fontpath"; $fontsize = font size; $box = imagettfbbox ($fontsize, 0, $font, $text);
$textW = $box[2] - $box[0];
$textH= $box[3]-$box[5];
$paddingx = 10;
$paddingy = 10;
$width = $textW+$paddingx;
$height= $textH+$paddingy;
$textx = $paddingx/2;
$texty = $height - $paddingy/2;
$shadoffx = 1;
$shadoffy = 1;
$img = imagecreatetruecolor($width,$height);
$white = imagecolorallocate($img,255,255,255);
$black = imagecolorallocate($img,0,0,0);
$lightgrey = imagecolorallocate($img,200,200,200);
$grey = imagecolorallocate($img,100,100,100);
$bgcol = imagecolorallocate($img,192,213,196); $fgcol = imagecolorallocate($img,243,104,88); imagefill($img,0,0,$bgcol);
imagettftext($img, $fontsize, 0, $textx+$shadoffx, $texty+$shadoffy, $grey, $font, $text);
imagettftext($img, $fontsize, 0, $textx, $texty, $fgcol, $font, $text);
if ($border == "out")
{
imageline ($img,0,0,$width,0,$white);imageline ($img,0,0,0,$height,$white);
imageline ($img,1,1,$width,1,$lightgrey);imageline ($img,1,1,1,$height-1,$lightgrey);
imageline ($img,0,$height-1,$width-1,$height-1,$black);imageline ($img,$width-1,$height-1,$width-1,0,$black);
imageline ($img,2,$height-2,$width-2,$height-2,$grey);imageline ($img,$width-2,$height-2,$width-2,2,$grey);
}
if ($border == "flat")
{
imageline ($img,0,0,$width,0,$white);imageline ($img,0,0,0,$height,$white);
imageline ($img,1,1,$width,1,$grey);imageline ($img,1,1,1,$height-1,$grey);
imageline ($img,0,$height-1,$width-1,$height-1,$white);imageline ($img,$width-1,$height-1,$width-1,0,$white);
imageline ($img,2,$height-2,$width-2,$height-2,$grey);imageline ($img,$width-2,$height-2,$width-2,2,$grey);
}
if ($border == "in")
{
imageline ($img,0,0,$width,0,$black);imageline ($img,0,0,0,$height,$black);
imageline ($img,1,1,$width,1,$grey);imageline ($img,1,1,1,$height-1,$grey);
imageline ($img,0,$height-1,$width-1,$height-1,$white);imageline ($img,$width-1,$height-1,$width-1,0,$white);
imageline ($img,2,$height-2,$width-2,$height-2,$lightgrey);imageline ($img,$width-2,$height-2,$width-2,2,$lightgrey);
}
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
?>
希望這能對某些人有所幫助!
-Patrick-