function create_captcha($width = 60, $height = 32) { session_start(); //生成验证码图片 Header("Content-type: image/PNG"); $im = imagecreate($width, $height); // width and height of image $back = ImageColorAllocate($im, 245, 245, 245); // specify background color imagefill($im, 0, 0, $back); // fill the background color into image $vcodes = ""; srand((double) microtime() * 1000000); //生成4位数字 for ($i = 0; $i < 4; $i++) { $font = ImageColorAllocate($im, rand(100, 255), rand(0, 100), rand(100, 255)); // 生成随机颜色 $authnum = rand(1, 9); $vcodes .= $authnum; imagestring($im, 5, 2 + $i * 10, 1, $authnum, $font); } for ($i = 0; $i < 100; $i++) { // interuppting $randcolor = ImageColorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($im, rand() % 70, rand() % 30, $randcolor); // 画像素点函数 } ImagePNG($im); ImageDestroy($im); $_SESSION['captcha'] = $vcodes; }
function create_captcha() { session_start(); //生成验证码图片 Header("Content-type: image/PNG"); $im = imagecreate(44, 18); // 画一张指定宽高的图片 $back = ImageColorAllocate($im, 245, 245, 245); // 定义背景颜色 imagefill($im, 0, 0, $back); //把背景颜色填充到刚刚画出来的图片中 $vcodes = ""; srand((double) microtime() * 1000000); //生成4位数字 for ($i = 0; $i < 4; $i++) { $font = ImageColorAllocate($im, rand(100, 255), rand(0, 100), rand(100, 255)); // 生成随机颜色 $authnum = rand(1, 9); $vcodes .= $authnum; imagestring($im, 5, 2 + $i * 10, 1, $authnum, $font); } $_SESSION['VCODE'] = $vcodes; for ($i = 0; $i < 100; $i++) { $randcolor = ImageColorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($im, rand() % 70, rand() % 30, $randcolor); // 画像素点函数 } ImagePNG($im); ImageDestroy($im); }
function getAuthImage($text) { $this->setpin($text); $im_x = 160; $im_y = 40; $im = imagecreatetruecolor($im_x, $im_y); $text_c = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 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 = PATH_SYS_PUBLIC . 'font-awesome/fonts/verdana.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, 10); //角度 $size = 28; 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 = 160; //干扰像素的数量 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 <= 80; $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 rand_create() { //通知浏览器将要输出PNG图片 Header("Content-type: image/PNG"); //准备好随机数发生器种子 srand((double) microtime() * 1000000); //准备图片的相关参数 $im = imagecreate(62, 20); $black = ImageColorAllocate($im, 0, 0, 0); //RGB黑色标识符 $white = ImageColorAllocate($im, 255, 255, 255); //RGB白色标识符 $gray = ImageColorAllocate($im, 200, 200, 200); //RGB灰色标识符 //开始作图 imagefill($im, 0, 0, $gray); while (($randval = rand() % 100000) < 10000) { } $_SESSION["login_check_num"] = $randval; //将四位整数验证码绘入图片 imagestring($im, 5, 10, 3, $randval, $black); //加入干扰象素 for ($i = 0; $i < 200; $i++) { $randcolor = ImageColorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($im, rand() % 70, rand() % 30, $randcolor); } //输出验证图片 ImagePNG($im); //销毁图像标识符 ImageDestroy($im); }
static function getAuthImage($text, $w = 200, $h = 40) { $im_x = $w; $im_y = $h; $im = imagecreatetruecolor($im_x, $im_y); $text_c = imagecolorallocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); $button_c = imagecolorallocate($im, mt_rand(100, 255), mt_rand(100, 255), mt_rand(100, 255)); imagefill($im, 16, 13, $button_c); $font = realpath(THINK_PATH . 'Common/Class/VerifyCode/') . '\\t1.ttf'; $len = strlen($text); $mt_array = array(-1, 1); $size = 28; for ($i = 0; $i < $len; $i++) { $tmp = substr($text, $i, 1); $p = array_rand($mt_array); $an = $mt_array[$p] * mt_rand(1, 10); //角度 imagettftext($im, $size, $an, 15 + $i * $size, 35, $text_c, $font, $tmp); } $distortion_im = imagecreatetruecolor($im_x, $im_y); imagefill($distortion_im, 16, 13, $button_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 = 160; //干扰像素的数量 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 <= 80; $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 getAuthImage($text, $im_x, $im_y, $move_x, $move_y, $font_size, $count) { $im = imagecreatetruecolor($im_x, $im_y); $text_c = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 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 = '../font/Airbus.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, 10); $size = $font_size; imagettftext($im, $size, $an, $i * $size + $move_x, $move_y, $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 * 1 * M_PI - M_PI * 0.1) * 1), $j, $rgb); } } } 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(1, 25); $rand2 = mt_rand(5, 10); for ($yy = $rand; $yy <= +$rand + 2; $yy++) { for ($px = -80; $px <= 80; $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"); ImagePNG($distortion_im); ImageDestroy($distortion_im); ImageDestroy($im); }
public function captcha() { $this->load->library('session'); //生成验证码图片 header("Content-type: image/png"); //要显示的字符,可自己进行增删 $str = "1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,y,z"; $list = explode(",", $str); $cmax = count($list) - 1; $verifyCode = ''; for ($i = 0; $i < 5; $i++) { $randnum = mt_rand(0, $cmax); //取出字符,组合成验证码字符 $verifyCode .= $list[$randnum]; } //避免程序读取session字符串破解,生成的验证码用MD5加密一下再放入session,提交的验证码md5以后和seesion存储的md5进行对比 //直接md5还不行,别人反向md5后提交还是可以的,再加个特定混淆码再md5强度才比较高,总长度在14位以上 //网上有反向md5的 Rainbow Table,64GB的量几分钟内就可以搞定14位以内大小写字母、数字、特殊字符的任意排列组合的MD5反向 //但这种方法不能避免直接分析图片上的文字进行破解,生成gif动画比较难分析出来 //加入前缀、后缀字符,prestr endstr 为自定义字符,将最终字符放入SESSION中 //$_SESSION['randcode'] = md5($this->prestr.$verifyCode.$this->endstr); $this->session->set_userdata('randcode', md5($this->session_prestr . $verifyCode . $this->session_endstr)); //生成图片 $im = imagecreate(65, 30); //此条及以下三条为设置的颜色 $black = imagecolorallocate($im, 0, 0, 0); $white = imagecolorallocate($im, 255, 255, 255); $gray = imagecolorallocate($im, 200, 200, 200); $red = imagecolorallocate($im, 255, 0, 0); $blue = imagecolorallocate($im, 52, 126, 189); //给图片填充颜色 imagefill($im, 0, 0, $white); //将验证码写入到图片中 imagestring($im, 5, 10, 8, $verifyCode, $blue); //加入干扰象素 for ($i = 0; $i < 50; $i++) { $randcolor = ImageColorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($im, rand() % 70, rand() % 30, $randcolor); } imagepng($im); imagedestroy($im); }
/** * 获取生成的Code * * @return [type] [description] */ public function display() { header("Content-type: image/PNG"); $im = imagecreate(52, 20); $black = ImageColorallocate($im, 0, 0, 0); $white = ImageColorallocate($im, 230, 255, 255); $gray = ImageColorallocate($im, 200, 200, 255); $blue = ImageColorallocate($im, 40, 100, 225); imagefill($im, 0, 0, $white); $_SESSION['_authcode'] = $this->genCode(); $authnum = $_SESSION['_authcode']; imagestring($im, 6, 8, 3, $authnum, $blue); for ($i = 0; $i < 200; $i++) { $randcolor = ImageColorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($im, rand() % 70, rand() % 30, $randcolor); } ImagePNG($im); ImageDestroy($im); exit; }
/** * @throws Exception * 生成验证码 , 存放到token相对应的session里面 */ public function createCaptcha() { $token = $_COOKIE["token"]; if (empty($token)) { throw new Exception("无法生产验证码,请稍后在试"); } $string = "abcdefghijklmnopqrstuvwxyz0123456789"; $str = ""; for ($i = 0; $i < 4; $i++) { $pos = rand(0, 35); $str .= $string[$pos]; } $this->load->library('session'); $flag = $this->session->set_userdata($token . "_captcha", $str); $img_handle = Imagecreate(80, 20); //图片大小80X20 $back_color = ImageColorAllocate($img_handle, 255, 255, 255); //背景颜色(白色) $txt_color = ImageColorAllocate($img_handle, 0, 0, 0); //文本颜色(黑色) //加入干扰线 for ($i = 0; $i < 3; $i++) { $line = ImageColorAllocate($img_handle, rand(0, 255), rand(0, 255), rand(0, 255)); Imageline($img_handle, rand(0, 15), rand(0, 15), rand(100, 150), rand(10, 50), $line); } //加入干扰象素 for ($i = 0; $i < 200; $i++) { $randcolor = ImageColorallocate($img_handle, rand(0, 255), rand(0, 255), rand(0, 255)); Imagesetpixel($img_handle, rand() % 100, rand() % 50, $randcolor); } Imagefill($img_handle, 0, 0, $back_color); //填充图片背景色 ImageString($img_handle, 28, 20, 0, $str, $txt_color); //水平填充一行字符串 ob_clean(); // ob_clean()清空输出缓存区 header("Content-type: image/png"); //生成验证码图片 Imagepng($img_handle); //显示图片 }
public function authCodeAction() { session_start(); //生成验证码图片 Header("Content-type: image/PNG "); srand((double) microtime() * 1000000); $authnum = rand(1000, 9999); $_SESSION['rand_code'] = $authnum; $im = imagecreate(62, 20); ImageColorAllocate($im, 0, 0, 0); $white = ImageColorAllocate($im, 255, 255, 255); //将五位整数验证码绘入图片 imagestring($im, 5, 10, 3, $authnum, $white); //加入干扰象素 for ($i = 0; $i < 200; $i++) { $randcolor = ImageColorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($im, rand() % 70, rand() % 30, $randcolor); } ImagePNG($im); ImageDestroy($im); }
public function handle_request() { $params = APF::get_instance()->get_request()->get_parameters(); $cookiename = @$params['cn']; $x_size = $params['x']; $y_size = $params['y']; $num_size = $params['s']; //添加字符数量 $font_num = isset($params['n']) ? $params['n'] : 4; $x1 = $params['x1']; $y1 = $params['y1']; $x2 = $params['x2']; $y2 = $params['y2']; $seccode = $this->random($font_num, 1); $md5seccode = Util_AuthorCrypt::encrypt($seccode, md5($_SERVER['HTTP_USER_AGENT'])); // if('askquestionreg'==$cookiename){ // $objMem=APF_Cache_Factory::get_instance()->get_memcache(); // $strGUID=APF::get_instance()->get_request()->get_cookie('aQQ_ajkguid'); // $objMem->set('askquestionreg_'.$strGUID,$md5seccode,0,90); // } APF::get_instance()->get_response()->set_cookie($cookiename, $md5seccode, 30 * 60); header("Content-type: image/png"); $im = imagecreatetruecolor($x_size, $y_size); $white = imagecolorallocate($im, 255, 255, 255); $grey = imagecolorallocate($im, 128, 128, 128); $black = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, $x_size, $y_size, $white); $font = APF::get_instance()->get_config("FontsUrl"); //var_dump($font);exit; imagettftext($im, $num_size, 2, $x1, $y1, $grey, $font, $seccode); imagettftext($im, $num_size, 2, $x2, $y2, $black, $font, $seccode); for ($i = 0; $i < 600; $i++) { $randcolor = ImageColorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($im, rand() % $x_size, rand() % $y_size, $randcolor); } imagepng($im); imagedestroy($im); return false; }
public function handle_request() { $params = APF::get_instance()->get_request()->get_parameters(); $x_size = $params['x']; $y_size = $params['y']; $num_size = $params['s']; //添加字符数量 $font_num = isset($params['n']) ? $params['n'] : 4; $x1 = $params['x1']; $y1 = $params['y1']; $x2 = $params['x2']; $y2 = $params['y2']; $verifyCode = strtolower($this->random($font_num, 1)); $keySuffix = md5($_SERVER['HTTP_USER_AGENT'] . $verifyCode); Bll_Security_Verify::saveVerifyCode($keySuffix, $verifyCode); $response = APF::get_instance()->get_response(); $response->set_content_type('image/png'); $response->set_cookie('sv', $keySuffix, 10 * 60, NULL, NULL, FALSE, TRUE); $response->set_cache_control('private'); $response->set_header('Expires', 'Thu Jun 05 2014 11:41:06 GMT'); $response->set_header('hash', $keySuffix); $response->set_header('Pragma', 'no-cache'); $im = imagecreatetruecolor($x_size, $y_size); $white = imagecolorallocate($im, 255, 255, 255); $grey = imagecolorallocate($im, 128, 128, 128); $black = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, $x_size, $y_size, $white); $font = APF::get_instance()->get_config("FontsUrl"); imagettftext($im, $num_size, 2, $x1, $y1, $grey, $font, $verifyCode); imagettftext($im, $num_size, 2, $x2, $y2, $black, $font, $verifyCode); for ($i = 0; $i < 600; $i++) { $randColor = ImageColorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($im, rand() % $x_size, rand() % $y_size, $randColor); } imagepng($im); imagedestroy($im); return false; }
<?php ob_start(); if (!isset($_SESSION)) { session_start(); } header('content-Type:image/gif'); echo mt_srand(time()); const width = 105; const height = 33; $length = strlen($_SESSION['ans_checknum']); $img = imagecreate(width, height); imagefill($img, 0, 0, ImageColorAllocate($img, 200, 200, 200)); for ($i = 0; $i < 250; $i++) { $randcolor = ImageColorallocate($img, rand(10, 250), rand(10, 250), rand(10, 250)); imagesetpixel($img, rand() % width, rand() % height, $randcolor); } for ($i = 0; $i < $length; $i++) { $color = imagecolorallocate($img, abs(mt_rand() % 128), abs(mt_rand() % 128), abs(mt_rand() % 128)); imagechar($img, height / 2, abs(mt_rand() % width / 8) + $i * width / 4, abs(mt_rand() % height / 2), $_SESSION['ans_checknum'][$i], $color); } imagegif($img); imageDestroy($img); ob_end_flush();
public function verify() { //Image::buildImageVerify(); $text = $this->make_rand(4); $_SESSION['verify'] = md5(strtolower($text)); $im_x = 160; $im_y = 40; $im = imagecreatetruecolor($im_x, $im_y); $text_c = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 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 = $_SERVER['DOCUMENT_ROOT'] . '/tpl/' . MODULE_NAME . '/default/common/t1.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, 10); //角度 $size = 28; 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 = 160; //干扰像素的数量 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 <= 80; $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); }
public function CodeAction() { $im_x = 100; $im_y = 35; $im = imagecreatetruecolor($im_x, $im_y); $text_c = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 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, 1, $buttum_c); $font = 't1.ttf'; $text = $this->make_rand(4); $session = $this->get('session'); $session->set("code", strtolower($text)); $this->get("logger")->err("========session code:" . $session->get("code")); $white = ImageColorAllocate($im, 0, 0, 0); $printtext = ""; for ($index = 0; $index < 4; $index++) { $printtext .= $text[$index] . " "; } ImageString($im, 10, 10, 10, $printtext, $white); $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 = 150; //干扰像素的数量 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, 35); $rand1 = mt_rand(15, 25); $rand2 = mt_rand(5, 10); for ($yy = $rand; $yy <= +$rand + 2; $yy++) { for ($px = -80; $px <= 80; $px = $px + 0.1) { $x = $px / $rand1; if ($x != 0) { $y = sin($x); } $py = $y * $rand2; imagesetpixel($distortion_im, $px + 50, $py + $yy, $text_c); } } //设置文件头; Header("Content-type: image/png"); ImagePNG($distortion_im); //销毁一图像,释放与image关联的内存; ImageDestroy($distortion_im); ImageDestroy($im); $response = new Response(json_encode("")); $response->headers->set('Content-Type', 'text/json'); return $response; }
/** * 图片创建方法 * @return void; */ function create_image() { $this->randCode(); $size = 25; $width = 80; $height = 26; $degrees = array(rand(0, 30), rand(0, 30), rand(0, 30), rand(0, 30)); for ($i = 0; $i < 4; ++$i) { if (rand() % 2) { } else { $degrees[$i] = -$degrees[$i]; } } $this->image = imagecreatetruecolor($size, $size); $this->validate = imagecreatetruecolor($width, $height); $back = imagecolorallocate($this->image, 255, 255, 255); $border = imagecolorallocate($this->image, 0, 0, 0); imagefilledrectangle($this->validate, 0, 0, $width, $height, $back); for ($i = 0; $i < 4; ++$i) { $temp = $this->RgbToHsv(rand(0, 250), rand(0, 150), rand(0, 250)); if ($temp[2] > 60) { $temp[2] = 60; } $temp = $this->HsvToRgb($temp[0], $temp[1], $temp[2]); $textcolor[$i] = imagecolorallocate($this->image, $temp[0], $temp[1], $temp[2]); } /* // ----------------------------------- // Determine angle and position // ----------------------------------- $length = 4; $angle =($length >= 6)? rand(-($length - 6),($length - 6)): 0; $x_axis = rand(6,(360 / $length)- 16); $y_axis =($angle >= 0)? rand($height, $width): rand(6, $height); // ----------------------------------- // Create the spiral pattern // ----------------------------------- $theta = 1; $thetac = 7; $radius = 16; $circles = 200; $points = 32; for($i = 0; $i <($circles * $points)- 1; $i ++) { $grid_color = imagecolorallocate($this->validate, mt_rand(10, 160), mt_rand(10, 160), mt_rand(10, 160)); $theta = $theta + $thetac; $rad = $radius *($i / $points); $x =($rad * cos($theta))+ $x_axis; $y =($rad * sin($theta))+ $y_axis; $theta = $theta + $thetac; $rad1 = $radius *(($i + 1)/ $points); $x1 =($rad1 * cos($theta))+ $x_axis; $y1 =($rad1 * sin($theta))+ $y_axis; imageline($this->validate, $x, $y, $x1, $y1, $grid_color); $theta = $theta - $thetac; } */ for ($i = 0; $i < 200; ++$i) { $randpixelcolor = ImageColorallocate($this->validate, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($this->validate, rand(1, 87), rand(1, 35), $randpixelcolor); } $temp = $this->RgbToHsv(rand(220, 255), rand(220, 255), rand(220, 255)); if ($temp[2] < 200) { $temp[2] = 255; } $temp = $this->HsvToRgb($temp[0], $temp[1], $temp[2]); $randlinecolor = imagecolorallocate($this->image, $temp[0], $temp[1], $temp[2]); // $this->imagelinethick( $this->validate , $textcolor[rand( 0 , 3 )] ); imagefilledrectangle($this->image, 0, 0, $size, $size, $back); //putenv( 'GDFONTPATH=' . realpath( '.' ) ); // Name the font to be used (note the lack of the .ttf extension imagettftext($this->image, $this->fontSize, 0, 8, 20, $textcolor[0], $this->fontFile, $this->code[0]); $this->image = imagerotate($this->image, $degrees[0], $back); imagecolortransparent($this->image, $back); imagecopymerge($this->validate, $this->image, 1, 4, 4, 5, imagesx($this->image) - 10, imagesy($this->image) - 10, 100); $this->image = imagecreatetruecolor($size, $size); imagefilledrectangle($this->image, 0, 0, $size, $size, $back); imagettftext($this->image, $this->fontSize, 0, 8, 20, $textcolor[1], $this->fontFile, $this->code[1]); $this->image = imagerotate($this->image, $degrees[1], $back); imagecolortransparent($this->image, $back); imagecopymerge($this->validate, $this->image, 21, 4, 4, 5, imagesx($this->image) - 10, imagesy($this->image) - 10, 100); $this->image = imagecreatetruecolor($size, $size); imagefilledrectangle($this->image, 0, 0, $size - 1, $size - 1, $back); imagettftext($this->image, $this->fontSize, 0, 8, 20, $textcolor[2], $this->fontFile, $this->code[2]); $this->image = imagerotate($this->image, $degrees[2], $back); imagecolortransparent($this->image, $back); imagecopymerge($this->validate, $this->image, 41, 4, 4, 5, imagesx($this->image) - 10, imagesy($this->image) - 10, 100); $this->image = imagecreatetruecolor($size, $size); imagefilledrectangle($this->image, 0, 0, $size - 1, $size - 1, $back); imagettftext($this->image, $this->fontSize, 0, 8, 20, $textcolor[3], $this->fontFile, $this->code[3]); $this->image = imagerotate($this->image, $degrees[3], $back); imagecolortransparent($this->image, $back); imagecopymerge($this->validate, $this->image, 61, 4, 4, 5, imagesx($this->image) - 10, imagesy($this->image) - 10, 100); //imagerectangle( $this->validate , 0 , 0 , $width - 1 , $height - 1 , $border ); }
/** * 产生一个验证码图片2 (生成更复杂更高级的验证码) * * @param string $authCode 验证码,必须要求为4位 * @param int $imgX 图片的X轴 * @param int $imgY 图片Y轴 * @return void */ public static function makeAuthCodeImg2($authCode, $imgX = 80, $imgY = 25) { $authCode = substr($authCode, 0, 4); $randStr = preg_split('//', $authCode, -1, PREG_SPLIT_NO_EMPTY); $size = 20; $width = $imgX; $height = $imgY; $degrees = array(rand(0, 45), rand(0, 45), rand(0, 45), rand(0, 45)); // 生成数字旋转角度 for ($i = 0; $i < 4; ++$i) { if (rand() % 2) { } else { $degrees[$i] = -$degrees[$i]; } } $image = imagecreatetruecolor($size, $size); // 数字图片画布 $validate = imagecreatetruecolor($width, $height); // 最终验证码画布 $back = imagecolorallocate($image, 255, 255, 255); // 背景色 $border = imagecolorallocate($image, 0, 0, 0); // 边框 imagefilledrectangle($validate, 0, 0, $width, $height, $back); // 画出背景色 // 数字颜色 for ($i = 0; $i < 4; ++$i) { // 考虑为使字符容易看清使用颜色较暗的颜色 $temp = self::rgb2hsv(rand(0, 255), rand(0, 255), rand(0, 255)); if ($temp[2] > 60) { $temp[2] = 60; } $temp = self::hsv2rgb($temp[0], $temp[1], $temp[2]); $textcolor[$i] = imagecolorallocate($image, $temp[0], $temp[1], $temp[2]); } for ($i = 0; $i < 200; ++$i) { $randpixelcolor = ImageColorallocate($validate, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($validate, rand(1, 87), rand(1, 27), $randpixelcolor); } // 干扰线使用颜色较明亮的颜色 $temp = self::rgb2hsv(rand(0, 255), rand(0, 255), rand(0, 255)); if ($temp[2] < 200) { $temp[2] = 255; } $temp = self::hsv2rgb($temp[0], $temp[1], $temp[2]); $randlinecolor = imagecolorallocate($image, $temp[0], $temp[1], $temp[2]); // 画5条干扰线 for ($i = 0; $i < 5; $i++) { imageline($validate, rand(1, 79), rand(1, 24), rand(1, 79), rand(1, 24), $randpixelcolor); } imagefilledrectangle($image, 0, 0, $size, $size, $back); // 画出背景色 imagestring($image, 5, 6, 2, $randStr[0], $textcolor[0]); // 画出数字 $image = imagerotate($image, $degrees[0], $back); imagecolortransparent($image, $back); imagecopymerge($validate, $image, 1, 4, 4, 5, imagesx($image) - 10, imagesy($image) - 10, 100); $image = imagecreatetruecolor($size, $size); // 刷新画板 imagefilledrectangle($image, 0, 0, $size, $size, $back); // 画出背景色 imagestring($image, 5, 6, 2, $randStr[1], $textcolor[1]); // 画出数字 $image = imagerotate($image, $degrees[1], $back); imagecolortransparent($image, $back); imagecopymerge($validate, $image, 21, 4, 4, 5, imagesx($image) - 10, imagesy($image) - 10, 100); $image = imagecreatetruecolor($size, $size); // 刷新画板 imagefilledrectangle($image, 0, 0, $size - 1, $size - 1, $back); // 画出背景色 imagestring($image, 5, 6, 2, $randStr[2], $textcolor[2]); // 画出数字 $image = imagerotate($image, $degrees[2], $back); imagecolortransparent($image, $back); imagecopymerge($validate, $image, 41, 4, 4, 5, imagesx($image) - 10, imagesy($image) - 10, 100); $image = imagecreatetruecolor($size, $size); // 刷新画板 imagefilledrectangle($image, 0, 0, $size - 1, $size - 1, $back); // 画出背景色 imagestring($image, 5, 6, 2, $randStr[3], $textcolor[3]); // 画出数字 $image = imagerotate($image, $degrees[3], $back); imagecolortransparent($image, $back); imagecopymerge($validate, $image, 61, 4, 4, 5, imagesx($image) - 10, imagesy($image) - 10, 100); imagerectangle($validate, 0, 0, $width - 1, $height - 1, $border); // 画出边框 header('Content-type: image/png'); imagepng($validate); imagedestroy($validate); imagedestroy($image); }
//播下一个生成随机数字的种子,以方便下面随机数生成的使用 //session_start();//将随机数存入session中 $_SESSION['authcode'] = ""; $im = imagecreate(50, 20) or die("Cant's initialize new GD image stream!"); //制定图片背景大小 $black = ImageColorAllocate($im, 0, 0, 0); //设定三种颜色 $white = ImageColorAllocate($im, 255, 255, 255); $gray = ImageColorAllocate($im, 235, 235, 235); imagefill($im, 0, 0, $gray); //采用区域填充法,设定(0,0) //生成数字和字母混合的验证码方法 $ychar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"; $list = explode(",", $ychar); for ($i = 0; $i < 4; $i++) { $randnum = rand(0, 35); $authnum .= $list[$randnum]; } $authnum = strtoupper($authnum); //while(($authnum=rand()%100000)<10000); //生成随机的五们数 //将五位整数验证码绘入图片 $_SESSION['authcode'] = $authnum; imagestring($im, 8, 8, 4, $authnum, $black); // 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y座标处(图像的左上角为 0, 0)。 //如果 font 是 1,2,3,4 或 5,则使用内置字体 for ($i = 0; $i < 40; $i++) { $randcolor = ImageColorallocate($im, rand(50, 100), rand(50, 100), rand(50, 100)); imagesetpixel($im, rand() % 70, rand() % 30, $randcolor); } ImagePNG($im); ImageDestroy($im);
<?php @session_start(); // 生成验证码图片 @Header("Content-type: image/PNG"); srand((double) microtime() * 1000000); $im = imagecreate(62, 20); $black = ImageColorAllocate($im, 255, 255, 255); $white = ImageColorAllocate($im, 83, 53, 140); $gray = ImageColorAllocate($im, 0, 0, 0); imagefill($im, 68, 30, $gray); while (($authnum = rand() % 100000) < 10000) { } // 将四位整数验证码绘入图片 imagestring($im, 5, 10, 3, $authnum, $white); for ($i = 0; $i < 500; $i++) { $randcolor = ImageColorallocate($im, rand(83, 255), rand(53, 255), rand(140, 255)); imagesetpixel($im, rand() % 70, rand() % 30, $randcolor); } ImagePNG($im); ImageDestroy($im); $_SESSION['CheckCode'] = $authnum . '';
* * @package cubi.bin * @copyright Copyright (c) 2005-2011, Openbiz Technology LLC * @license http://code.google.com/p/openbiz-cubi/wiki/CubiLicense * @link http://code.google.com/p/openbiz-cubi/ * @version $Id$ */ require_once 'app_init.php'; $CodeLength = $_GET['length'] ? (int) $_GET['length'] : 6; $CodeLevel = $_GET['level'] ? (int) $_GET['level'] : 1; //Header("Content-type: image/PNG"); $im = imagecreate(65, 22); $back = ImageColorAllocate($im, 245, 245, 245); imagefill($im, 0, 0, $back); srand((double) microtime() * 1000000); $graycolor = ImageColorallocate($im, 102, 102, 102); srand(floor(time() / (60 * 60 * 24))); switch ($CodeLevel) { default: case "1": for ($i = 0; $i < $CodeLength; $i++) { //$font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255)); srand(); $authnum = rand(1, 9); $vcodes .= $authnum; imagestring($im, 5, 2 + $i * 10, 3, $authnum, $graycolor); } break; case "2": for ($i = 0; $i < $CodeLength; $i++) { //$font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255));
public function createRandImage($randCode = NULL, $width = 60, $height = 24, $mix = 50) { $par = intval($width / 4); $randCode = strval($randCode); $image = imagecreatetruecolor($width, $height); $gray = ImageColorAllocate($image, 55, 55, 55); imagefill($image, 0, 0, $gray); for ($i = 0; $i < 4; $i++) { $text_color = imagecolorallocate($image, rand(128, 255), rand(128, 255), rand(128, 255)); imagettftext($image, 14, intval(rand(0, 60)), 10 + $i * $par, 23 + rand(3, 8), $text_color, 'files/public/font/Symbola.ttf', $randCode[$i]); } for ($i = 0; $i < 250; $i++) { $randcolor = ImageColorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($image, rand(1, $width), rand(1, $height), $randcolor); } imagepng($image); imagedestroy($image); return $randCode; }
public static function drawCaptcha($rndstring, $rndcodelen, $blackbg) { $specRndList = array('A', 'B', 'E', 'G', 'H', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'U', 'W', 'X', 'Y', 'Z'); //创建图片,并设置背景色 $VD_WIDTH = 110; $VD_HEIGHT = 30; $im = imagecreate($VD_WIDTH, $VD_HEIGHT); if ($blackbg) { ImageColorAllocate($im, 0, 0, 0); } else { ImageColorAllocate($im, 255, 255, 255); } //画边框 if ($blackbg) { $bordercolor = ImageColorAllocate($im, 0, 0, 0); } else { $bordercolor = ImageColorAllocate($im, 0xff, 0xff, 0xff); } imagefilledrectangle($im, 0, 0, $VD_WIDTH, $VD_HEIGHT, $bordercolor); for ($i = 0; $i < 100; $i++) { $randcolor = ImageColorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($im, rand() % 70, rand() % 30, $randcolor); } for ($i = 0; $i < 2; $i++) { if ($blackbg) { $rndCodeColor = ImageColorAllocate($im, mt_rand(20, 130), mt_rand(50, 140), mt_rand(50, 160)); } else { $rndCodeColor = ImageColorAllocate($im, mt_rand(200, 220), mt_rand(150, 190), mt_rand(160, 190)); } imagearc($im, mt_rand(-5, $VD_WIDTH), mt_rand(-5, $VD_HEIGHT), mt_rand(30, 120), 35, mt_rand(40, 70), mt_rand(30, 50), $rndCodeColor); } if ($blackbg) { $rndCodeColor = ImageColorAllocate($im, mt_rand(180, 255), mt_rand(160, 255), mt_rand(190, 255)); } else { $rndCodeColor = ImageColorAllocate($im, mt_rand(15, 220), mt_rand(35, 160), mt_rand(45, 190)); } //输出文字 for ($i = 0; $i < $rndcodelen; $i++) { $bc = mt_rand(0, 1); $rndstring[$i] = strtoupper($rndstring[$i]); $fsize = mt_rand(4, 5); $ptry = mt_rand(1, 5); //imagestring($im, $fsize, $i*12+5, $ptry, $rndstring[$i], $rndCodeColor); imagettftext($im, 20, mt_rand(-20, 20), $i * 20 + 5, $ptry + 23, $rndCodeColor, __DIR__ . '/../fonts/ARIAL.TTF', $rndstring[$i]); if (in_array($rndstring[$i], $specRndList)) { for ($j = 0; $j < 5; $j++) { imagesetpixel($im, $i * 20 + 5 + mt_rand(0, imagefontwidth($fsize)), $ptry + mt_rand(1, imagefontheight($fsize)), $bordercolor); } } else { for ($j = 0; $j < 5; $j++) { imagesetpixel($im, $i * 20 + 5 + mt_rand(0, imagefontwidth($fsize)), $ptry + mt_rand(1, imagefontheight($fsize)), $rndCodeColor); } } } return $im; }
function getAuthImage($paramArr) { $options = array('width' => 80, 'height' => 20, 'numCnt' => 4, 'text' => 'ABCD'); if (is_array($paramArr)) { $options = array_merge($options, $paramArr); } extract($options); $font = LJL_API_ROOT . '/Config/Fonts/Ga.ttf'; $len = $numCnt; $font_size = $width / ($len + 1) - 5; $size = $width / $len; $box = imagettfbbox($size, 0, $font, $text); $textWidth = $box[2] - $box[0]; $textHeight = $box[1] - $box[7]; $x = ($width - $textWidth) / 2; $y = ($height - $textHeight) / 2 + $size; $im_x = $width; $im_y = $height; $im = imagecreatetruecolor($im_x, $im_y); $text_c = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 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); 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, 10); //角度 $size = 28; //imagettftext($im, $size, $an, 15+$i*$size, 35, $text_c, $font, $tmp); imagettftext($im, $font_size, rand(-5, 5), $x + $font_size / 2 + ($font_size + 3) * $i, $y, $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 = 160; //干扰像素的数量 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 <= 80; $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); }
public function verifycode() { //生成验证码图片 Header("Content-type: image/PNG"); // 画一张指定宽高的图片 $im = imagecreate(80, 36); // 定义背景颜色 $back = ImageColorAllocate($im, 245, 245, 245); //把背景颜色填充到刚刚画出来的图片中 imagefill($im, 0, 0, $back); $vcodes = ""; srand((double) microtime() * 1000000); //生成5位随机数字或字母(无01无ol防止混淆) $code = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ"; for ($i = 0; $i < 5; $i++) { // 生成随机颜色 $font = ImageColorAllocate($im, rand(0, 255), rand(0, 100), rand(0, 255)); $authnum = $code[rand(0, strlen($code) - 1)]; $vcodes .= $authnum; imagestring($im, 5, 15 + $i * 10, 8, $authnum, $font); } session("v_code", $vcodes); //加入干扰象素 for ($i = 0; $i < 200; $i++) { $randcolor = ImageColorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($im, rand() % 80, rand() % 30, $randcolor); // 画像素点函数 } ImagePNG($im); ImageDestroy($im); }
function get_auth_image() { $im_x = 160; $im_y = 40; $text = $this->_make_rand(4); session_start(); $_SESSION['pass_auth_code'] = strtolower($text); // $this->session->set_flashdata('pass_auth_code', strtolower($text)); // log_message('info',$_SESSION['pass_auth_code']); // var_dump($this->session->flashdata('pass_auth_code')); // die; $im = imagecreatetruecolor($im_x, $im_y); $text_c = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 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 = FCPATH . 'attachments/fonts/t1.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, 10); //角度 $size = 28; @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 = 160; //干扰像素的数量 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 <= 80; $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); }
public static function output($type=0, $length=4, $sessionName = 'VerificationCode'){ self::$length = $length ? $length : 1; self::$sessionName = $sessionName; self::$type = $type ? $type : mt_rand() % 4 +1; if (self::$type == 4) { self::$fontSize[0] =16; self::$fontSize[1] =18; self::$fontFace = self::$fontChinese[mt_rand() % count(self::$fontChinese)]; } else{ self::$fontSize[0] =20; self::$fontSize[1] =24; self::$fontFace = self::$font[mt_rand() % count(self::$font)]; } self::$width = (int)(24 * self::$length+tan(deg2rad(30))*24); //self::$width = 110; self::$height = 40; if (strlen(self::$code)==0) self::createCode(); //生成码值 self::$image=imagecreatetruecolor(self::$width,self::$height); //两种生成模式的图片 if (self::$type == 4) { self::createImage1(); } else{ if (mt_rand()%2) { self::createImage1(); } else self::createImage2(); } //加入干扰象素; for($i=0; $i<self::$width; $i++){ $randcolor = ImageColorallocate(self::$image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); imagesetpixel(self::$image, mt_rand()%self::$width , mt_rand()%self::$height , $randcolor); } header("Content-type: image/png"); imagepng(self::$image); imagedestroy(self::$image); /*echo self::$type; echo ' - '; echo self::$code;*/ }
public function create() { //验证码类型 switch ($this->type) { //数字+字母结合字符验证码 case 'char': //验证码字符串,去除【0、1、o、l】防止互相混淆 $str = '23456789abcdefghijkmnpqrstuvwxyz'; //验证码 $code = ''; for ($i = 0; $i < $this->length; $i++) { //去除字符串里面的任一字符拼接 $code .= $str[mt_rand(0, strlen($str) - 1)]; } //创建的图片宽、高 $im = imagecreate($this->width, $this->height); //给图像分配颜色 $black = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120)); //干扰色 $gray = imagecolorallocate($im, 118, 151, 199); //边框色 $bgcolor = imagecolorallocate($im, 235, 236, 237); //背景色 //画背景 imagefilledrectangle($im, 0, 0, $this->width, $this->height, $bgcolor); //画边框 imagerectangle($im, 0, 0, $this->width - 1, $this->height - 1, $gray); //imagefill($im, 0, 0, $bgcolor); //在画布上随机生成大量点,起干扰作用; for ($i = 0; $i < 80; $i++) { //画一个单一像素 imagesetpixel($im, rand(0, $this->width), rand(0, $this->height), $black); } //将字符随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成 $strx = rand($this->padding_x[0], $this->padding_x[1]); //验证码字符X轴宽度 for ($i = 0; $i < $this->length; $i++) { //验证码字符Y轴高度 $stry = rand($this->padding_y[0], $this->padding_y[1]); //水平地画一行字符串 imagestring($im, $this->fontSize, $strx, $stry, substr($code, $i, 1), $black); //每个字符间的间距:步长 $strx += rand($this->str_step[0], $this->str_step[1]); } break; //数字验证码 //数字验证码 case 'number': $code = ''; for ($i = 0; $i < $this->length; $i++) { $code .= rand(0, 9); } //4位验证码也可以用rand(1000,9999)直接生成 //将生成的验证码写入session,备验证页面使用 $im = imagecreate($this->width, $this->height); $black = imagecolorallocate($im, 0, 0, 0); $gray = imagecolorallocate($im, 200, 200, 200); $bgcolor = imagecolorallocate($im, 255, 255, 255); imagefill($im, 0, 0, $gray); //画边框 imagerectangle($im, 0, 0, $this->width - 1, $this->height - 1, $black); //随机绘制两条虚线,起干扰作用 $style = array($black, $black, $black, $black, $black, $gray, $gray, $gray, $gray, $gray); imagesetstyle($im, $style); for ($i = 0; $i < $this->line; $i++) { imageline($im, 0, rand(0, $this->height), $this->width, rand(0, $this->height), IMG_COLOR_STYLED); } //在画布上随机生成大量黑点,起干扰作用; for ($i = 0; $i < $this->point; $i++) { imagesetpixel($im, rand(0, $this->width), rand(0, $this->height), $black); } //将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成 $strx = rand($this->padding_x[0], $this->padding_x[1]); //验证码字符X轴宽度 for ($i = 0; $i < $this->length; $i++) { //验证码字符Y轴高度 $stry = rand($this->padding_y[0], $this->padding_y[1]); //水平地画一行字符串 imagestring($im, $this->fontSize, $strx, $stry, substr($code, $i, 1), $black); //每个字符间的间距:步长 $strx += rand($this->str_step[0], $this->str_step[1]); } break; //中文验证码 //中文验证码 case 'zh': $fontface = FRAME_CORE . "/Font/" . $this->font; //字体文件 $str = "们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这主中人上为来分生对于学下级地个用同行面说种过命度革而多子后自社加小机也经力线本电高量长党得实家定深法表着水理化争现所二起政三好十战无农使性前等反体合斗路图把结第里正新开论之物从当两些还天资事队批点育重其思与间内去因件日利相由压员气业代全组数果期导平各基或月毛然如应形想制心样干都向变关问比展那它最及外没看治提五解系林者米群头意只明四道马认次文通但条较克又公孔领军流入接席位情运器并飞原油放立题质指建区验活众很教决特此常石强极土少已根共直团统式转别造切九你取西持总料连任志观调七么山程百报更见必真保热委手改管处己将修支识病象几先老光专什六型具示复安带每东增则完风回南广劳轮科北打积车计给节做务被整联步类集号列温装即毫知轴研单色坚据速防史拉世设达尔场织历花受求传口断况采精金界品判参层止边清至万确究书术状厂须离再目海交权且儿青才证低越际八试规斯近注办布门铁需走议县兵固除般引齿千胜细影济白格效置推空配刀叶率述今选养德话查差半敌始片施响收华觉备名红续均药标记难存测士身紧液派准斤角降维板许破述技消底床田势端感往神便贺村构照容非搞亚磨族火段算适讲按值美态黄易彪服早班麦削信排台声该击素张密害侯草何树肥继右属市严径螺检左页抗苏显苦英快称坏移约巴材省黑武培著河帝仅针怎植京助升王眼她抓含苗副杂普谈围食射源例致酸旧却充足短划剂宣环落首尺波承粉践府鱼随考刻靠够满夫失包住促枝局菌杆周护岩师举曲春元超负砂封换太模贫减阳扬江析亩木言球朝医校古呢稻宋听唯输滑站另卫字鼓刚写刘微略范供阿块某功套友限项余倒卷创律雨让骨远帮初皮播优占死毒圈伟季训控激找叫云互跟裂粮粒母练塞钢顶策双留误础吸阻故寸盾晚丝女散焊功株亲院冷彻弹错散商视艺灭版烈零室轻血倍缺厘泵察绝富城冲喷壤简否柱李望盘磁雄似困巩益洲脱投送奴侧润盖挥距星松送获兴独官混纪依未突架宽冬章湿偏纹吃执阀矿寨责熟稳夺硬价努翻奇甲预职评读背协损棉侵灰虽矛厚罗泥辟告卵箱掌氧恩爱停曾溶营终纲孟钱待尽俄缩沙退陈讨奋械载胞幼哪剥迫旋征槽倒握担仍呀鲜吧卡粗介钻逐弱脚怕盐末阴丰雾冠丙街莱贝辐肠付吉渗瑞惊顿挤秒悬姆烂森糖圣凹陶词迟蚕亿矩康遵牧遭幅园腔订香肉弟屋敏恢忘编印蜂急拿扩伤飞露核缘游振操央伍域甚迅辉异序免纸夜乡久隶缸夹念兰映沟乙吗儒杀汽磷艰晶插埃燃欢铁补咱芽永瓦倾阵碳演威附牙芽永瓦斜灌欧献顺猪洋腐请透司危括脉宜笑若尾束壮暴企菜穗楚汉愈绿拖牛份染既秋遍锻玉夏疗尖殖井费州访吹荣铜沿替滚客召旱悟刺脑措贯藏敢令隙炉壳硫煤迎铸粘探临薄旬善福纵择礼愿伏残雷延烟句纯渐耕跑泽慢栽鲁赤繁境潮横掉锥希池败船假亮谓托伙哲怀割摆贡呈劲财仪沉炼麻罪祖息车穿货销齐鼠抽画饲龙库守筑房歌寒喜哥洗蚀废纳腹乎录镜妇恶脂庄擦险赞钟摇典柄辩竹谷卖乱虚桥奥伯赶垂途额壁网截野遗静谋弄挂课镇妄盛耐援扎虑键归符庆聚绕摩忙舞遇索顾胶羊湖钉仁音迹碎伸灯避泛亡答勇频皇柳哈揭甘诺概宪浓岛袭谁洪谢炮浇斑讯懂灵蛋闭孩释乳巨徒私银伊景坦累匀霉杜乐勒隔弯绩招绍胡呼痛峰零柴簧午跳居尚丁秦稍追梁折耗碱殊岗挖氏刃剧堆赫荷胸衡勤膜篇登驻案刊秧缓凸役剪川雪链渔啦脸户洛孢勃盟买杨宗焦赛旗滤硅炭股坐蒸凝竟陷枪黎救冒暗洞犯筒您宋弧爆谬涂味津臂障褐陆啊健尊豆拔莫抵桑坡缝警挑污冰柬嘴啥饭塑寄赵喊垫丹渡耳刨虎笔稀昆浪萨茶滴浅拥穴覆伦娘吨浸袖珠雌妈紫戏塔锤震岁貌洁剖牢锋疑霸闪埔猛诉刷狠忽灾闹乔唐漏闻沈熔氯荒茎男凡抢像浆旁玻亦忠唱蒙予纷捕锁尤乘乌智淡允叛畜俘摸锈扫毕璃宝芯爷鉴秘净蒋钙肩腾枯抛轨堂拌爸循诱祝励肯酒绳穷塘燥泡袋朗喂铝软渠颗惯贸粪综墙趋彼届墨碍启逆卸航衣孙龄岭骗休借"; $str = iconv('utf-8', 'gbk', $str); $code = ""; for ($i = 0; $i < $this->length; $i++) { $Xi = mt_rand(0, strlen($str) / 2); if ($Xi % 2) { $Xi += 1; } $code .= substr($str, $Xi, 2); } $im = imagecreatetruecolor($this->width, $this->height); $bkcolor = imagecolorallocate($im, 250, 250, 250); imagefill($im, 0, 0, $bkcolor); /***添加干扰***/ for ($i = 0; $i < $this->line; $i++) { $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imagearc($im, mt_rand(-10, $this->width), mt_rand(-10, $this->height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor); } for ($i = 0; $i < 255; $i++) { $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imagesetpixel($im, mt_rand(0, $this->width), mt_rand(0, $this->height), $fontcolor); //在画布上随机生成大量黑点,起干扰作用; for ($j = 0; $j < $this->point; $j++) { imagesetpixel($im, mt_rand(0, $this->width), mt_rand(0, $this->height), $fontcolor); } } /***********内容*********/ for ($i = 0; $i < $this->length; $i++) { $fontcolor = imagecolorallocate($im, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); //这样保证随机出来的颜色较深。 $codex = iconv("GBK", "UTF-8", substr($code, $i * 2, 2)); imagettftext($im, mt_rand(14, 18), mt_rand(-60, 60), 30 * $i + 20, mt_rand(30, 35), $fontcolor, $fontface, $codex); } break; //数学表达式,计算题 //数学表达式,计算题 case 'math': $im = imagecreate($this->width, $this->height); //imagecolorallocate($im, 14, 114, 180); // background color $red = imagecolorallocate($im, 255, 0, 0); $white = imagecolorallocate($im, 255, 255, 255); $num1 = rand(1, 20); $num2 = rand(1, 20); $code = $num1 + $num2; $gray = imagecolorallocate($im, 118, 151, 199); $black = imagecolorallocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); //画背景 imagefilledrectangle($im, 0, 0, $this->width, $this->height, $black); //在画布上随机生成大量黑点,起干扰作用; for ($i = 0; $i < $this->point; $i++) { imagesetpixel($im, rand(0, $this->width), rand(0, $this->height), $gray); } // imagestring($im, $this->fontSize, 5, 4, $num1, $red); // imagestring($im, $this->fontSize, 30, 3, "+", $red); // imagestring($im, $this->fontSize, 45, 4, $num2, $red); // imagestring($im, $this->fontSize, 70, 3, "=", $red); // imagestring($im, $this->fontSize, 80, 2, "?", $white); $padding_x = rand($this->padding_x[0], $this->padding_x[1]); $padding_y = rand($this->padding_y[0], $this->padding_y[1]); imagestring($im, $this->fontSize, $padding_x, $padding_y, $num1, $red); imagestring($im, $this->fontSize, $padding_x + 20, $padding_y, "+", $red); imagestring($im, $this->fontSize, $padding_x + 40, $padding_y, $num2, $red); imagestring($im, $this->fontSize, $padding_x + 60, $padding_y, "=", $red); imagestring($im, $this->fontSize, $padding_x + 80, $padding_y, "?", $white); break; //样式1 //样式1 case 'style1': //验证码字符串 $str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $code = ''; for ($i = 0; $i < $this->length; $i++) { $code .= $str[rand(0, strlen($str) - 1)]; } $im = imagecreatetruecolor($this->width, $this->height); $text_c = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 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 = FRAME_CORE . '/Font/' . $this->font; for ($i = 0; $i < $this->length; $i++) { $tmp = substr($code, $i, 1); $array = array(-1, 1); $p = array_rand($array); $an = $array[$p] * mt_rand(1, 10); //角度 $size = $this->fontSize; //imagettftext($im, $size, $an, 15+$i*$size, 35, $text_c, $font, $tmp); imagettftext($im, $size, $an, rand($this->padding_x[0], $this->padding_x[1]) + $i * $size, rand($this->padding_y[0], $this->padding_y[1]), $text_c, $font, $tmp); } $distortion_im = imagecreatetruecolor($this->width, $this->height); imagefill($distortion_im, 16, 13, $buttum_c); for ($i = 0; $i < $this->width; $i++) { for ($j = 0; $j < $this->height; $j++) { $rgb = imagecolorat($im, $i, $j); if ((int) ($i + 20 + sin($j / $this->height * 2 * M_PI) * 10) <= imagesx($distortion_im) && (int) ($i + 20 + sin($j / $this->height * 2 * M_PI) * 10) >= 0) { imagesetpixel($distortion_im, (int) ($i + 10 + sin($j / $this->height * 2 * M_PI - M_PI * 0.1) * 4), $j, $rgb); } } } //加入干扰象素; $count = 160; //干扰像素的数量 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() % $this->width, mt_rand() % $this->height, $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 <= 80; $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); break; } //将生成的验证码保存到session中 $_SESSION['verify'] = trim(strtoupper($code)); //$_SESSION['verify'] = md5(strtoupper($code)); //创建png格式图片,定义颜色值 header("Content-type: image/PNG"); //以 PNG 格式将图像输出到浏览器或文件 imagepng($im); //销毁一图像 imagedestroy($im); }
<?php session_start(); //將隨機數存入session中 //生成驗證碼圖片 Header("Content-type: image/PNG"); srand((double) microtime() * 1000000); //播下一個生成隨機數字的種子,以方便下面隨機數生成的使用 $_SESSION['authnum'] = ""; $im = imagecreate(62, 22); //制定圖片背景大小 $black = ImageColorAllocate($im, 0, 0, 0); //設定三種顏色 $white = ImageColorAllocate($im, 255, 255, 255); $gray = ImageColorAllocate($im, 200, 200, 200); imagefill($im, 0, 0, $gray); //採用區域填充法,設定(0,0) while (($authnum = rand() % 100000) < 10000) { } //將四位整數驗證碼繪入圖片 $_SESSION['authnum'] = $authnum; imagestring($im, 5, 10, 3, $authnum, $black); // 用 col 顏色將字符串 s 畫到 image 所代表的圖像的 x,y 座標處(圖像的左上角為 0, 0)。 //如果 font 是 1,2,3,4 或 5,則使用內置字體 for ($i = 0; $i < 200; $i++) { $randcolor = ImageColorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($im, rand() % 70, rand() % 30, $randcolor); } ImagePNG($im); ImageDestroy($im);
/** * Created by PhpStorm. * User: zhuangshaoxiong * Date: 2015/12/3 * Time: 20:19 */ $string = "abcdefghijklmnopqrstuvwxyz0123456789"; $str = ""; for ($i = 0; $i < 4; $i++) { $pos = rand(0, 35); $str .= $string[$pos]; } session_start(); $_SESSION['checknode'] = $str; $img_handle = Imagecreate(100, 20); $back_color = ImageColorAllocate($img_handle, 255, 255, 255); $txt_color = ImageColorAllocate($img_handle, 0, 0, 0); echo ddddd; for ($i = 0; $i < 3; $i++) { $line = ImageColorAllocate($img_handle, rand(0, 255), rand(0, 255), rand(0, 255)); Imageline($img_handle, rand(0, 15), rand(0, 15), rand(100, 150), rand(10, 50), $line); } for ($i = 0; $i < 200; $i++) { $randcolor = ImageColorallocate($img_handle, rand(0, 255), rand(0, 255), rand(0, 255)); Imagesetpixel($img_handle, rand() % 100, rand() % 50, $randcolor); } Imagefill($img_handle, 0, 0, $back_color); ImageString($img_handle, 28, 10, 0, $str, $txt_color); ob_clean(); header("Content-Type: image/png;"); Imagepng($img_handle);