PHP中phpGD库输出中文的秘诀
找到这篇文章不容易,终于解决了我的问题。简单说就一句话,mb_convert_encoding($markWords,"html-entities","UTF-8")
开始时网上找到的方法是:去掉编译选项“--enable-gd-jis-conv” ,但我用的windows,不是linux,GD库都是编译好的东东,又不好重新编译,
到处找关闭GD库中 “JIS-mapped Japanese Font Support” 的方法,都没找到。最后看到 了这个,作者的文章超赞,谢谢作者。
/************************************下面是原文内容********************************************/
一个项目用到图表程序pChart2 然而输出的汉字竟然是乱码,换了其他图表程序(JpChart)也是一样,
于是直接写了一个简单使用GD库的测试程序,输出的也是乱码,证明确实是GD库函数问题
搜索了一下,说是由于GD库编译时添加了: --enable-gd-jis-conv 的支持( GD: Enable JIS-mapped Japanese font ) , 在phpinfo()里也可以看到:JIS-mapped Japanese Font Support的支持为enable
还需要重新编译,我觉得太麻烦了,应该会有更好的解决办法。
就是将要处理的汉字,先由UTF8的汉字转成html实体形式再使用.
示例代码:
<?php //ini_set('JIS-mapped Japanese Font Support',false); $pic=imagecreate(250,30); $black=imagecolorallocate($pic,0,0,0); $white=imagecolorallocate($pic,255,255,255); $font="C://WINDOWS//Fonts//simhei.ttf"; $str = '中华人民共和国'; $str = mb_convert_encoding($str, "html-entities","utf-8" );//结 果:哪一队优先开球 imagettftext($pic,10,0,10,20,$white,$font,$str); header("Content-type: image/jpeg"); $filename='./photo.jpg'; $im=imagecreatefromjpeg($filename); imagecopymerge($im,$pic,0,0,0,0,250,30,50); imagejpeg($im); ?>
pChart 示例代码:
<?php /* Example14: A smooth flat pie graph */ // Standard inclusions header("content-type:text/html; charset=utf-8"); include_once("pChart/pData.class.php"); include_once("pChart/pChart.class.php"); // Dataset definition $DataSet = new pData; $DataSet->AddPoint(array(10,2,3,5,3),"Serie1"); $DataSet->AddPoint(iconv_arr(array("Jan","二月","三月","Apr","May")),"Serie2"); $DataSet->AddAllSeries(); $DataSet->SetAbsciseLabelSerie("Serie2"); // Initialise the graph $Test = new pChart(300,200); $Test->loadColorPalette("Sample/softtones.txt"); $Test->drawFilledRoundedRectangle(7,7,293,193,5,240,240,240); $Test->drawRoundedRectangle(5,5,295,195,5,230,230,230); // Draw the pie chart $Test->setFontProperties("Fonts/simkai.ttf",8); $Test->drawBasicPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),120,100,70,PIE_PERCENTAGE,255,255,218); $Test->drawPieLegend(230,15,$DataSet->GetData(),$DataSet->GetDataDescription(),250,250,250); $Test->Render("example14.png"); /** * 将文本由UTF8编码转化为数字形式编码(HTML实体) * @param $arr 该参数可以为数组或者string * @author Steven lxq70361@qq.com */ function iconv_arr($arr){ if(is_array($arr)){ foreach($arr as $k=>$v){ $arr[$k] = iconv_arr($v); } }else{ $arr = mb_convert_encoding($arr, "html-entities","utf-8" ); } return $arr; } ?>

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools
