首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

Smarty UBB 插件實現彩色字體特效

     編輯:PHP 相關知識

一、彩字基本實現原理(GD 動態生成圖片)

<?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。

三、Smarty UBB 修飾符插件

建立 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 標籤(字號、字體、閃爍等)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved