haidong 发表于 2010-2-24 13:43:15

一个生成图形验证码的class

<OL><LI><CODE><FONT face=新宋体>&lt;?</FONT></CODE><LI><CODE><FONT face=新宋体>class ValidateImage</FONT></CODE><LI><CODE><FONT face=新宋体>{</FONT></CODE><LI><CODE><FONT face=新宋体>    var $Text;</FONT></CODE><LI><CODE><FONT face=新宋体>    var $Font;</FONT></CODE><LI><CODE><FONT face=新宋体>    var $X;</FONT></CODE><LI><CODE><FONT face=新宋体>    var $Y;</FONT></CODE><LI><CODE><FONT face=新宋体>    var $Width;</FONT></CODE><LI><CODE><FONT face=新宋体>    var $Height;</FONT></CODE><LI><CODE><FONT face=新宋体>    var $BGColor;</FONT></CODE><LI><CODE><FONT face=新宋体>    var $TextColor;</FONT></CODE><LI><CODE><FONT face=新宋体>    var $BorderColor;</FONT></CODE><LI><CODE><FONT face=新宋体>    var $NoiseColor;</FONT></CODE><LI><CODE><FONT face=新宋体>    var $NoiseRate;</FONT></CODE><LI><CODE><FONT face=新宋体>    var $TextSpace;</FONT></CODE><LI><CODE><FONT face=新宋体>    var $image;</FONT></CODE><LI><LI><CODE><FONT face=新宋体>    function ValidateImage()</FONT></CODE><LI><CODE><FONT face=新宋体>    {</FONT></CODE><LI><CODE><FONT face=新宋体>      $this-&gt;Text = NULL;</FONT></CODE><LI><CODE><FONT face=新宋体>      $this-&gt;Font = 5;</FONT></CODE><LI><CODE><FONT face=新宋体>      $this-&gt;X = NULL;</FONT></CODE><LI><CODE><FONT face=新宋体>      $this-&gt;Y = NULL;</FONT></CODE><LI><CODE><FONT face=新宋体>      $this-&gt;Width = NULL;</FONT></CODE><LI><CODE><FONT face=新宋体>      $this-&gt;Height = NULL;</FONT></CODE><LI><CODE><FONT face=新宋体>      $this-&gt;BGColor = array(255, 255, 255);</FONT></CODE><LI><CODE><FONT face=新宋体>      $this-&gt;TextColor = array(0, 0, 0);</FONT></CODE><LI><CODE><FONT face=新宋体>      $this-&gt;BorderColor = array(110, 110, 110);</FONT></CODE><LI><CODE><FONT face=新宋体>      $this-&gt;TextSpace = 3;</FONT></CODE><LI><CODE><FONT face=新宋体>      $this-&gt;NoiseColor = array();</FONT></CODE><LI><CODE><FONT face=新宋体>      $this-&gt;NoiseRate = 0.3;</FONT></CODE><LI><CODE><FONT face=新宋体>      $this-&gt;image = NULL;</FONT></CODE><LI><CODE><FONT face=新宋体>    }</FONT></CODE><LI><LI><CODE><FONT face=新宋体>    //创建图像</FONT></CODE><LI><CODE><FONT face=新宋体>    function Create()</FONT></CODE><LI><CODE><FONT face=新宋体>    {</FONT></CODE><LI><CODE><FONT face=新宋体>      $len = <A href="http://www.php.net/manual/zh/function.strlen"><U><FONT color=#0000ff>strlen</FONT></U></A>($this-&gt;Text);</FONT></CODE><LI><CODE><FONT face=新宋体>      </FONT></CODE><LI><CODE><FONT face=新宋体>      //载入字体</FONT></CODE><LI><CODE><FONT face=新宋体>      if(!<A href="http://www.php.net/manual/zh/function.is_int"><U><FONT color=#0000ff>is_int</FONT></U></A>($this-&gt;Font))</FONT></CODE><LI><CODE><FONT face=新宋体>      {</FONT></CODE><LI><CODE><FONT face=新宋体>            $this-&gt;Font = <A href="http://www.php.net/manual/zh/function.imageloadfont"><U><FONT color=#0000ff>imageloadfont</FONT></U></A>($this-&gt;Font);</FONT></CODE><LI><CODE><FONT face=新宋体>      }</FONT></CODE><LI><CODE><FONT face=新宋体>      </FONT></CODE><LI><CODE><FONT face=新宋体>      //设置文字位置</FONT></CODE><LI><CODE><FONT face=新宋体>      if (<A href="http://www.php.net/manual/zh/function.is_null"><U><FONT color=#0000ff>is_null</FONT></U></A>($this-&gt;X))</FONT></CODE><LI><CODE><FONT face=新宋体>      {</FONT></CODE><LI><CODE><FONT face=新宋体>            $this-&gt;X = $this-&gt;TextSpace;</FONT></CODE><LI><CODE><FONT face=新宋体>      }</FONT></CODE><LI><CODE><FONT face=新宋体>      if (<A href="http://www.php.net/manual/zh/function.is_null"><U><FONT color=#0000ff>is_null</FONT></U></A>($this-&gt;Y))</FONT></CODE><LI><CODE><FONT face=新宋体>      {</FONT></CODE><LI><CODE><FONT face=新宋体>            $this-&gt;Y = $this-&gt;TextSpace;</FONT></CODE><LI><CODE><FONT face=新宋体>      }</FONT></CODE><LI><LI><CODE><FONT face=新宋体>      //设置宽度</FONT></CODE><LI><CODE><FONT face=新宋体>      if (<A href="http://www.php.net/manual/zh/function.is_null"><U><FONT color=#0000ff>is_null</FONT></U></A>($this-&gt;Width))</FONT></CODE><LI><CODE><FONT face=新宋体>      {</FONT></CODE><LI><CODE><FONT face=新宋体>            if ($len == 0)</FONT></CODE><LI><CODE><FONT face=新宋体>            {</FONT></CODE><LI><CODE><FONT face=新宋体>                $this-&gt;Width = $this-&gt;X * 2;</FONT></CODE><LI><CODE><FONT face=新宋体>            }</FONT></CODE><LI><CODE><FONT face=新宋体>            else</FONT></CODE><LI><CODE><FONT face=新宋体>            {</FONT></CODE><LI><CODE><FONT face=新宋体>                $this-&gt;Width = $this-&gt;TextSpace * ($len - 1) + <A href="http://www.php.net/manual/zh/function.imagefontwidth"><U><FONT color=#0000ff>imagefontwidth</FONT></U></A>($this-&gt;Font) * $len + $this-&gt;X * 2;</FONT></CODE><LI><CODE><FONT face=新宋体>            }</FONT></CODE><LI><CODE><FONT face=新宋体>      }</FONT></CODE><LI><CODE><FONT face=新宋体>      </FONT></CODE><LI><CODE><FONT face=新宋体>      //设置高度</FONT></CODE><LI><CODE><FONT face=新宋体>      if (<A href="http://www.php.net/manual/zh/function.is_null"><U><FONT color=#0000ff>is_null</FONT></U></A>($this-&gt;Height))</FONT></CODE><LI><CODE><FONT face=新宋体>      {</FONT></CODE><LI><CODE><FONT face=新宋体>            $this-&gt;Height = <A href="http://www.php.net/manual/zh/function.imagefontheight"><U><FONT color=#0000ff>imagefontheight</FONT></U></A>($this-&gt;Font) + $this-&gt;Y * 2;</FONT></CODE><LI><CODE><FONT face=新宋体>      }</FONT></CODE><LI><CODE><FONT face=新宋体>      </FONT></CODE><LI><CODE><FONT face=新宋体>      //噪声数量</FONT></CODE><LI><CODE><FONT face=新宋体>      $noiseNum = <A href="http://www.php.net/manual/zh/function.floor"><U><FONT color=#0000ff>floor</FONT></U></A>($this-&gt;Height * $this-&gt;Width * $this-&gt;NoiseRate);</FONT></CODE><LI><CODE><FONT face=新宋体>      </FONT></CODE><LI><CODE><FONT face=新宋体>      $this-&gt;image = <A href="http://www.php.net/manual/zh/function.imagecreatetruecolor"><U><FONT color=#0000ff>imagecreatetruecolor</FONT></U></A>($this-&gt;Width, $this-&gt;Height);</FONT></CODE><LI><CODE><FONT face=新宋体>      //$this-&gt;image = imagecreate($this-&gt;Width, $this-&gt;Height);   //如果服务器不支持真彩色</FONT></CODE><LI><CODE><FONT face=新宋体>      $colorBG = <A href="http://www.php.net/manual/zh/function.imagecolorallocate"><U><FONT color=#0000ff>imagecolorallocate</FONT></U></A> ($this-&gt;image, $this-&gt;BGColor, $this-&gt;BGColor, $this-&gt;BGColor);</FONT></CODE><LI><CODE><FONT face=新宋体>      $colorText = <A href="http://www.php.net/manual/zh/function.imagecolorallocate"><U><FONT color=#0000ff>imagecolorallocate</FONT></U></A>($this-&gt;image, $this-&gt;TextColor, $this-&gt;TextColor, $this-&gt;TextColor);</FONT></CODE><LI><CODE><FONT face=新宋体>      $colorBorder = <A href="http://www.php.net/manual/zh/function.imagecolorallocate"><U><FONT color=#0000ff>imagecolorallocate</FONT></U></A>($this-&gt;image, $this-&gt;BorderColor, $this-&gt;BorderColor, $this-&gt;BorderColor);</FONT></CODE><LI><CODE><FONT face=新宋体>      $colorNoise = <A href="http://www.php.net/manual/zh/function.count"><U><FONT color=#0000ff>count</FONT></U></A>($this-&gt;NoiseColor) == 3 ? <A href="http://www.php.net/manual/zh/function.imagecolorallocate"><U><FONT color=#0000ff>imagecolorallocate</FONT></U></A>($this-&gt;image, $this-&gt;NoiseColor, $this-&gt;NoiseColor, $this-&gt;NoiseColor) : NULL;</FONT></CODE><LI><CODE><FONT face=新宋体>      </FONT></CODE><LI><CODE><FONT face=新宋体>      //填充背景</FONT></CODE><LI><CODE><FONT face=新宋体>      <A href="http://www.php.net/manual/zh/function.imagefilledrectangle"><U><FONT color=#0000ff>imagefilledrectangle</FONT></U></A>($this-&gt;image, 0, 0, $this-&gt;Width - 1,$this-&gt;Height - 1, $colorBG);</FONT></CODE><LI><CODE><FONT face=新宋体>      </FONT></CODE><LI><CODE><FONT face=新宋体>      //绘制边框</FONT></CODE><LI><CODE><FONT face=新宋体>      <A href="http://www.php.net/manual/zh/function.imagerectangle"><U><FONT color=#0000ff>imagerectangle</FONT></U></A>($this-&gt;image, 0, 0, $this-&gt;Width - 1,$this-&gt;Height - 1, $colorBorder);</FONT></CODE><LI><CODE><FONT face=新宋体>      </FONT></CODE><LI><CODE><FONT face=新宋体>      $isAutoNoiseColor = <A href="http://www.php.net/manual/zh/function.count"><U><FONT color=#0000ff>count</FONT></U></A>($this-&gt;NoiseColor) != 3;</FONT></CODE><LI><CODE><FONT face=新宋体>      </FONT></CODE><LI><CODE><FONT face=新宋体>      //绘制噪音</FONT></CODE><LI><CODE><FONT face=新宋体>      for($i = 0; $i &lt; $noiseNum; $i++)</FONT></CODE><LI><CODE><FONT face=新宋体>      {</FONT></CODE><LI><CODE><FONT face=新宋体>            if ($isAutoNoiseColor)</FONT></CODE><LI><CODE><FONT face=新宋体>                $colorNoise = <A href="http://www.php.net/manual/zh/function.imagecolorallocate"><U><FONT color=#0000ff>imagecolorallocate</FONT></U></A>($this-&gt;image, <A href="http://www.php.net/manual/zh/function.mt_rand"><U><FONT color=#0000ff>mt_rand</FONT></U></A>(0, 255), <A href="http://www.php.net/manual/zh/function.mt_rand"><U><FONT color=#0000ff>mt_rand</FONT></U></A>(0, 255), <A href="http://www.php.net/manual/zh/function.mt_rand"><U><FONT color=#0000ff>mt_rand</FONT></U></A>(0, 255));</FONT></CODE><LI><CODE><FONT face=新宋体>            <A href="http://www.php.net/manual/zh/function.imagesetpixel"><U><FONT color=#0000ff>imagesetpixel</FONT></U></A>($this-&gt;image, <A href="http://www.php.net/manual/zh/function.mt_rand"><U><FONT color=#0000ff>mt_rand</FONT></U></A>(0, $this-&gt;Width), <A href="http://www.php.net/manual/zh/function.mt_rand"><U><FONT color=#0000ff>mt_rand</FONT></U></A>(0, $this-&gt;Height), $colorNoise);</FONT></CODE><LI><CODE><FONT face=新宋体>      }</FONT></CODE><LI><LI><CODE><FONT face=新宋体>      //绘制文字</FONT></CODE><LI><CODE><FONT face=新宋体>      for($i = 0; $i &lt; <A href="http://www.php.net/manual/zh/function.strlen"><U><FONT color=#0000ff>strlen</FONT></U></A>($this-&gt;Text); $i++)</FONT></CODE><LI><CODE><FONT face=新宋体>      {</FONT></CODE><LI><CODE><FONT face=新宋体>            $chr = $this-&gt;Text[$i];</FONT></CODE><LI><CODE><FONT face=新宋体>            $x = $this-&gt;X + ($this-&gt;TextSpace + <A href="http://www.php.net/manual/zh/function.imagefontwidth"><U><FONT color=#0000ff>imagefontwidth</FONT></U></A>($this-&gt;Font)) * $i;</FONT></CODE><LI><CODE><FONT face=新宋体>            <A href="http://www.php.net/manual/zh/function.imagestring"><U><FONT color=#0000ff>imagestring</FONT></U></A>($this-&gt;image, $this-&gt;Font, $x, $this-&gt;Y, $chr, $colorText);</FONT></CODE><LI><CODE><FONT face=新宋体>      }</FONT></CODE><LI><CODE><FONT face=新宋体>      return true;</FONT></CODE><LI><CODE><FONT face=新宋体>    }</FONT></CODE><LI><CODE><FONT face=新宋体>    </FONT></CODE><LI><CODE><FONT face=新宋体>    //输出图片</FONT></CODE><LI><CODE><FONT face=新宋体>    function Show()</FONT></CODE><LI><CODE><FONT face=新宋体>    {</FONT></CODE><LI><CODE><FONT face=新宋体>      <A href="http://www.php.net/manual/zh/function.ob_clean"><U><FONT color=#0000ff>ob_clean</FONT></U></A>();</FONT></CODE><LI><CODE><FONT face=新宋体>      <A href="http://www.php.net/manual/zh/function.header"><U><FONT color=#0000ff>header</FONT></U></A>("Content-type:image/jpeg");</FONT></CODE><LI><CODE><FONT face=新宋体>      <A href="http://www.php.net/manual/zh/function.imagejpeg"><U><FONT color=#0000ff>imagejpeg</FONT></U></A>($this-&gt;image);</FONT></CODE><LI><CODE><FONT face=新宋体>      <A href="http://www.php.net/manual/zh/function.imagedestroy"><U><FONT color=#0000ff>imagedestroy</FONT></U></A>($this-&gt;image);</FONT></CODE><LI><CODE><FONT face=新宋体>    }</FONT></CODE><LI><CODE><FONT face=新宋体>}</FONT></CODE><LI><LI><CODE><FONT face=新宋体>?&gt;</FONT></CODE></LI></OL><PRE> </PRE><OL><LI><CODE><FONT face=新宋体>&lt;?php</FONT></CODE><LI><CODE><FONT face=新宋体>$img = new ValidateImage();</FONT></CODE><LI><CODE><FONT face=新宋体>    $img-&gt;Font = "gdf.gdf";//字体文件路径或者整数1-5(系统内部字体)</FONT></CODE><LI><CODE><FONT face=新宋体>    $img-&gt;Text = "aslfjsalf";//验证码上的文字</FONT></CODE><LI><CODE><FONT face=新宋体>    $img-&gt;TextSpace = 2;   //字间距</FONT></CODE><LI><CODE><FONT face=新宋体>    $img-&gt;X = 5;             //首字符x坐标</FONT></CODE><LI><CODE><FONT face=新宋体>    $img-&gt;Y = 2;             //字符Y坐标</FONT></CODE><LI><CODE><FONT face=新宋体>    $img-&gt;Create();</FONT></CODE><LI><CODE><FONT face=新宋体>    $img-&gt;Show();</FONT></CODE><LI><CODE><FONT face=新宋体>?&gt;</FONT></CODE></LI></OL><PRE> </PRE>
页: [1]
查看完整版本: 一个生成图形验证码的class