> php教程 > php手册 > 解决GD中文乱码问题

解决GD中文乱码问题

WBOY
풀어 주다: 2016-06-13 12:33:25
원래의
1337명이 탐색했습니다.

今天仔细研究了下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>

 
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿