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

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

Jun 13, 2016 am 10:46 AM
phpChineseGarbled charactersAppearaddpicturenumberwatermarkofsolve

$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
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!