|
楼主 |
发表于 2007-4-8 10:37
|
显示全部楼层
因为GD函数只接受UTF8格式编码的文字,所以在写文字前首先要进行编码转换。PHP自带的iconv和mbstring库都可以完成这项工作,
刚搜了下,原来是这样...
顺带把作者写的转换代码帖上:
- <?php
- /*****************************************************************
- * excode.php
- * @author 风舞
- * @email wjiujun (at) gmail.com
- * @note 一个中文附加码
- *****************************************************************/
- $RANDCODE=array('宠','辱','不','惊','静','看','庭','前','花','开','花','落','得','失','无','意','漫','望','天','外','云','卷','云','舒');
- $CODETABLE=array();
- $fp=fopen("gb2312.txt","r");
- while($line=fgets($fp))
- $CODETABLE[hexdec(substr($line,0,6))]=substr($line,7,6);
- fclose($fp);
- //gb2312转utf8
- function gb2utf8($gbstr)
- {
- global $CODETABLE;
- if(trim($gbstr)=="")
- return $gbstr;
- $ret="";
- $utf8="";
- while($gbstr)
- {
- if(ord(substr($gbstr,0,1))>127)
- {
- $thisW=substr($gbstr,0,2);
- $gbstr=substr($gbstr,2,strlen($gbstr));
- $utf8="";
- @$utf8=u2utf8(hexdec($CODETABLE[hexdec(bin2hex($thisW))-0x8080]));
- if($utf8!="")
- for($i=0;$i<strlen($utf8);$i+=3)
- $ret.=chr(substr($utf8,$i,3));
- }
- else
- {
- $ret.=substr($gbstr,0,1);
- $gbstr=substr($gbstr,1,strlen($gbstr));
- }
- }
- return $ret;
- }
- //unicode转utf8
- function u2utf8($c)
- {
- $str="";
- if($c<0x80)
- $str.=$c;
- elseif($c<0x800)
- {
- $str.=(0xC0|$c>>6);
- $str.=(0x80|$c&0x3F);
- }
- elseif($c<0x10000)
- {
- $str.=(0xE0|$c>>12);
- $str.=(0x80|$c>>6&0x3F);
- $str.=(0x80|$c&0x3F);
- }
- elseif($c<0x200000)
- {
- $str.=(0xF0|$c>>18);
- $str.=(0x80|$c>>12&0x3F);
- $str.=(0x80|$c>>6&0x3F);
- $str.=(0x80|$c&0x3F);
- }
- return $str;
- }
- //生成附加码
- function create_excode($length)
- {
- global $RANDCODE;
- header("content-type: image/png");
- $image_x=$length*30; //图片宽度
- $image_y=40; //图片高度
- $noise_num=80*$length; //杂点数量
- $line_num=$length-2; //干扰线数量
- $image=imagecreate($image_x,$image_y);
- imagecolorallocate($image,0xff,0xff,0xff); //设定背景颜色
- $rectangle_color=imagecolorallocate($image,0xAA,0xAA,0xAA); //边框颜色
- $noise_color=imagecolorallocate($image,0x00,0x00,0x00); //杂点颜色
- $font_color=imagecolorallocate($image,0x00,0x00,0x00); //字体颜色
- $line_color=imagecolorallocate($image,0x33,0x33,0x33); //干扰线颜色
- //加入杂点
- for($i=0;$i<$noise_num;$i++)
- imagesetpixel($image,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color);
- $font_face="simkai.ttf"; //字体
- $x=2;
- $session_code='';
- for($i=0;$i<$length;$i++)
- {
- $code=$RANDCODE[mt_rand(0,count($RANDCODE)-1)];
- imagettftext($image,18,mt_rand(-6,6),$x,29,$font_color,$font_face,gb2utf8($code));
- $x+=30;
- $session_code.=$code;
- }
- @session_start();
- $_SESSION['excode']=$session_code; //把附加码的值放在session中
- //加入干扰线
- for($i=0;$i<$line_num;$i++)
- imageline($image,mt_rand(0,$image_x),mt_rand(0,$image_y),
- mt_rand(0,$image_x),mt_rand(0,$image_y),$line_color);
- imagerectangle($image,0,0,$image_x-1,$image_y-1,$rectangle_color); //加个边框
- imagepng($image);
- imagedestroy($image);
- }
- create_excode(6);
- ?>
复制代码
作者还说到:
PHP自带的iconv和mbstring库都可以完成这项工作,但一般的虚拟主机很少支持,所以要自己写个,像这里用gb2utf8完成这项工作。当然如果你的页面就是以UTF8格式存储的,就可以省去这些额外工作。
但之前我就是用UTF-8保存php文件才显示不了图像,才有此帖...
出处:http://phpweblog.net/ |
|