Home > php教程 > php手册 > PHP给图片加数字水印出现中文乱码的解决

PHP给图片加数字水印出现中文乱码的解决

WBOY
Release: 2016-06-13 10:46:44
Original
1003 people have browsed it

$textcolor = imagecolorallocate($img, 255, 0, 0); //设置水印字体颜色 
        $font = 'c:/windows/fonts/simhei.ttf'; //定义字体 
        $text = iconv("GB2312", "UTF-8", $waterImageUrl); //将中文字转换为UTF8 
        imagettftext($img, 20, 10, 50, 40, $textcolor, $font, $text);//将文字写到图片中 
        $this->outputImage($img, $this->getExtendsName($imageUrl), $imageUrl); 
        imagedestroy($img);///销毁图像  

 很多情况下,问题是这样解决地,可是,你别忘记了,还有特殊情况,那就是你设置了php页编码的情况,比如:header( "Content-Type:text/html; charset=UTF-8 ");
,嗯,这时候,你还用这个方式,那就死翘翘了,一样会乱码,为什么,因为本来就已经是UTF-8,你转什么啊?不乱才怪,这时候,可以将第三行去掉,直接将imagettftext方法中的$text改为$waterImageUrl,当然,还有更加特殊的情况,那就是,你传值的页面是别的编码,而非UTF-8,那估计又有文章,所以,我奉劝各位学PHP的朋友,最好保证你的网站中每个页面编码一致,这样,问题解决起来方便很多。
还是贴下代码吧,怕有些人不知道, www.2cto.com

$textcolor = imagecolorallocate($img, 255, 0, 0); //设置水印字体颜色 
        $font = 'c:/windows/fonts/simhei.ttf'; //定义字体 
        //$text = iconv("UTF-8", "UTF-8", $waterImageUrl); //将中文字转换为UTF8 
        //实际上可以看到,这里可以不用转,因为我在前面的index.php文件中已经设置了编码为UTF-8,如果 
        //写为//$text = iconv("GB2312", "UTF-8", $waterImageUrl); 反而会出现乱码 
        $text = $waterImageUrl; 
        imagettftext($img, 20, 10, 50, 40, $textcolor, $font, $text);//将文字写到图片中 
        $this->outputImage($img, $this->getExtendsName($imageUrl), $imageUrl); 
        imagedestroy($img);///销毁图像  


摘自 0+0+0+...=1
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template