php利用GD庫輸出中文的方法

來源:互聯網
上載者:User
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實體形式再使用.

範例程式碼:


pChart 範例程式碼:

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;}?>
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.