【php开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了php开源代码-getcode.php的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
<?php
/*
* Created on 2006-11-19
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse -
PHP - Code Templates
*/
//生成验证码图片
session_start(); //启动session会话
Header("Content-type: image/GIF"); //设置页面的输出格式
srand((double)microtime()*1000000); //给随机数发生器播种
$im = imagecreate(60,30); //创建画布
$gray = ImageColorAllocate($im,128,128,128); //分配验证码图片的背景颜色
$white = ImageColorAllocate($im,255,255,255); //分配干扰点的颜色
$blue = imagecolorallocate($im,0,0,255); //分配输出文字的颜色
imagefill($im,60,30,$gray); //填充背景颜色
$font = 'arial.ttf'; //选择输出字符串的字体
$_SESSION['checkcode'] = rand(1000,9999); //随机生成四位整数验证码
//输出四位整数文本
//$utf8 = mb_convert_encoding((string)$_SESSION['checkcode'],"UTF-8", "BIG2312");
ImageTTFText($im,16,0,10,20, $blue,$font,(string)$_SESSION['checkcode']);
for($i=0;$i<100;$i++) //加入干扰象素
{
imagesetpixel($im, rand()%70 , rand()%30 , $white); //画干扰点
}
ImageGIF($im); //以png格式输出
ImageDestroy($im); //释放图片资源
?>