Tommy 碎碎念

Tommy Wu's blog

« 上一篇 | 下一篇 »

讓 PhpBB2 也用中文數字吧! Part II
post by tommy @ 15 三月, 2007 12:09
加上動態使用背景圖案並把顯示的文字做些旋轉.

請參考前面提到的這篇文章, 並在 chinese_img 的目錄下頭, 再建立一個子目錄 backgrounds, 然後把你所要使用的背景圖案的 gif 檔案, 放到這個目錄下頭.

接著把 usercp_captcha_chinese.php 改成下面這個樣子:

<?php
 
if ( !defined('IN_PHPBB') )
{
die('Hacking attempt');
exit;
}
// Do we have an id? No, then just exit
if (empty($HTTP_GET_VARS['id']))
{
exit;
}
 
$confirm_id = htmlspecialchars($HTTP_GET_VARS['id']);
 
if (!preg_match('/^[A-Za-z0-9]+$/', $confirm_id))
{
$confirm_id = '';
}
 
$gd = true;
if(!extension_loaded("gd")){
//Gd isn't loaded
//Display error messages always, or not?
$gd = false;
}
if(!function_exists("gd_info") || !function_exists('imagettftext')){
//GD function, gd_info don't exists. GD isn't loaded correctly.
//Freetype function imagettftext doesn't exists. This is required by this mod.
$gd = false;
}
if($gd === false){
//GD not loaded, or one of the needed functions aren't there. Require usercp_confirm ;)
require($phpbb_root_path . 'includes/usercp_confirm.' . $phpEx);
die;
}
 
// Try and grab code for this id and session
$sql = 'SELECT code
FROM '
. CONFIRM_TABLE . "
WHERE session_id = '"
. $userdata['session_id'] . "'
AND confirm_id = '$confirm_id'"
;
$result = $db->sql_query($sql);
 
// If we have a row then grab data else create a new id
if ($row = $db->sql_fetchrow($result))
{
$db->sql_freeresult($result);
$code = $row['code'];
}
else
{
exit;
}
 
if (function_exists('ImageRotate'))
$size = 36;
else
$size = 24;
 
$bg_files = glob($phpbb_root_path.'includes/chinese_img/backgrounds/*.gif');
$bg_file = $bg_files[rand(0, count($bg_files)-1)];
 
$len = strlen($code);
$im = ImageCreate(24 * $len, 24);
$background = ImageColorAllocate($im, 255, 255, 255);
 
$im_bg = @ImageCreateFromGIF($bg_file);
if ($im_bg) {
list($width, $height, $type, $attr) = @GetImageSize($bg_file);
if (function_exists('ImageCopyReSampled'))
ImageCopyReSampled($im, $im_bg, 0, 0, 0, 0, 24 * $len, 24, $width, $height);
else
ImageCopyReSized($im, $im_bg, 0, 0, 0, 0, 24 * $len, 24, $width, $height);
ImageDestroy($im_bg);
}
 
for ($i = 0; $i < $len; $i++) {
$im_num = @ImageCreateFromPNG($phpbb_root_path . 'includes/chinese_img/'.$code[$i].'.png');
if (function_exists('ImageRotate')) {
$im_rotate = ImageRotate($im_num, rand(-45, 45), 0);
ImageDestroy($im_num);
$im_num = ImageCreate($size, $size);
if (function_exists('ImageCopyReSampled'))
ImageCopyReSampled($im_num, $im_rotate, 0, 0, 0, 0, $size, $size, imagesx($im_rotate), imagesy($im_rotate));
else
ImageCopyReSized($im_num, $im_rotate, 0, 0, 0, 0, $size, $size, imagesx($im_rotate), imagesy($im_rotate));
ImageDestroy($im_rotate);
}
if ($im_num) {
ImageColorTransparent($im_num, ImageColorAt($im_num, 0, 0));
ImageCopy($im, $im_num, 24 * $i, 0, 0, 0, 24, 24);
ImageDestroy($im_num);
}
}
 
header('content-type:image/png');
header('Cache-control: no-cache, no-store');
 
ImagePNG($im);
ImageDestroy($im);
die;
 
?>

這樣子就應該可以了.

對了, 如果你的系統產生的字並沒有旋轉, 表示你的系統的 PHP 不支援 ImageRotate() 這個函式 (例如 Debian 本身內建). 如果你一定要使用這個功能, 就必須自行編譯一份有含這個功能的 php gd 模組 (在 Debian 中, 把 rules 的

--with-gd=shared,/usr 

改成

--with-gd=shared 

重新編譯後, 把 gd.so 覆蓋原本的就可以了.

Del.icio.us Furl HEMiDEMi Technorati MyShare
迴響
暱稱:
標題:
個人網頁:
電子郵件:
authimage

迴響

  

Bad Behavior 已經阻擋了 57 個過去 7 天試圖闖關的垃圾迴響與引用。
Power by LifeType. Template design by JamesHuang. Valid XHTML and CSS