Home  >  Article  >  Backend Development  >  What to do if the php verification code is garbled

What to do if the php verification code is garbled

藏色散人
藏色散人Original
2020-11-05 11:03:163411browse

Solution to garbled php verification code: First determine the Chinese verification code text to be drawn; then pass "imagettftext($im,12,3,20,20,$te,'msyhbd.ttf',$ str);" method can solve the problem of garbled characters.

What to do if the php verification code is garbled

Recommended: "PHP Video Tutorial"

php utf8 page verification code picture Chinese garbled code

During development, the Chinese verification code on the uft-8 page was garbled. I found the reason by searching on Baidu. Friends who need it can refer to the solution below. The code is as follows: ? session_start(); // Generate random numbers for ( $i =0; $i 4; $i ){ $rand .= dechex (rand(1,15));

The Chinese verification code on the uft-8 page appears garbled during development Yes, I found the reason by searching on Baidu. Friends who need it can refer to the solution below. The code is as follows:

<? 
session_start(); 
 
//生成随机数 
for($i=0;$i<4;$i++){ 
$rand.=dechex(rand(1,15)); 
} 
$_SESSION[&#39;checkpic&#39;]=$rand; 
$im=imagecreatetruecolor(100,30);//画板,新建一个真彩色图像 
//设置颜色 
$bg=imagecolorallocate($im,0,0,0);//红,绿,蓝 背景颜色 
$te=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//字体颜色 
//画线条 
for($i=0;$i<3;$i++){ 
$te2=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); 
imageline($im,rand(0,100),0,rand(0,100),30,$te2);//坐标(x1,y1)到坐标(x2,y2) 
} 
//画点 
for($i=0;$i<200;$i++){ 
imagesetpixel($im,rand()%100,rand()%30,$te2); 
} 
 
//输出中文 
$str=iconv("gbk","utf-8","新年快乐!");//确定要绘制的中文文字 
imagettftext($im,12,3,20,20,$te,&#39;msyhbd.ttf&#39;,&#39;中文en&#39;); 
//把字符串写在图像左上角 
//imagestring($im,5,rand(0,50),rand(0,15),$rand,$te); 
//输出图像 
header("Content-type:image/jpeg");//文件类型 
imagejpeg($im); 
 
?>

Analyze the cause and solution. The error code is as follows:

$str=iconv("gbk","utf-8","新年快乐!");//确定要绘制的中文文字 
imagettftext($im,12,3,20,20,$te,&#39;msyhbd.ttf&#39;,&#39;中文en&#39;);

Correct The code should look like this:

$str=iconv("gbk","utf-8","新年快乐!");//确定要绘制的中文文字 
imagettftext($im,12,3,20,20,$te,&#39;msyhbd.ttf&#39;,$str);

The above is the detailed content of What to do if the php verification code is garbled. For more information, please follow other related articles on the PHP Chinese website!

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