我的隨機(jī)值也-1了,可還是會出現(xiàn)兩個漢字或者三個漢字的情況
<?php
session_start();
$image=imagecreatetruecolor(200, 60);
$color=imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $color);
$fonttype="D:\wamp64\www\project01\STKAITI.TTF";
$array_font="的是了我不人在他有這個上們來到時大地為子中你說生國年
? ? 著就那和要她出也得里后自以會家可下而過天去能對小多然于心學(xué)么之都好看
? ? 起發(fā)當(dāng)沒成只如事把還用第樣道想作種美總從無情己面最女但現(xiàn)前些所同日手又
? ? 行意動方期它頭經(jīng)長兒回位分愛老因很給名法間斯知世什兩次使身者被高已親其進(jìn)此話常與活正感";
? ? $v=str_split($array_font,3);
? ? $captch_code='';
? ? for ($i=0;$i<4;$i++){
? ? ? ? $fontsize=6;
? ? ? ?$fontcolor= imagecolorallocate($image, rand(40,160), rand(40,160), rand(40,160));
? ? ? $index = rand(1,count($v)-1);
? ? ? $cn=$v[$index];
? ? ? $captch_code.=$cn;
? ? ? imagettftext($image, mt_rand(20,24),mt_rand(-60,60),(40*$i+20),mt_rand(30,35), $fontcolor, $fonttype, $cn);
? ? }
? ? $_SERVER['codee']=$captch_code;
? ? for($i=0;$i<200;$i++){
? ? ? ? //設(shè)定顏色
? ? ? ? $color = imagecolorallocate($image, rand(50,180), rand(50,180), rand(50,280));
? ? ? ? //坐標(biāo)
? ? ? ? $x=rand(0,200);
? ? ? ? $y= rand(0,60);
? ? ? ? //系統(tǒng)函數(shù)imagesetpixel設(shè)定像素點(diǎn)
? ? ? ? imagesetpixel($image, $x, $y, $color);
? ? }
? ? for ($i=0;$i<3;$i++){
? ? ? ? $linecolor=imagecolorallocate($image, rand(80,220), rand(80,220), rand(80,220));
? ? ? ? //坐標(biāo)
? ? ? ? $x1 = rand(1,99);
? ? ? ? $y1 = rand(1,59);
? ? ? ? $x2 = rand(5,200);
? ? ? ? $y2 = rand(1,59);
? ? ? ? //系統(tǒng)函數(shù)imageline設(shè)置劃線
? ? ? ? //也可以直接在函數(shù)imageline內(nèi)設(shè)定坐標(biāo):
? ? ? ? //imageline($image,rand(8,99),rand(1,28),rand(5,99),rand(1,28)$linecolor);
? ? ? ? imageline($image, $x1, $y1, $x2, $y2, $linecolor);
? ? }
? ? header('content-type:image/png');
? ?imagepng($image);
imagedestroy($image);
?>
2019-04-16
在UTF8中, 1個漢字=3個字節(jié)。
這里存在換行,1個換行符 '\n' = 1個字節(jié)。前三個漢字正常分割,然后還有 一個換行符+1個漢字=4字節(jié),
結(jié)果很顯然,換行符吞掉了后面的兩個字節(jié)。