/** * 普通验证码实现 * */ class Custom_Controller_Plugin_Img extends Zend_Controller_Plugin_Abstract { function getImg($session) { header("Content-type: image/png"); srand((double)microtime()*1000000);//设置随机数的种子 $im=imagecreate(45,18);//创建一个画布 //$black=imagecolorallocate($im,0,0,0);//定义画布颜色为黑色 //$white=imagecolorallocate($im,255,255,255);//定义画布颜色为白色 $gray=imagecolorallocate($im,200,200,200);//定义画布颜色为灰色 imagefill($im,0,0,$gray);//用灰色填充画布 $s = new Zend_Session_Namespace($session);//注册SESSION变量 //$_SESSION["autonum"]="";//初始化SESSION变量 $authnum = ''; for($i=0;$i<4;$i++){ $str=mt_rand(1,3);//设置随机的字体 $size=mt_rand(3,6);//设置随机的字符大小 $auto=mt_rand(0,9);//设置字符为随机的数字0-9 $authnum .= $auto; imagestring($im,$size,(5+$i*10),$str,$auto,imagecolorallocate($im,rand(0,130),rand(0,130),rand(0,130))); } $s->word = $authnum; 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);//生成PNG格式的图像 imagedestroy($im);//释放图像资源 } }