php GD2 --1

来源:互联网 发布:php个人简历源代码 编辑:程序博客网 时间:2024/04/30 19:10
<?php    header("content-type:text/html;charset=utf-8");  // gd库支持utf-8编码,如输出中文,必须使用此标头    header("content-type:image/jpeg");               //输出jpeg图像$img = imagecreate(200,200);                      //创建图像画布imagecolorallocate($img,0,0,0);                   //设置背景色。其实第一次调用imagecolorallocate就是设置背景色$white = imagecolorallocate($img,255,255,255);    //设置白色前景色,以后每一次调用,都是前景色,在具体画图,输出文章的时候,作为参数调用$red = imagecolorallocate($img,255,0,0);          //设置红色imagestring($img,5,10,10,'hello',$white);       //输出文字,使用内置字体,最大5,最小1,颜色为白色imagestring($img,5,100,100,'中文字',$red);        //输出中文,颜色为红色,可以看到,使用imagestring输出是乱码$font = 'simfang.ttf';                           //使用ttf字体,仿宋体imagettftext($img,20,30,20,150,$white,$font,"中文字体");  //输出中文,要使用imagettftext函数,并且要指定字体文件imageline($img,0,0,100,50,$white);               //画一直线imagejpeg($img);                                 //输出图像imagedestroy($img);                               //销毁保存在内存中的图像?>



------index.php

<?php       session_start();                                   //用于在$_SESSION中获取验证码的值  ?>  <script type="text/javascript">      window.onload=function() {          var xmlHttp = false;          if (window.XMLHttpRequest) {                    //Mozilla、Safari等浏览器              xmlHttp = new XMLHttpRequest();          }           else if (window.ActiveXObject) {                //IE浏览器              try {                  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");              } catch (e) {                  try {                      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");                 } catch (e) {}              }          }          var checkImg=document.getElementById("checkImg");          var checkCode=document.getElementById("checkCode");          checkImg.onclick=function() {              checkImg.src="ValidationCode.class.php?num="+Math.random();  //以下用于单击验证码时重新加载验证码图片              //需要加num=Math.random()以防止图片在本地缓存,单击图片时不刷新显示          }          checkCode.onblur=function(){              //alert("Hello");              xmlHttp.open("POST","checkCode.php",true);              xmlHttp.onreadystatechange=function () {                  if(xmlHttp.readyState==4 && xmlHttp.status==200) {                      var msg=xmlHttp.responseText;    //设置标志用于表示验证码是否正确                      if(msg=='1')                          document.getElementById("checkResult").innerHTML="正确";  //在这可以设置其中间显示一张图片                      else                          document.getElementById("checkResult").innerHTML="错误";                  }              }              xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");              xmlHttp.send("validateCode="+checkCode.value);          }         }  </script>  <input type="text" id="checkCode">  <img src="checkcode.php" id="checkImg"><span id="checkResult">请在此输入验证码</span>



--checkode.php

<?phpsession_start();header("Content-Type:text/html;charset=utf-8");//设置编码风格header("Content-Type:image/jpeg");//设置图片格式$image=imagecreate(70,40);//创建画布$bgcolor=imagecolorallocate($image,250,180,180);//设置背景颜色$fontcolor=imagecolorallocate($image,30,30,30);//设置字体颜色$font="simfang.TTF";//设置字体$rand = '';for($a=0;$a<4;$a++){//循环语句$rand.=dechex(rand(0,15));}$_SESSION['checkCode']=$rand;$string=$rand;imagefttext($image,20,7,10,30,$fontcolor,$font,$string);//输出验证码imagejpeg($image);imagedestroy($image);?>

--userCheckCode

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>使用验证码</title></head><body><form method="post" action="checode.php">请输入验证码<input type="text" name="aa" /><img src="checkcode.php" onclick="this.src='checkcode.php?id='+Math.random()"/>    <input type="submit" value="确定" name="sub"/></form></body></html>



0 0
原创粉丝点击