<?php
header("Content-type: image/png");
$text = $_GET['t'];
$font = 'stxingka.ttf'; // TTF 字體文件
$fontsize = 30;
$box = imagettfbbox($fontsize, 0, $font, $text); // 獲取文本邊界
$dx = abs($box[2] - $box[0]) + 10;
$dy = abs($box[5] - $box[3]);
$im = imagecreate($dx, $dy);
imagecolorallocate($im, 255, 255, 255); // 背景色(白)
$fontcolor = imagecolorallocate($im, 255, 0, 0); // 字色(紅)
imagettftext($im, $fontsize, 0, 0, abs($box[5]), $fontcolor, $font, $text);
imagepng($im);
imagedestroy($im);
?>
進階美化:更換藝術字體、漸變背景圖、陰影/描邊、啟用快取機制避免重複渲染。
圖片 URL:<img src="color.php?t=xxx" />
參數 t 需 urlencode() 編碼,長度建議限制。編碼統一用 UTF-8。
建立 plugins/modifier.ubb.php:
<?php
function smarty_modifier_ubb($string) {
$ubb = array(
'/\[b\](.+?)\[\/b\]/i', // 粗體
'/\[url=(.+?)\](.+?)\[\/url\]/i', // 超連結
'/\[colorfont\](.+?)\[\/colorfont\]/ie' // 彩字(需 e 修飾符)
);
$tohtml = array(
'<b>\1</b>',
'<a href="\1">\2</a>',
'<img src="color.php?t=" . urlencode("\1") . " />'
);
return preg_replace($ubb, $tohtml, $string);
}
?>
內容中插入:[colorfont]文字[/colorfont]
模板輸出:{$content|ubb}
同理可擴展更多 UBB 標籤(字號、字體、閃爍等)。