/** * Created by PhpStorm. * User: administrator * Date: 2015/7/8 * Time: 13:20 */ function captcha() { require_once 'string.func.php'; session_start(); //使用gd库来作验证码 //创建画布 $image = imagecreatetruecolor(100, 30); //背景色,为白色 $bgColor = imagecolorallocate($image, 255, 255, 255); //填充矩形填充画布 imagefill($image, 0, 0, $bgColor); $string = buildRandomString(); $sess_name = 'captcha'; $_SESSION[$sess_name] = $string; for ($i = 0; $i < 4; $i++) { $fontSize = 6; $fontColor = imagecolorallocate($image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); $x = $i * 100 / 4 + mt_rand(5, 10); $y = mt_rand(5, 10); imagestring($image, $fontSize, $x, $y, substr($string, $i, 1), $fontColor); } //增加点干扰元素 for ($i = 0; $i < 200; $i++) { $pointColor = imagecolorallocate($image, rand(50, 200), rand(250, 200), rand(50, 200)); imagesetpixel($image, mt_rand(1, 99), mt_rand(1, 29), $pointColor); } //增加线干扰元素 for ($i = 0; $i < 3; $i++) { $lineColor = imagecolorallocate($image, mt_rand(80, 220), mt_rand(80, 220), mt_rand(80, 220)); imageline($image, mt_rand(1, 99), mt_rand(1, 29), mt_rand(1, 99), mt_rand(1, 29), $lineColor); } header('content-type:images/gif'); imagegif($image); imagedestroy($image); }
function verifyImage($type = 1, $length = 4, $pixel = 0, $line = 10, $sses_name = "verify") { // 创建画布 $width = 80; $height = 28; $image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($image, 255, 255, 255); $balck = imagecolorallocate($image, 0, 0, 0); // 用填充矩形填充画布 imagefilledrectangle($image, 1, 1, $width - 2, $height - 2, $white); $chars = buildRandomString($type, $length); $_SESSION[$sses_name] = $chars; for ($i = 0; $i < $length; $i++) { $size = mt_rand(14, 18); $angle = mt_rand(-15, 15); $x = 3 + $i * $size; $y = mt_rand(18, 22); $fontfile = "../fonts/" . "STFANGSO.TTF"; $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); $text = substr($chars, $i, 1); imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); } // 加点 for ($i = 0; $i < $pixel; $i++) { imagesetpixel($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), $balck); } // 加直线 for ($i = 0; $i < $line; $i++) { $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); imageline($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), mt_rand(0, $width - 1), mt_rand(0, $height - 1), $color); } header("content-type:image/jpg"); imagegif($image); imagedestroy($image); }
function verifyImage($type = 1, $length = 4, $pixel = 0, $line = 0, $sess_name = "verify") { //session_start();在include.php文件中有執行,不能重複執行 // 创建画布 $width = 80; $height = 30; $image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($image, 255, 255, 255); //定義畫筆顏色爲白色 $black = imagecolorallocate($image, 0, 0, 0); //定義畫筆顏色爲黑色 // 用填充矩形填充画布 imagefilledrectangle($image, 1, 1, $width - 2, $height - 2, $white); // 白色填充画布 $chars = buildRandomString($type, $length); //string.func.php中定義的函數 $_SESSION[$sess_name] = $chars; //將驗證碼放入session中 $fontfiles = array('MSYH.TTF', 'MSYHBD.TTF', 'SIMSUN.TTC', 'SIMYOU.TTF'); // 可用字体 for ($i = 0; $i < $length; $i++) { $size = mt_rand(14, 18); // 字体大小 $angle = mt_rand(-15, 15); // 字体角度 $x = 5 + $i * $size; $y = mt_rand(20, 26); //隨機產生一個20~26之間的數 $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); // 字体颜色 $fontfile = '../fonts/' . $fontfiles[mt_rand(0, count($fontfiles) - 1)]; // 字体 $text = substr($chars, $i, 1); // 显示的字符 imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); } // 添加画布干扰元素---点 if ($pixel) { for ($i = 0; $i < $pixel; $i++) { imagesetpixel($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), $black); } } // 添加畫布干擾元素----線 if ($line) { $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); // 字体颜色 for ($i = 0; $i < $line; $i++) { imageline($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), mt_rand(0, $width - 1), mt_rand(0, $height - 1), $color); } } header("content-type:image/gif"); imagegif($image); //顯示畫布 imagedestroy($image); //銷燬圖像資源 }
function verifyImage($type = 1, $length = 4, $pixel = 0, $line = 0, $sess_name = "verify") { session_start(); // 创建画布 $width = 80; $height = 30; // 画布宽高 $image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); // 用填充矩阵填充画布 imagefilledrectangle($image, 1, 1, $width - 2, $height - 2, $white); $chars = buildRandomString($type, $length); // $sess_name = "verify"; $_SESSION[$sess_name] = $chars; $fontfiles = array("SIMYOU.TTF"); // 存放字体文件的数组 for ($i = 0; $i < $length; $i++) { $size = mt_rand(14, 18); // 字体大小 $angle = mt_rand(-15, 15); // 角度 $x = 5 + $i * $size; // 坐标 $y = mt_rand(20, 26); // 坐标 $fontfile = "../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)]; // 随机取出一个字体文件 $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); // 产生随机颜色 $text = substr($chars, $i, 1); // 每次按位置$i取出一个字符 imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); } // 增加干扰元素 // 增加黑点 if ($pixel) { for ($i = 0; $i < $pixel; $i++) { imagesetpixel($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), $black); } } // 增加线条 if ($line) { for ($i = 1; $i < $line; $i++) { $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); imageline($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), mt_rand(0, $width - 1), mt_rand(0, $height - 1), $color); } } header("content-type:image/gif"); imagegif($image); // 显示验证码图片 imagedestroy($image); //销毁该图片资源 }
function verifyImage($type = 1, $length = 4, $pixel = 0, $line = 0, $sess_name = "verify") { // NOTE: 在include.php中已经调用了session_start,这里再调用会有问题 // session_start(); // 创建画布 $width = 100; $height = 28; $image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); // 用填充矩形填充画布 imagefilledrectangle($image, 1, 1, $width - 2, $height - 2, $white); $chars = buildRandomString($type, $length); $_SESSION['verify'] = $chars; //comment the gif header then debug // echo "_SESSION verify:"; // echo $_SESSION['verify']; // $fontfiles = array ("MSYH.TTF", "MSYHBD.TTF", "SIMLI.TTF", "SIMSUN.TTC", "SIMYOU.TTF", "STZHONGS.TTF" ); $fontfiles = array("SIMYOU.TTF"); // 由于字体文件比较大,就只保留一个字体,如果有需要的同学可以自己添加字体,字体在你的电脑中的fonts文件夹里有,直接运行输入fonts就能看到相应字体 for ($i = 0; $i < $length; $i++) { $size = mt_rand(14, 18); $angle = mt_rand(-15, 15); $x = 5 + $i * $size; $y = mt_rand(20, 26); $fontfile = "./fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)]; $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); $text = substr($chars, $i, 1); imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); } if ($pixel) { for ($i = 0; $i < 50; $i++) { imagesetpixel($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), $black); } } if ($line) { for ($i = 1; $i < $line; $i++) { $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); imageline($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), mt_rand(0, $width - 1), mt_rand(0, $height - 1), $color); } } // echo $chars; // 图片保存到文件 // $imgname='D:\wwwphp\BoyStyle\data\test.jpg'; // imagejpeg($image,$imgname); //通过header设置输出到浏览器 header("content-type:image/gif"); imagegif($image); imagedestroy($image); }
function verifyImage($type = 1, $length = 4, $pixel = 0, $line = 0, $sess_name = "verify") { session_start(); //1.创建画布 $width = 80; $height = 28; $image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); //2.用填充矩形填充画布 imagefilledrectangle($image, 1, 1, $width - 2, $height - 2, $white); //3.生成随机字符串 $chars = buildRandomString($type, $length); //echo $chars; $_SESSION[$sess_name] = $chars; $fontfiles = array("msyh.ttf", "msyhbd.ttf", "simkai.ttf", "SIMLI.TTF", "simsun.ttc", "SIMYOU.TTF"); //4.将字符串循环写入画布 for ($i = 0; $i < $length; $i++) { $size = mt_rand(14, 18); //字体大小 $angle = mt_rand(-15, 15); //角度 $x = 5 + $i * $size; //x坐标 $y = mt_rand(20, 26); //y坐标 $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); $fontfile = "../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)]; $text = substr($chars, $i, 1); imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); } //5.添加点 if ($pixel) { for ($i = 0; $i < $pixel; $i++) { imagesetpixel($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), $black); } } //6.添加线段 if ($line) { for ($i = 1; $i < $line; $i++) { $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); imageline($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), mt_rand(0, $width - 1), mt_rand(0, $height - 1), $color); } } header("content-type:image/gif"); imagegif($image); imagedestroy($image); }
function verifyImage($type = 1, $length = 4, $pixel = 0, $line = 0, $sess_name = "verify") { //创建画布 $width = 80; $height = 28; $image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); //用填充矩形填充画布 imagefilledrectangle将图片的封闭长方形区域着色。参数 x1、y1 及 x2、y2 分别为矩形对角线的坐标。参数 col 表示欲涂上的颜色。 imagefilledrectangle($image, 1, 1, $width - 2, $height - 2, $white); $chars = buildRandomString($type, $length); $_SESSION[$sess_name] = $chars; //$fontfiles = array ("MSYH.TTF","SIMLI.TTF","SIMSNU.TTC","SIMYOU.TTF","STZHONGS.TTF"); $fontfiles = array("Bradley Hand Bold.ttf"); //由于字体文件比较大,就保留一个字体 for ($i = 0; $i < $length; $i++) { $size = mt_rand(14, 18); $angle = mt_rand(-15, 15); $x = 5 + $i * $size; $y = mt_rand(20, 26); $fontfile = "../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)]; $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); $text = substr($chars, $i, 1); //imagettftext() 将字符串 text 画到 image 所代表的图像上,从坐标 x,y(左上角为 0, 0)开始,角度为 angle,颜色为 color,使用 fontfile 所指定的 truetype 字体文件。 imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); } //干扰元素 - 点 if ($pixel) { for ($i = 0; $i < 50; $i++) { imagesetpixel($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), $black); } } //干扰元素 - 线 if ($line) { for ($i = 1; $i < $line; $i++) { $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); imageline($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), mt_rand(0, $width - 1), mt_rand(0, $height - 1), $color); } } header("content-type:image/gif"); imagegif($image); imagedestroy($image); }
function identifying_code_Image($type = 1, $length = 4, $line = 1, $pixel = 20, $sess_name = "identifying_code") { //session_start(); //创建真色彩画布 $width = 80; $height = 30; $image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); //用填充矩形填充画布 imagefilledrectangle($image, 1, 1, $width - 2, $height - 2, $white); // 定制验证码 $chars = buildRandomString($type, $length); $_SESSION[$sess_name] = $chars; $fontfiles = array("SIMYOU.TTF"); for ($i = 0; $i < $length; $i++) { $size = mt_rand(14, 18); $angle = mt_rand(-16, 16); $x = 5 + $i * $size; $y = mt_rand(20, 26); $fontfile = "../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)]; $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); $text = substr($chars, $i, 1); imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); } //选择给验证码加像素点,降低易辩性 if ($pixel) { for ($i = 0; $i < $pixel; $i++) { imagesetpixel($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), $black); } } //给验证码加line if ($line) { for ($i = 0; $i < $line; $i++) { $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); imageline($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), mt_rand(0, $width - 1), mt_rand(0, $height - 1), $color); } } //选择给验证码加像素点,降低易辩性 END header("content-type:image/gif"); imagegif($image); imagedestroy($image); }
function verifyImage($type = 1, $length = 4, $pixel = 0, $line = 0, $sess_name = "verify") { if (!isset($_SESSION)) { session_start(); } //锟斤拷锟斤拷锟斤拷锟斤拷 $width = 100; $height = 65; $image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷浠拷锟� imagefilledrectangle($image, 1, 1, $width - 2, $height - 2, $white); $chars = buildRandomString($type, $length); $_SESSION[$sess_name] = $chars; //$fontfiles = array ("MSYH.TTF", "MSYHBD.TTF", "SIMLI.TTF", "SIMSUN.TTC", "SIMYOU.TTF", "STZHONGS.TTF" ); $fontfiles = array("SIMYOU.TTF"); //锟斤拷锟斤拷锟斤拷锟斤拷锟侥硷拷锟饺较大,撅拷只锟斤拷锟斤拷一锟斤拷锟斤拷锟藉,锟斤拷锟斤拷锟斤拷锟揭拷锟酵э拷锟斤拷锟斤拷约锟斤拷锟斤拷锟斤拷锟藉,锟斤拷锟斤拷锟斤拷锟斤拷牡锟斤拷锟斤拷械锟絝onts锟侥硷拷锟斤拷锟斤拷锟叫o拷直锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷fonts锟斤拷锟杰匡拷锟斤拷锟斤拷应锟斤拷锟斤拷 for ($i = 0; $i < $length; $i++) { $size = mt_rand(14, 18); $angle = mt_rand(-15, 15); $x = 5 + $i * $size; $y = mt_rand(20, 26); $fontfile = "../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)]; $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); $text = substr($chars, $i, 1); imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); } if ($pixel) { for ($i = 0; $i < 50; $i++) { imagesetpixel($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), $black); } } if ($line) { for ($i = 1; $i < $line; $i++) { $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); imageline($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), mt_rand(0, $width - 1), mt_rand(0, $height - 1), $color); } } header("content-type:image/gif"); imagegif($image); imagedestroy($image); }
function verifyImage($type = 1, $length = 4, $pixel = 0, $line = 0, $sess_name = 'verify') { session_start(); //创建画布 $width = 80; $height = 30; $image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($image, 255, 255, 255); //白色 $black = imagecolorallocate($image, 0, 0, 0); //黑色 //用填充矩形填充画布 imagefilledrectangle($image, 1, 1, $width - 2, $height - 2, $white); $chars = buildRandomString($type, $length); $_SESSION[$sess_name] = $chars; $fontfiles = array("SEGOEUII.TTF", "TAHOMA.TTF", "VERDANA.TTF"); for ($i = 0; $i < $length; $i++) { $size = mt_rand(14, 18); $angle = mt_rand(-15, 15); $x = 5 + $i * $size; $y = mt_rand(20, 26); $fontfile = "../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)]; $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); $text = substr($chars, $i, 1); imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); } //添加干扰元素--点 if ($pixel) { for ($i = 0; $i < $pixel; $i++) { imagesetpixel($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), $black); } } //添加干扰元素--线 if ($line) { for ($i = 1; $i < $line; $i++) { $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); imageline($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), mt_rand(0, $width - 1), mt_rand(0, $height - 1), $color); } } header('content-type:image/gif'); imagegif($image); imagedestroy($image); }
function verifyImage($type = 1, $length = 4, $pixel = 0, $line = 0, $sess_name = "verify") { session_start(); //创建画布 $width = 80; $height = 28; $image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); //用填充矩形填充画布 imagefilledrectangle($image, 1, 1, $width - 2, $height - 2, $white); $chars = buildRandomString($type, $length); $_SESSION[$sess_name] = $chars; //$fontfiles = array ("MSYH.TTF", "MSYHBD.TTF", "SIMLI.TTF", "SIMSUN.TTC", "SIMYOU.TTF", "STZHONGS.TTF" ); $fontfiles = array("SIMYOU.TTF"); //由于字体文件比较大,就只保留一个字体,如果有需要的同学可以自己添加字体,字体在你的电脑中的fonts文件夹里有,直接运行输入fonts就能看到相应字体 for ($i = 0; $i < $length; $i++) { $size = mt_rand(14, 18); $angle = mt_rand(-15, 15); $x = 5 + $i * $size; $y = mt_rand(20, 26); $fontfile = "../fonts/" . $fontfiles[0]; $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); $text = substr($chars, $i, 1); imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); } if ($pixel) { for ($i = 0; $i < 50; $i++) { imagesetpixel($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), $black); } } if ($line) { for ($i = 1; $i < $line; $i++) { $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); imageline($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), mt_rand(0, $width - 1), mt_rand(0, $height - 1), $color); } } ob_clean(); header("content-type:image/gif"); imagegif($image); imagedestroy($image); }
/** * Created by PhpStorm. * User: ray * Date: 15-5-18 * Time: 下午10:40 */ function verifyImage($type = 1, $length = 4, $pixel = 0, $line = 0, $sess_name = "verify") { //session_start(); // Generate verification code through GD lib // Create canvas $width = 80; $height = 30; $image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); // Fill the canvas with white imagefilledrectangle($image, 1, 1, $width - 2, $height - 2, $white); $chars = buildRandomString($type, $length); $_SESSION[$sess_name] = $chars; $fontfiles = array("SIMLI.TTF", "SIMYOU.TTF", "STLITI.TTF", "STXIHEI.TTF", "STXINWEI.TTF", "STZHONGS.TTF"); for ($i = 0; $i < $length; $i++) { $size = mt_rand(14, 18); $angle = mt_rand(-15, 15); $x = 5 + $i * $size; $y = mt_rand(20, 26); $fontfile = "../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)]; $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); $text = substr($chars, $i, 1); imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); } if ($pixel) { for ($i = 0; $i < $pixel; $i++) { imagesetpixel($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), $black); } } if ($line) { for ($i = 1; $i <= $line; $i++) { $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); imageline($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), mt_rand(0, $width - 1), mt_rand(0, $height - 1), $color); } } header("Content-type:image/gif"); imagegif($image); imagedestroy($image); }
function verifyImage($type = 1, $length = 4, $pixel = 0, $line = 0, $sess_name = "verify") { //session_start(); //create canvas require_once 'string.func.php'; $width = 80; $height = 28; $image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); //use rectangle to fill the canvas imagefilledrectangle($image, 1, 1, $width - 2, $height - 2, $white); $chars = buildRandomString($type, $length); $_SESSION[$sess_name] = $chars; $fontfiles = array("msyh.ttc", "msyhbd.ttc", "msyhl.ttc", "SIMLI.TTF", "simsun.ttc", "SIMYOU.TTF", "STZHONGS.TTF"); for ($i = 0; $i < $length; $i++) { $size = mt_rand(14, 18); $angle = mt_rand(-15, 15); $x = 5 + $i * $size; $y = mt_rand(20, 26); $fontfile = "../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)]; $text = substr($chars, $i, 1); $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(9, 180)); imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); } if ($pixel) { for ($i = 0; $i < 50; $i++) { imagesetpixel($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), $black); } } if ($line) { for ($i = 1; $i < $line; $i++) { $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(9, 180)); imageline($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), mt_rand(0, $width - 1), mt_rand(0, $height - 1), $color); } } header("content-type:image/gif"); imagegif($image); imagedestroy($image); }
function verifyImage($type = 1, $length = 4, $pixel = 0, $line = 0, $sess_name = "verify") { // 创建画布 $width = 80; $height = 28; $image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); // 用矩形填充画布 imagefilledrectangle($image, 1, 1, $width - 2, $width - 2, $white); $type = 3; $length = 4; $chars = buildRandomString($type, $length); $_SESSION[$sess_name] = $chars; $fontfiles = array("Amatic-Bold.ttf", "OpenSans-BoldItalic.ttf"); for ($i = 0; $i < $length; $i++) { $size = mt_rand(14, 18); $angle = mt_rand(-15, 15); $x = 5 + $i * $size; $y = mt_rand(20, 26); $fontfile = "../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)]; $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); $text = substr($chars, $i, 1); imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); } for ($i = 0; $i < $pixel; $i++) { $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); imagesetpixel($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), $color); } for ($i = 0; $i < $line; $i++) { $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); imageline($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), mt_rand(0, $width - 1), mt_rand(0, $height - 1), $color); } header("Content-Type:image/gif"); imagegif($image); imagedestroy($image); }
function verifyImage($type = 1, $length = 4, $pixel = 0, $line = 0, $sess_name = "verify") { $width = 80; $height = 30; $image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 255, 255, 255); imagefilledrectangle($image, 1, 1, $width - 2, $height - 2, $white); $chars = buildRandomString($type, $length); $_SESSION[$sess_name] = $chars; $fontfiles = array("SIMYOU.TTF"); for ($i = 0; $i < $length; $i++) { $size = mt_rand(14, 18); $angle = mt_rand(-15, 15); $x = 5 + $i * $size; $y = mt_rand(20, 26); $fontfile = "../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)]; $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); $text = substr($chars, $i, 1); imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); } if ($pixel) { for ($i = 0; $i < $pixel; $i++) { imagesetpixel($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), $black); } } if ($line) { for ($i = 0; $i < $line; $i++) { $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); imageline($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), mt_rand(0, $width - 1), mt_rand(0, $height - 1), $color); } } header("content-type:image/gif"); imagegif($image); imagedestroy($image); }
function verifyImage($type = 1, $length = 4, $pixel = 0, $line = 0, $sess_name = "verify") { session_start(); //0b_clean()函数丢弃输出缓冲区中的内容 ob_clean(); //通过GD库做验证码 //创建画布宽、高 $width = 80; $height = 28; //imagecreatetruecolor()函数新建一个真彩色图像,返回一个图像标示符,代表了一幅大小为X、Y(两个参数)的黑色图像 $image = imagecreatetruecolor($width, $height); //imagecolorallocate()为一副图像分配颜色,语法:imagecolorallocate(resource $image,$red,$green,$blue) //填充画布颜色,白色255,255,255,画笔颜色,黑色0,0,0 $white = imagecolorallocate($image, 255, 255, 255); //$black创建画笔颜色 $black = imagecolorallocate($image, 0, 0, 0); //imagefilledrectangle()函数,画一矩形并填充,语法:imagefilledrectangle(resource $image,$x1,$y1,$x2,$y2,int $color) //用填充矩形填充画布,把画布变成白色 imagefilledrectangle($image, 1, 1, $width - 2, $height - 2, $white); //引用string.func.php文件内的buildRandomString()函数产生随机数 $chars = buildRandomString($type, $length); //把该验证码赋值给$sess_name,以便后面与用户输入的进行验证对比 $_SESSION['$sess_name'] = $chars; //因字体文件太大,上传GitHub及下载时过分耗时,故删减成2个字体文件,其余字体可自行添加 //$fontfiles = array("MSYH.TTF","MSYHBD.TTF","SIMLI.TTF","SIMSUN.TTC","SIMYOU.TTF","STZHONGS.TTF"); $fontfiles = array("SIMLI.TTF", "SIMYOU.TTF"); //for循环生成随机验证码 for ($i = 0; $i < $length; $i++) { //mt_rand()函数,使用Mersenne Twister算法返回随机整数 //$size变量定义字体大小 $size = mt_rand(14, 18); //angle变量定义字体旋转角度 $angle = mt_rand(-15, 15); //$x、$y定义了字体在图片内的位置 $x = 5 + $i * $size; $y = mt_rand(20, 26); $fontfile = "../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)]; $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); $text = substr($chars, $i, 1); //imagettftext()函数用TrueType字体向图像写入字体 //array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text ) //这里图像源为$image包含的已创建的图床,$size、$angle定义了里面字体的大小及旋转的角度,$x、$y定义了字体在图片内的位置,$color定义了填充的颜色,$fontfile定义了字体路径,$text定义了要输入的数字 imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); } //该函数给图像加上小点做干扰,默认是50个点,封装后放入verifyImage()函数中做参数 //$pixel = 50; if ($pixel) { for ($i = 0; $i < $pixel; $i++) { imagesetpixel($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), $black); } } //该函数给图像加上直线,默认是10根,封装后放入verifyImage()函数中做参数 //$line = 10; if ($line) { for ($i = 1; $i < $line; $i++) { $color = imagecolorallocate($image, mt_rand(50, 90), mt_rand(80, 200), mt_rand(90, 180)); imageline($image, mt_rand(0, $width - 1), mt_rand(0, $height - 1), mt_rand(0, $width - 1), mt_rand(0, $height - 1), $color); } } header('Content-Type: image/gif'); //imagegif把图像以gif文件格式输出到浏览器或文件 imagegif($image); //inagedestroy销毁图像,释放与image关联的内存 imagedestroy($image); }
<?php /** * Created by PhpStorm. * User: Administrator * Date: 16-1-20 * Time: 上午1:08 */ require_once '../include.php'; $chars = buildRandomString(); $_SESSION['checkCode'] = $chars; echo $chars;
<?php header('content-type:text/html;charset=utf-8'); function buildRandomString() { $str = '1234567890ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwyx'; return substr(str_shuffle($str), 0, 6); } for ($i = 0; $i < 129; $i++) { $username = buildRandomString(); $password = md5($username); $age = mt_rand(0, 100); $sex = mt_rand(0, 1); $sql = "INSERT users(username,password,age,sex) VALUES('{$username}','{$password}','{$age}','{$sex}');\n\n"; echo $sql; }