php(gd库)输出中文图像的转换函数
来源:互联网 发布:网络高手黑客怎么联系 编辑:程序博客网 时间:2024/04/28 06:31
作者信息:sadly()
文章信息:中级 初发日期:2001 11 关键字:gd 输出中文图像
来源链接:http://www.chinalinuxpub.com/read.htm?id=900
以下是一段gb2312 -> UTF-8 的函数,西西,我抄了 文章信息:中级 初发日期:2001 11 关键字:gd 输出中文图像
来源链接:http://www.chinalinuxpub.com/read.htm?id=900
摘要:
php(gd库)输出中文图像的转换函数
unicode -> UTF-8 的算法,所以实际比GB2312->unicode
没多多少东西,请大家注意调用gb2utf8()时不能中英文混用
程序需要的GB2312.txt文件
gb2utf8.php
//Program writen by sadly www.phpx.com
function gb2utf8($gb)
{
if(!trim($gb))
return $gb;
$filename="gb2312.txt";
$tmp=file($filename);
$codetable=array();
while(list($key,$value)=each($tmp))
$codetable[hexdec(substr($value,0,6))]=substr($value,7,6);
$utf8="";
while($gb)
{
if (ord(substr($gb,0,1))>127)
{
$this=substr($gb,0,2);
$gb=substr($gb,2,strlen($gb));
$utf8.=u2utf8(hexdec($codetable[hexdec(bin2hex($this))-0x8080]));
}
else
{
$gb=substr($gb,1,strlen($gb));
$utf8.=u2utf8(substr($gb,0,1));
}
}
$ret="";
for($i=0;$i $ret.=chr(substr($utf8,$i,3));
return $ret;
}
function u2utf8($c)
{
for($i=0;$i $str="";
if ($c < 0x80) {
$str.=$c;
}
else if ($c < 0x800) {
$str.=(0xC0 | $c>>6);
$str.=(0x80 | $c & 0x3F);
}
else if ($c < 0x10000) {
$str.=(0xE0 | $c>>12);
$str.=(0x80 | $c>>6 & 0x3F);
$str.=(0x80 | $c & 0x3F);
}
else if ($c < 0x200000) {
$str.=(0xF0 | $c>>18);
$str.=(0x80 | $c>>12 & 0x3F);
$str.=(0x80 | $c>>6 & 0x3F);
$str.=(0x80 | $c & 0x3F);
}
return $str;
}
?>
function gb2utf8($gb)
{
if(!trim($gb))
return $gb;
$filename="gb2312.txt";
$tmp=file($filename);
$codetable=array();
while(list($key,$value)=each($tmp))
$codetable[hexdec(substr($value,0,6))]=substr($value,7,6);
$utf8="";
while($gb)
{
if (ord(substr($gb,0,1))>127)
{
$this=substr($gb,0,2);
$gb=substr($gb,2,strlen($gb));
$utf8.=u2utf8(hexdec($codetable[hexdec(bin2hex($this))-0x8080]));
}
else
{
$gb=substr($gb,1,strlen($gb));
$utf8.=u2utf8(substr($gb,0,1));
}
}
$ret="";
for($i=0;$i $ret.=chr(substr($utf8,$i,3));
return $ret;
}
function u2utf8($c)
{
for($i=0;$i $str="";
if ($c < 0x80) {
$str.=$c;
}
else if ($c < 0x800) {
$str.=(0xC0 | $c>>6);
$str.=(0x80 | $c & 0x3F);
}
else if ($c < 0x10000) {
$str.=(0xE0 | $c>>12);
$str.=(0x80 | $c>>6 & 0x3F);
$str.=(0x80 | $c & 0x3F);
}
else if ($c < 0x200000) {
$str.=(0xF0 | $c>>18);
$str.=(0x80 | $c>>12 & 0x3F);
$str.=(0x80 | $c>>6 & 0x3F);
$str.=(0x80 | $c & 0x3F);
}
return $str;
}
?>
调用举例:通过GD输出 "中国" 两个汉字
example.php
//Header("Content-type: image/gif");
$im = imagecreate(400,300);
$bkg = ImageColorAllocate($im, 0,0,0);
$clr = ImageColorAllocate($im, 255,255,255);
$fnt = "d:/winnt/fonts/simhei.ttf";
include("gb2utf8.php");
$str = gb2utf8("中国");
ImageTTFText($im, 20, 0, 10, 20, $clr, $fnt, $str);
ImageGif($im);
ImageDestroy($im);
?>
$im = imagecreate(400,300);
$bkg = ImageColorAllocate($im, 0,0,0);
$clr = ImageColorAllocate($im, 255,255,255);
$fnt = "d:/winnt/fonts/simhei.ttf";
include("gb2utf8.php");
$str = gb2utf8("中国");
ImageTTFText($im, 20, 0, 10, 20, $clr, $fnt, $str);
ImageGif($im);
ImageDestroy($im);
?>
- php(gd库)输出中文图像的转换函数
- php(gd库)输出中文图像的转换函数
- [PHP] GD库(六):输出图像 imagegif 、imagejpeg 与 imagepng 函数
- PHP GD库生成图像的几个函数总结
- [PHP] GD库(三):获取图像信息 getimagesize 函数
- [PHP] GD库(五):载入图像 imagecreatefrom_gif_jpeg_png 系列函数
- php GD图像处理 中文乱码的问题
- [PHP] GD库(一):图像处理
- [PHP] GD库(四):创建图像、销毁图像 imagecreate 与 imagedestroy 函数
- PHP的GD库GD函数详解-获得信息
- PHP GD库相关图像生成和处理函数
- php之gd图像库
- PHP安装GD库后和图像相关的函数包括
- 运用php GD库的图像函数制作登录验证码图片
- php-GD库的函数(一)
- php-GD库的函数(二)
- [PHP] GD库(九)合并图像 imagecopymerge 函数(水印制作实例)
- [PHP] GD库(七):分配、取消图像颜色 imagecolorallocate 与 imagecolordeallocate 函数
- 常见硬件术语手册
- 如何禁用Windows屏保和电源管理
- 从XML到Java代码的数据绑定之一
- IMF后台管理集成系统功能升级
- PHP下GD安装全攻略
- php(gd库)输出中文图像的转换函数
- 代码还是要亲自动手写才行啊
- 从XML到Java代码的数据绑定之二
- Protel 99 快捷键
- 3721病毒杀除方法详解
- 如何在.net中实现文件下载
- Informix入门之——SQL函数整理
- PHP5的异常处理机制[5]--PHP5之前的错误处理--使用PEAR处理错误
- 我找到的INFORMIX 培训教材