php图片验证码只显示一个小框,验证码的一个类,如何就是不出来图片呢

本文介绍了一个自定义的PHP验证码类,用于生成包含字母和数字的字符串或加减运算验证码,并带有噪音线。代码中包含了创建图像、设置噪音线、生成随机字符串等关键步骤,但在实际运行时遇到图片无法显示的问题。分析可能存在的问题包括图像输出头设置、内存管理或GD库函数使用不当等。解决此类问题通常需要检查HTTP响应头、确保GD库已启用并检查代码中是否有错误导致图像绘制失败。
摘要由CSDN通过智能技术生成

验证码的一个类,怎么就是不出来图片呢

自己闲着没事,自定义了一个验证码生成的类,但是不知道为什么,图片就是现实不出来。还请高手指点一下。

class imageCode{

public $img_width;    //验证码宽度

public $img_height;//高度

public $arr_char = array();  //验证码上显示的字符

public $font_size;         //字体大小

public $img;//验证码图片

public $code_result;//要保存在session中的数或者字符串

public $line_count = 7;//噪音线数量

private $str_chars = "0123456789abcdefghjkmnpqrstuvwxyzABCEDFGHJKLMNPQRSTUVWXYZ";//验证码字符串

//构造函数

function __construct($img_width,$img_height,$font_size,$line_count){

$this->img_width = $img_width;

$this->img_height = $img_height;

$this->font_size = $font_size;

$this->line_count = $line_count;

}

//验证码生成主方法

function createCodeImg(){

$this->createGraphics();

$this->getChars();

$this->setNoiceLine();

$x = rand(2,5);

$arr_X_Y = array(array($x,rand(1,3)),array($x+$this->font_size,rand(1,4)),array($x+2*$this->font_size,rand(1,3)),array($x+3*$this->font_size,rand(1,4)));

print_r($arr_X_Y);

for($i = 0;$i

$text_color = imagecolorallocate($this->img, rand(180,250), rand(180,250), rand(180,250));

imagechar($this->img,$this->font_size,$arr_X_Y[$i][0],$arr_X_Y[$i][1],$this->arr_char[$i],$text_color);

}

}

//创建画图板

function createGraphics(){

header("Content-type: image/png");

$this->img = @imagecreatetruecolor($this->img_width, $this->img_height) or die("建立图像失败"); //创建图片

$background_color = imagecolorallocate($this->img, 250, 250, 250);

imagefill($this->img,0,0,$background_color);

$border_color = imagecolorallocate($this->img,0,0,0); //边框色

imagerectangle($this->img,0,0,$this->img_width,$this->img_height,$border_color);

}

//画噪音线

function setNoiceLine(){

for($i = 0;$i line_count;$i ++){

$x1 = rand(3,20);//开始位置

$y1 = rand(2,$this->img_height);

$x2 = rand($this->img_width-20,$this->img_width-2);//结束位置

$y2 = rand(2,$this->img_height);

$line_color = imagecolorallocate($this->img, rand(180,250), rand(180,250), rand(180,250));

imageline($this->img,$x1,$y1,$x2,$y2,$line_color);

}

}

//产生随机字符串或者加减字符

function getChars(){

$strCode = "";

if(rand(0,1) == 1){//字符串类型的 验证码

for($i = 0;$i 

$this->arr_char[$i] = $this->str_chars[rand(0,56)];

$strCode .= $this->arr_char[$i];

}

$this->code_result = $strCode;

}

else{//加减类型验证码

$first_num = rand(1,10);

$second_num = 0;

$result = 0;

$arr_operater = array("+","-");

$operater = $arr_operater[rand(0,1)];

switch ($operater){

case "+":

$second_num = rand(0,10);

$result = $first_num + $second_num;

break;

case "-":

$second_num = rand(0,$first_num);

$result = $first_num - $second_num;

break;

}

$this->arr_char = array($first_num,$operater,$second_num,"=");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值