<?php
function getAuthImage($text) {
    $im_x = 35*strlen($text);
    $im_y = 40;
    $im = imagecreatetruecolor($im_x,$im_y);
    $grey=imagecolorallocate($im,100,100,100);//灰色阴影
    $tmpC0=mt_rand(100,255);
    $tmpC1=mt_rand(100,255);
    $tmpC2=mt_rand(100,255);
    $buttum_c = ImageColorAllocate($im,$tmpC0,$tmpC1,$tmpC2);
    imagefill($im, 16, 13, $buttum_c);

    $font = 'D:\SOURCECODEPRO-MEDIUM.TTF';

    for ($i=0;$i<strlen($text);$i++)
    {
        $tmp =substr($text,$i,1);
        $array = array(-1,1);
        $p = array_rand($array);
        $an = $array[$p]*mt_rand(1,20);//角度
        $size = 28;
        $text_c = ImageColorAllocate($im, mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));    //随机颜色     
        imagettftext($im, $size, $an, 17+$i*$size, 37, $grey, $font, $tmp);     //灰色阴影
        imagettftext($im, $size, $an, 15+$i*$size, 35, $text_c, $font, $tmp);      //输出验证码
    }


    $distortion_im = imagecreatetruecolor ($im_x, $im_y);

    imagefill($distortion_im, 16, 13, $buttum_c);
    for ( $i=0; $i<$im_x; $i++) {
        for ( $j=0; $j<$im_y; $j++) {
            $rgb = imagecolorat($im, $i , $j);
            if( (int)($i+20+sin($j/$im_y*2*M_PI)*10) <= imagesx($distortion_im)&& (int)($i+20+sin($j/$im_y*2*M_PI)*10) >=0 ) {
                imagesetpixel ($distortion_im, (int)($i+10+sin($j/$im_y*2*M_PI-M_PI*0.1)*4) , $j , $rgb);
            }
        }
    }
    //加入干扰象素;
    $count = 260;//干扰像素的数量
    for($i=0; $i<$count; $i++){
        $randcolor = ImageColorallocate($distortion_im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
        imagesetpixel($distortion_im, mt_rand()%$im_x , mt_rand()%$im_y , $randcolor);
    }

    $rand = mt_rand(5,30);
    $rand1 = mt_rand(15,25);
    $rand2 = mt_rand(5,10);
    for ($yy=$rand; $yy<=+$rand+2; $yy++){
        for ($px=-80;$px<=25*strlen($text);$px=$px+0.1)
        {
            $x=$px/$rand1;
            if ($x!=0)
            {
                $y=sin($x);
            }
            $py=$y*$rand2;

            imagesetpixel($distortion_im, $px+80, $py+$yy, $text_c);   //加入曲线
        }
    }

    //设置文件头;
    Header("Content-type: image/JPEG");

    //以PNG格式将图像输出到浏览器或文件;
    ImagePNG($distortion_im);

    //销毁一图像,释放与image关联的内存;
    ImageDestroy($distortion_im);
    ImageDestroy($im);
}

function make_rand($length="32"){//验证码文字生成函数
    $str="abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ";
    $result="";
    for($i=0;$i<$length;$i++){
        $num[$i]=rand(0,25);
        $result.=$str[$num[$i]];
    }
    return $result;
}


function make_rand_strnum($length = "4")
{
    //随机种子
  $char_str = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ';
  $num_str = '23456789';
  $char_str_len = strlen($char_str)-1;
  $num_str_len = strlen($num_str)-1;
  $checkcode = $code = ''; 
  //生成随机码
  $t = 0;
  for($i=0;$i<$length;$i++){
    //设置字体大小
    $fontsize = 6;
    //设置数字
    $code = substr($char_str,rand(0,$char_str_len),1);
    

    if($i==3 && $t==($length-1))//全部字符1+1+1
    {
      $code = substr($num_str,rand(0,$num_str_len),1);
    }
    elseif($i==3 && $t==(($length-1)*2))//全部数字2+2+2
    {
      $code = substr($char_str,rand(0,$char_str_len),1);
    }
    else{//其他正常情况
      $k= rand(1,2);
      $t = $t + $k;
      if ($k==2)
      {
        $code = substr($num_str,rand(0,$num_str_len),1);
      }
    }
   $checkcode .= $code;
}
 //echo  $checkcode;exit;
return $checkcode;
}




//输出调用
$checkcode = make_rand_strnum(4);
session_start();//将随机数存入session中
$_SESSION['jn_checkcode']=strtolower($checkcode);
getAuthImage($checkcode);
?>