解决GD中文乱码问题

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

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

  下面给个使用实例:

   

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


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

 

<?php 
$pic
=imagecreate(250,30); 
$black=imagecolorallocate($pic,0,0,0); 
$white=imagecolorallocate($pic,255,255,255); 
$font="C://WINDOWS//Fonts//simhei.ttf";  
$str ='php'.iconv('gb2312','utf-8','面对对象')." www.phpobject.net"
imagettftext($pic,10,0,10,20,$white,$font,$str);

header("Content-type: image/jpeg");
$filename='../src/images/photo.jpg';
$im=imagecreatefromjpeg($filename);
imagecopymerge($im,$pic,0,0,0,0,250,30,50);
imagejpeg($im);
?>  
     

 

相关文章

php实现的debug log日志操作类实例

本文实例讲述了php实现的debug log日志操作类。分享给大家供大家参考,具体如下: <?php class Tool { public static func...

PHP中file_exists函数不支持中文名的解决方法

一般来说PHP中常使用file_exists()判断某个文件或者文件夹是否存在,如果存在则返回true,否则返回false。但是该函数在网页使用UTF8编码的情况下,对于中文的文件名或者...

采集邮箱的php代码(抓取网页中的邮箱地址)

复制代码 代码如下: <?php $url='//www.jb51.net'; //这个网页里绝对含有邮件地址。 $content=file_get_contents($url);...

php字符串截取中文截取2,单字节截取模式

//中文截取2,单字节截取模式 function cn_substr($str,$slen,$startdd=0){     $re...

PHP基于正则批量替换Img中src内容实现获取缩略图的功能示例

本文实例讲述了PHP基于正则批量替换Img中src内容实现获取缩略图的功能。分享给大家供大家参考,具体如下: 这里PHP用正则批量替换Img中src内容,实现获取图片路径缩略图的功能 网...