php生成图片时报错:Warning: imagettftext() [function.imagettftext]: any2eucjp()解决方法

php生成图片时报错:Warning: imagettftext [function.imagettftext]: any2eucjp解决方法

报错原因

我们在使用imggettftext 函数的时候会遇到这个问题

imagettftext(): any2eucjp(): invalid code in input string

一切都没有错 这个是php的一个bug

https://bugs.php.net/bug.php?id=42218

原因 编译php的时候 --enable-gd-jis-conv 开启这个选项

这个是对 日文的支持

即使我们使用的 utf8 编码 (imagettftext 只支持 utf8 编码) 但是还是会被识别成 日文(日文同样是方块字)

不修改php参数的情况下还没找到解决方案

目前的办法只能是 重新编译 php 取消 --enable-gd-jis-conv 这个参数

解决方法

首先检查你的字体是否存在,路径是否正确。然后在看下面的。

然后看gd库中的JIS-mapped Japanese FontSupport 是否是enabled,如果是这样的话就需要转码了:

mb_convert_encoding($markWords,“html-entities”,“UTF-8”)

或者去掉编译选项“–enable-gd-jis-conv” 。

就是将要处理的汉字,先由UTF8的汉字转成html实体形式再使用.

由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");//结 果:&#21738;&#19968;&#38431;&#20248;&#20808;&#24320;&#29699;
     
    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 示例代码

 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;
    }
    ?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值