Home  >  Article  >  Backend Development  >  Solve the GD Chinese garbled problem_PHP tutorial

Solve the GD Chinese garbled problem_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:56:581069browse

今天仔细研究了下GD的一些相关技术,顺手也研究下GD中文乱码的问题。

  使用GD库输出中文字符串,调用imagestring是没有用的。需要使用imagettftext()函数。imagettftext函数的具体使用就参考手册啦。

  下面给个使用实例:

   

<span style="COLOR: #000000"><br>$pic=imagecreate(250,30);  <br>$black=imagecolorallocate($pic,0,0,0);  <br>$white=imagecolorallocate($pic,255,255,255);  <br>$font="C://WINDOWS//Fonts//simhei.ttf";  //这里的路进需要注意下,必须是字符的路径 <br>$str ='php'.iconv('gb2312','utf-8','面对对象')." www.phpobject.net";  <br>imagettftext($pic,10,0,10,20,$white,$font,$str); <br>      </span>


    前面我给出一个简单的GD水印实例,只举例说明了使用图片如何水印的,这里给出一个文字水印的简单代码。

 
<span style="COLOR: #000000"><br><span style="COLOR: #0000bb"><?php <BR>$pic</SPAN><SPAN style="COLOR: #007700">=</SPAN><SPAN style="COLOR: #0000bb">imagecreate</SPAN><SPAN style="COLOR: #007700">(</SPAN><SPAN style="COLOR: #0000bb">250</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">30</SPAN><SPAN style="COLOR: #007700">); <BR></SPAN><SPAN style="COLOR: #0000bb">$black</SPAN><SPAN style="COLOR: #007700">=</SPAN><SPAN style="COLOR: #0000bb">imagecolorallocate</SPAN><SPAN style="COLOR: #007700">(</SPAN><SPAN style="COLOR: #0000bb">$pic</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">0</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">0</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">0</SPAN><SPAN style="COLOR: #007700">); <BR></SPAN><SPAN style="COLOR: #0000bb">$white</SPAN><SPAN style="COLOR: #007700">=</SPAN><SPAN style="COLOR: #0000bb">imagecolorallocate</SPAN><SPAN style="COLOR: #007700">(</SPAN><SPAN style="COLOR: #0000bb">$pic</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">255</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">255</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">255</SPAN><SPAN style="COLOR: #007700">); <BR></SPAN><SPAN style="COLOR: #0000bb">$font</SPAN><SPAN style="COLOR: #007700">=</SPAN><SPAN style="COLOR: #dd0000">"C://WINDOWS//Fonts//simhei.ttf"</SPAN><SPAN style="COLOR: #007700">; <BR></SPAN><SPAN style="COLOR: #0000bb">$str </SPAN><SPAN style="COLOR: #007700">=</SPAN><SPAN style="COLOR: #dd0000">'php'</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">iconv</SPAN><SPAN style="COLOR: #007700">(</SPAN><SPAN style="COLOR: #dd0000">'gb2312'</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #dd0000">'utf-8'</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #dd0000">'面对对象'</SPAN><SPAN style="COLOR: #007700">).</SPAN><SPAN style="COLOR: #dd0000">" www.phpobject.net"</SPAN><SPAN style="COLOR: #007700">; <BR></SPAN><SPAN style="COLOR: #0000bb">imagettftext</SPAN><SPAN style="COLOR: #007700">(</SPAN><SPAN style="COLOR: #0000bb">$pic</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">10</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">0</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">10</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">20</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">$white</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">$font</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">$str</SPAN><SPAN style="COLOR: #007700">); <br><br></SPAN><SPAN style="COLOR: #0000bb">header</SPAN><SPAN style="COLOR: #007700">(</SPAN><SPAN style="COLOR: #dd0000">"Content-type: image/jpeg"</SPAN><SPAN style="COLOR: #007700">); <BR></SPAN><SPAN style="COLOR: #0000bb">$filename</SPAN><SPAN style="COLOR: #007700">=</SPAN><SPAN style="COLOR: #dd0000">'../src/images/photo.jpg'</SPAN><SPAN style="COLOR: #007700">; <BR></SPAN><SPAN style="COLOR: #0000bb">$im</SPAN><SPAN style="COLOR: #007700">=</SPAN><SPAN style="COLOR: #0000bb">imagecreatefromjpeg</SPAN><SPAN style="COLOR: #007700">(</SPAN><SPAN style="COLOR: #0000bb">$filename</SPAN><SPAN style="COLOR: #007700">); <BR></SPAN><SPAN style="COLOR: #0000bb">imagecopymerge</SPAN><SPAN style="COLOR: #007700">(</SPAN><SPAN style="COLOR: #0000bb">$im</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">$pic</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">0</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">0</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">0</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">0</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">250</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">30</SPAN><SPAN style="COLOR: #007700">,</SPAN><SPAN style="COLOR: #0000bb">50</SPAN><SPAN style="COLOR: #007700">); <BR></SPAN><SPAN style="COLOR: #0000bb">imagejpeg</SPAN><SPAN style="COLOR: #007700">(</SPAN><SPAN style="COLOR: #0000bb">$im</SPAN><SPAN style="COLOR: #007700">); <BR></SPAN><SPAN style="COLOR: #0000bb">?></span>   <br>     </span>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317921.htmlTechArticleToday I carefully studied some related technologies of GD, and also studied the problem of Chinese garbled characters in GD. Using the GD library to output Chinese strings and calling imagestring is useless. Need to use imag...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn