为了账号安全,请及时绑定邮箱和手机立即绑定

我的随机值也-1了,可还是会出现两个汉字或者三个汉字的情况

<?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="的是了我不人在他有这个上们来到时大地为子中你说生国年

    着就那和要她出也得里后自以会家可下而过天去能对小多然于心学么之都好看

    起发当没成只如事把还用第样道想作种美总从无情己面最女但现前些所同日手又

    行意动方期它头经长儿回位分爱老因很给名法间斯知世什两次使身者被高已亲其进此话常与活正感";

    $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++){

        //设定颜色

        $color = imagecolorallocate($image, rand(50,180), rand(50,180), rand(50,280));

        //坐标

        $x=rand(0,200);

        $y= rand(0,60);

        //系统函数imagesetpixel设定像素点

        imagesetpixel($image, $x, $y, $color);

    }

    for ($i=0;$i<3;$i++){

        $linecolor=imagecolorallocate($image, rand(80,220), rand(80,220), rand(80,220));

        //坐标

        $x1 = rand(1,99);

        $y1 = rand(1,59);

        $x2 = rand(5,200);

        $y2 = rand(1,59);

        //系统函数imageline设置划线

        //也可以直接在函数imageline内设定坐标:

        //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);

?>


正在回答

1 回答

https://img1.sycdn.imooc.com//5cb5be5f000189bb07890334.jpg

在UTF8中, 1个汉字=3个字节。

这里存在换行,1个换行符 '\n' = 1个字节。前三个汉字正常分割,然后还有 一个换行符+1个汉字=4字节,

结果很显然,换行符吞掉了后面的两个字节。

2 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

我的随机值也-1了,可还是会出现两个汉字或者三个汉字的情况

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信