/** * @desc 创建结果图片 * @param * @return file */ public function create_userimg() { $user_name = addslashes($this->input->get('name')); $user_name = urldecode($user_name); $user_score = (int)$this->input->get('score'); if (preg_match("/^[x7f-xff]+$/", $user_name)) { $user_name_len = mb_strlen($user_name); $user_name_len = ceil($user_name_len/3); $nickleft = 30+60*$user_name_len; }else{ $user_name_len = strlen($user_name); $nickleft = 120+13*$user_name_len; } $font = '.'.PATH_FONT.'/yahei.ttf'; $font_kaiti = '.'.PATH_FONT.'/SIMLI.TTF'; $size = 25; //$bgimg = 'resource/default/images/lohas/result_bg.png'; $bgimg = 'resource/default/images/shishang/result_show.png'; $nick = 'resource/default/images/shishang/shishang_nick.png'; $im = @imagecreatetruecolor(506,432) or die("Cannot Initialize new GD image stream"); $white = imagecolorallocate( $im, 255, 255, 255 ); imagefill( $im, 0, 0, $white ); imagecopy($im, $this->Image, 0, 0, 0, 0, 506, 432); $bgimg = imagecreatefrompng($bgimg); imagecopy($im,$bgimg,0,0,0,0,506,432); $nick = imagecreatefrompng($nick); imagecopy($im,$nick,$nickleft,136,0,0,77,14); $green=imagecolorallocate($im,68,160,28); $hei=imagecolorallocate($im,0,0,0); $bai=imagecolorallocate($im,255,255,255); if (preg_match("/^[x7f-xff]+$/", $user_name)) { imagettftext($im,'25',0,62,150,$hei,$font_kaiti,$user_name); }else{ imagettftext($im,'21',0,62,150,$hei,$font,$user_name); } imagettftext($im,'12',0,70,209,$green,$font,$text_1); imagettftext($im,'11',0,170,238,$green,$font,$text_2); imagettftext($im,'12',0,360,210,$green,$font,$score); imagettftext($im,'11',0,95,237,$green,$font,$user_vs); imagettftext($im,'10',0,330,340,$hei,$font,$date_text); header("Content-type: image/png"); imagepng($im); imagedestroy($im); }问题二: PHP中文换行问题,因为文字要写在图片里,类似css的自动换行, php对英文有wordwrap()函数支持换行,但不支持中文,下面函数解决了PHP中文换行问题 代码如下 /** * @desc GD库生成图片中文自动换行 * 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度 * */ public function autowrap($fontsize, $angle, $fontface, $string, $width) { $content = ""; // 将字符串拆分成一个个单字 保存到数组 letter 中 for ($i=0;$i<mb_strlen($string);$i++) { $letter[] = mb_substr($string, $i, 1); } foreach ($letter as $l) { $teststr = $content."".$l; $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr); // 判断拼接后的字符串是否超过预设的宽度 if (($testbox[2] > $width) && ($content !== "")) { $content .= "n"; } $content .= $l; } $content = mb_convert_encoding($content, "html-entities","utf-8" ); return $content; } 问题三: 我在调用数据库接口的时候,取XML中的CDATA数据 解析XML中带有<!CDATA>问题,“ <name><![CDATA[Lois & Clark]]></name>”, CDATA中的数据取不到值,simplexml_load_string函数解决问题 simplexml_load_string($response, SimpleXMLElement , LIBXML_NOCDATA); 代码如下 /** * @desc CURL提交XML返回食材库XMl数据 * @param STRING XML * XML */ private function do_curl_post($xmldata){ $url = SITE_URL.$this->shicai_interface; $header[] = "Content-type: text/xml"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $xmldata); $response = curl_exec($ch); $response = simplexml_load_string($response, SimpleXMLElement , LIBXML_NOCDATA); if(curl_errno($ch)){ print curl_error($ch); } curl_close($ch); return $response; }