caldson
新手上路

UID 107650
精华
0
积分 12
帖子 6
金钱 12 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2008-1-18
状态 离线
|
[广告]: Enom域名自助付费 自助注册 自助PUSH 主流域名COM等一律57.99元年
中文文字转换成图片遇到的问题
刚找到的一段代码,数字和英文都可以居中,怎么才能让中文也显示居中呢?
### Declare this script will be displayed as a PNG image.
header("Content-type: image/png");
####################### BEGIN USER EDITS #######################
$imagewidth = 400;
$imageheight = 40;
$fontsize = "28";
$fontangle = "0";
$font = "/usr/share/fonts/msttcorefonts/simhei.ttf";
$text = "12121212121";
$backgroundcolor = "ffffff";
$textcolor = "000000";
######################## END USER EDITS ########################
### Convert HTML backgound color to RGB
if( eregi( "([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $backgroundcolor, $bgrgb ) )
{$bgred = hexdec( $bgrgb[1] ); $bggreen = hexdec( $bgrgb[2] ); $bgblue = hexdec( $bgrgb[3] );}
### Convert HTML text color to RGB
if( eregi( "([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $textcolor, $textrgb ) )
{$textred = hexdec( $textrgb[1] ); $textgreen = hexdec( $textrgb[2] ); $textblue = hexdec( $textrgb[3] );}
### Create image
$im = imagecreate( $imagewidth, $imageheight );
### Declare image's background color
$bgcolor = imagecolorallocate($im, $bgred,$bggreen,$bgblue);
### Declare image's text color
$fontcolor = imagecolorallocate($im, $textred,$textgreen,$textblue);
### Get exact dimensions of text string
$box = @imageTTFBbox($fontsize,$fontangle,$font,$text);
### Get width of text from dimensions
$textwidth = abs($box[4] - $box[0]);
### Get height of text from dimensions
$textheight = abs($box[5] - $box[1]);
### Get x-coordinate of centered text horizontally using length of the image and length of the text
$xcord = ($imagewidth/2)-($textwidth/2)-2;
### Get y-coordinate of centered text vertically using height of the image and height of the text
$ycord = ($imageheight/2)+($textheight/2);
### Declare completed image with colors, font, text, and text location
imagettftext ( $im, $fontsize, $fontangle, $xcord, $ycord, $fontcolor, $font, iconv("gb2312", "UTF-8", $text) );
### Display completed image as PNG
imagepng($im);
### Close the image
imagedestroy($im);
|
|