php利用GD库输出中文的方法
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; } ?>

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
