php 验证码 扭曲,php图片验证码函数实现扭曲字符的实现代码

/**

* CaptchaImage() - 创建扭曲字符的验证码图片

* $session_name string 验证码图片创建时所需生成Session的变量名

* $width int 验证图片的宽度,默认120,注:图片高度与宽度比例相对固定

* $noise int 干扰素的点数,默认0

* $disturb int 干扰字符个数,默认0

* $curve bool 是否增加干扰曲线,默认ture

* */

function CaptchaImage($session_name = '', $width = 120, $noise = 0, $disturb = 0, $curve = true){

$im_x = $width;

$im_y = ceil(0.25 * $width);

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

$text_c = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));

$gray_rand = mt_rand(160, 220);

$buttum_c = ImageColorAllocate($im, $gray_rand, $gray_rand, $gray_rand);

imagefill($im, 0, 0, $buttum_c);

session_start();

$text = '';

$characters = 'ABCEFGHJKLMNPQRSTUVWXYZ';

for($i = 0, $len = strlen($characters); $i < 4; $i++){

$text .= $characters{rand(0, $len - 1)};

if(isset($session_name{0}) && session_start()){

$_SESSION[$session_name] = $text;

$_SESSION['CaptchaSessionTime'] = time();

}

}

$font = 'arial.ttf';

$size = floor(0.2 * $width);

$ttfbox = imagettfbbox($size, 0, $font, $text);

$text_width = abs($ttfbox[2] - $ttfbox[0]);

$side = floor(($im_x - $text_width) / 2);

$array = array(-1, 1);

for ($i = 0; $i < strlen($text); $i++){

$p = array_rand($array);

$an = $array[$p] * mt_rand(5, 10); // 字符倾斜角度

imagettftext($im, $size, $an, $side + $i * ($size - 1), $im_y - 3, $text_c, $font, $text{$i});

}

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

imagefill($distortion_im, 0, 0, $buttum_c);

$distortion = floor(0.04 * $width); // 扭曲程度

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

for ($j = 0; $j < $im_y; $j++){

$rgb = imagecolorat($im, $i , $j);

if( (int)($i + sin($j / $im_y * 2 * M_PI) * $distortion) <= $im_x && (int)($i + sin($j / $im_y * 2 * M_PI) * $distortion) >= 0 ){

imagesetpixel($distortion_im, (int)($i + sin($j / $im_y * 2 * M_PI - M_PI * 0.1) * $distortion) , $j, $rgb);

}

}

}

if($disturb > 0){

$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

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

imagestring($distortion_im, 3, mt_rand(-10, $im_x), mt_rand(-10, $im_y), $chars[mt_rand(0, 35)], $text_c);

}

}

//加入干扰象素;

if($noise > 0){

for($i = 0; $i < $noise; $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);

}

}

// 加干扰曲线

if($curve){

$rand = mt_rand(5, 30);

$rand1 = mt_rand(15, 25);

$rand2 = mt_rand(5, 10);

for ($yy = $rand; $yy <= $rand + 2; $yy++){

for ($px = -60; $px <= 60; $px++){

$x = $px / $rand1;

$y = ($x != 0) ? sin($x) : $y;

$py = $y * $rand2;

imagesetpixel($distortion_im, $px + 60, $py + $yy, $text_c);

}

}

}

//设置文件头;

Header("Content-type: image/JPEG");

//以PNG格式将图像输出到浏览器或文件;

ImagePNG($distortion_im);

//销毁一图像,释放与image关联的内存;

ImageDestroy($distortion_im);

ImageDestroy($im);

}

CaptchaImage('code', 100); //生成一张图片 并将随机数字存放到key为code的session中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值