• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

如何用php生成扭曲及旋转的验证码图片_php技巧

php 搞代码 4年前 (2022-01-26) 35次浏览 已收录 0个评论
<?php <BR>function make_rand($length="32"){//验证码文字生成函数 <BR>        $str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; <BR>    $result=""; <BR>    for($i=0;$i<$length;$i++){ <BR>        $num[$i]=rand(0,61); <BR>        $result.=$str[$num[$i]]; <BR>    } <BR>    return $result; <BR>} <BR>$checkcode = make_rand(5); <BR>$im_x=160; <BR>$im_y=32; <BR>function make_crand($length="5") { <BR>    $string = ''; <BR>    for($i=0;$i<$length;$i++) { <BR>        $string .= chr(rand(0xB0,0xF7)).chr(rand(0xA1,0xFE)); <BR>    } <BR>    return $string; <BR>} <BR>function getAuthImage($text , $im_x = 230 , $im_y = 32) { <BR>    $im = imagecreatetruecolor($im_x,$im_y); <BR>    $text_c = ImageColorAllocate($im, mt_rand(0,100),mt_rand(0,100),mt_rand(0,100)); <BR>    $tmpC0=mt_rand(100,255); <BR>    $tmpC1=mt_rand(100,255); <BR>    $tmpC2=mt_rand(100,255); <BR>    $buttum_c = ImageColorAllocate($im,$tmpC0,$tmpC1,$tmpC2); <BR>    imagefill($im, 16, 13, $buttum_c); <BR>    //echo $text; <BR>    $font = 'c://WINDOWS//Fonts//simsun.ttc'; <BR>    //echo strlen($text); <BR>    $text=iconv("gb2312","UTF-8",$text); <BR>    //echo mb_strlen($text,"UTF-8"); <BR>    for ($i=0;$i<mb_strlen($text);$i++) <BR>    { <BR>            $tmp =mb_substr($text,$i,1,"UTF-8"); <BR>            $array = array(-1,0,1); <BR>            $p = array_rand($array); <BR>            $an = $array[$p]*mt_rand(1,9);//角度 <BR>            $size = 20; <BR>            imagettftext($im,$size,$an,10+$i*$size*2,25,$text_c,$font,$tmp); <BR>    } <BR>     $distortion_im = imagecreatetruecolor ($im_x, $im_y); <BR>     imagefill($distortion_im, 16, 13, $buttum_c); <BR>     for ( $i=0; $i<$im_x; $i++) { <BR>         for ( $j=0; $j<$im_y; $j++) { <BR>             $rgb = imagecolorat($im, $i , $j); <BR>             if( (int)($i+20+sin($j/$im_y*2*M_PI)*10) =0 ) { <BR>                 imagesetpixel ($distortion_im, (int)($i+10+sin($j/$im_y*2*M_PI-M_PI*0.5)*3) , $j , $rgb); <BR>             } <BR>         } <BR>     } <BR>     //加入干扰象素; <BR>    $count = 600;//干扰像素的数量 <BR>    for($i=0; $i<$count; $i++){ <BR>            $randcolor = ImageColorallocate($distortion_im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); <BR>            imagesetpixel($distortion_im, mt_rand()%$im_x , mt_rand()%$im_y , $randcolor); <BR>    } <BR>    $line_c=5; <BR>     //imageline <BR>     for($i=0; $i < $line_c; $i++) { <BR>         $linecolor = imagecolorallocate($distortion_im, 17, 158, 20); <BR>         $lefty = mt_rand(1, $im_x-1); <BR>         $righty = mt_rand(1, $im_y-1); <BR>         imageline($distortion_im, 0, $lefty, imagesx($di<i>本文@来#源gaodai$ma#com搞$$代**码网</i><strong>搞代gaodaima码</strong>stortion_im), $righty, $linecolor); <BR>     } <BR>     Header("Content-type: image/PNG"); <BR>    //以PNG格式将图像输出到浏览器或文件; <BR>    //ImagePNG($im); <BR>    ImagePNG($distortion_im); <BR>    //销毁一图像,释放与image关联的内存; <BR>    ImageDestroy($distortion_im); <BR>    ImageDestroy($im); <BR>} <BR>?><BR>


用法示例如下:

<?php<BR>getAuthImage(make_crand(5)); <BR>?><BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何用php生成扭曲及旋转的验证码图片_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址