• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP类的运用解决方案

php 搞代码 3年前 (2022-01-24) 18次浏览 已收录 0个评论

PHP类的运用
我是新手刚接触类的概念,现在我在网上找到了一个生成验证码的类,要怎么才能在需要输出验证码的地方输出类中生成的验证码呢?类中的代码是这样的:
<?php
class AuthCode
{
var $image;
var $sBgcolor;
var $nWidth;
var $nHeight;
var $nLen;
var $bNoise;
var $nNoise;
var $bBorder;
var $aFontlist;
function AuthCode()
{
$this->sBgcolor = “#FFFFFF”;
$this->nWidth = 70;
$this->nHeight = 25;
$this->nLeftMargin = 5;
$this->nRightMargin = 5;
$this->nTopMargin = 3;
$this->nBottomMargin = 2;
$this->nLen = 4;
$this->bNoise = true;
$this->nNoisePoint = 50;
$this->nNoiseLine = 5;
$this->bBorder = true;

$this->aFontlist = “arial.ttf”;
}

function OutputImg()
{
$this->image = “”;
$this->image = imagecreate($this->nWidth, $this->nHeight);
$back = $this->getcolor($this->sBgcolor);
imagefilledrectangle($this->image, 0, 0, $this->nWidth, $this->nHeight, $back);
$size = ($this->nWidth – $this->nLeftMargin – $this->nRightMargin)/$this->nLen;
if($size>($this->nHeight – $this->nTopMargin – $this->nBottomMargin))
$size=$this->nHeight – $this->nTopMargin

!本文来源gaodai.ma#com搞##代!^码网(

搞gaodaima代码 – $this->nBottomMargin;

$left = ($this->nWidth-$this->nLen*($size+$size/10))/2 + $this->nLeftMargin;
$code = “”;
for ($i=0; $inLen; $i++)
{
$randtext = rand(0, 9);
$code .= $randtext;
$textColor = imagecolorallocate($this->image, rand(0, 100), rand(0, 100), rand(0, 100));
$font = $this->aFontlist;
$randsize = rand($size-$size/10, $size+$size/10);
$location = $left+($i*$size+$size/10);
imagettftext($this->image, $randsize, rand(-18,18), $location, rand($size, $size+$size/5) + $this->nTopMargin, $textColor, $font, $randtext);
}
if($this->bNoise == true) $this->setnoise();
$_SESSION[‘yzm’] = md5($code);
$bordercolor = $this->getcolor(“#FFFFFF”);
if($this->bBorder==true) imagerectangle($this->image, 0, 0, $this->nWidth-1, $this->nHeight-1, $bordercolor);
header(“Content-type: image/png”);
imagepng($this->image);
imagedestroy($this->image);
}
function setnoise()//设置噪点
{
for ($i=0; $inNoiseLine; $i++){
$randColor = imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
imageline($this->image, rand(0, $this->nWidth), rand(0, $this->nHeight), rand(0, $this->nWidth), rand(0, $this->nHeight), $randColor);
}

for ($i=0; $inNoisePoint; $i++){
$randColor = imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($this->image, rand(0, $this->nWidth), rand(0, $this->nHeight), $randColor);


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP类的运用解决方案
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址