php图像存在错误显示不出来
<?phpclassvalidateCode{private$charset='abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ234...
<?php
class validateCode{
private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789'; //随机因子
private $code; //验证码
private $codelen=4; //验证码长度
private $width=130;
private $height=50;
private $img;
//生成验证码
private function createCode(){
$_len=strlen($this->charset);
for ($i=0;$i<$this->codelen;$i++){
$this->code.=$this->charset[mt_rand(0, $_len)];
}
return $this->code;
}
private function createBG(){
$this->img = imagecreatetruecolor($this->width, $this->height);
$color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
}
private function outPut(){
ob_clean();
Header('Content-type:image/png');
imagepng($this->img);
imagedestroy($this->img);
}
public function doimg(){
//$this->createCode();
$this->outPut();
}
}
test.php
require_once 'includes/validateCode.class.php';
$validate=new validateCode();
$validate->doimg();
//echo '<img src="'.$validate->doimg().'" />';这个没用
如果屏蔽掉require就找不到验证类 明明就设置了自动载入 其他类可以 为什么这个类无法载入 展开
class validateCode{
private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789'; //随机因子
private $code; //验证码
private $codelen=4; //验证码长度
private $width=130;
private $height=50;
private $img;
//生成验证码
private function createCode(){
$_len=strlen($this->charset);
for ($i=0;$i<$this->codelen;$i++){
$this->code.=$this->charset[mt_rand(0, $_len)];
}
return $this->code;
}
private function createBG(){
$this->img = imagecreatetruecolor($this->width, $this->height);
$color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
}
private function outPut(){
ob_clean();
Header('Content-type:image/png');
imagepng($this->img);
imagedestroy($this->img);
}
public function doimg(){
//$this->createCode();
$this->outPut();
}
}
test.php
require_once 'includes/validateCode.class.php';
$validate=new validateCode();
$validate->doimg();
//echo '<img src="'.$validate->doimg().'" />';这个没用
如果屏蔽掉require就找不到验证类 明明就设置了自动载入 其他类可以 为什么这个类无法载入 展开
2015-04-08 · 知道合伙人软件行家
关注
展开全部
追问
为什么图像无法显示出来 还没做验证码呢 只是输出背景的图像就无法显示出来
追答
请细查以下代码,看缺失什么:
private function outPut(){
ob_clean();
Header('Content-type:image/png');
imagepng($this->img);
imagedestroy($this->img);
}
public function doimg(){
//$this->createCode();
$this->outPut();
}
你看到有调用 createBG() 的吗?
展开全部
<?php
class validateCode{
private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789'; //随机因子
private $code; //验证码
private $codelen=4; //验证码长度
private $width=130;
private $height=50;
private $img;
//生成验证码
private function createCode(){
$_len=strlen($this->charset);
for ($i=0;$i<$this->codelen;$i++){
$this->code.=$this->charset[mt_rand(0, $_len-1)];
}
return $this->code;
}
private function createBG(){
//画布
$this->img = imagecreatetruecolor($this->width, $this->height);
$color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
imagefilledrectangle($this->img,0,0,$this->width,$this->height,$color);
$black = imagecolorallocate($this->img, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
$strx = rand(20, 8);
for ($i = 0; $i < $this->codelen; $i++) {
$strpos = rand(1, 6);
imagestring($this->img, 5, $strx, $strpos, substr($this->createCode(), $i, 1), $black);
$strx += rand(8, 14);
}
return $this->img;
}
private function outPut(){
ob_clean();
Header('Content-type:image/png');
imagepng($this->createBG());
imagedestroy($this->createBG());
}
public function doimg(){
$this->outPut();
}
}
$validate=new validateCode();
echo $validate->doimg();/
?>
class validateCode{
private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789'; //随机因子
private $code; //验证码
private $codelen=4; //验证码长度
private $width=130;
private $height=50;
private $img;
//生成验证码
private function createCode(){
$_len=strlen($this->charset);
for ($i=0;$i<$this->codelen;$i++){
$this->code.=$this->charset[mt_rand(0, $_len-1)];
}
return $this->code;
}
private function createBG(){
//画布
$this->img = imagecreatetruecolor($this->width, $this->height);
$color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
imagefilledrectangle($this->img,0,0,$this->width,$this->height,$color);
$black = imagecolorallocate($this->img, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
$strx = rand(20, 8);
for ($i = 0; $i < $this->codelen; $i++) {
$strpos = rand(1, 6);
imagestring($this->img, 5, $strx, $strpos, substr($this->createCode(), $i, 1), $black);
$strx += rand(8, 14);
}
return $this->img;
}
private function outPut(){
ob_clean();
Header('Content-type:image/png');
imagepng($this->createBG());
imagedestroy($this->createBG());
}
public function doimg(){
$this->outPut();
}
}
$validate=new validateCode();
echo $validate->doimg();/
?>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<?php
class validateCode{
private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789'; //随机因子
private $code; //验证码
private $codelen=4; //验证码长度
private $width=130;
private $height=50;
private $img;
//生成验证码
private function createCode(){
$_len=strlen($this->charset);
for ($i=0;$i<$this->codelen;$i++){
$this->code.=$this->charset[mt_rand(0, $_len-1)];
}
return $this->code;
}
private function createBG(){
//画布
$this->img = imagecreatetruecolor($this->width, $this->height);
$color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
imagefilledrectangle($this->img,0,0,$this->width,$this->height,$color);
$black = imagecolorallocate($this->img, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
$strx = rand(20, 8);
for ($i = 0; $i < $this->codelen; $i++) {
$strpos = rand(1, 6);
imagestring($this->img, 5, $strx, $strpos, substr($this->createCode(), $i, 1), $black);
$strx += rand(8, 14);
}
return $this->img;
}
private function outPut(){
ob_clean();
Header('Content-type:image/png');
imagepng($this->createBG());
imagedestroy($this->createBG());
}
public function doimg(){
$this->outPut();
}
}
$validate=new validateCode();
echo $validate->doimg();/
?>
class validateCode{
private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789'; //随机因子
private $code; //验证码
private $codelen=4; //验证码长度
private $width=130;
private $height=50;
private $img;
//生成验证码
private function createCode(){
$_len=strlen($this->charset);
for ($i=0;$i<$this->codelen;$i++){
$this->code.=$this->charset[mt_rand(0, $_len-1)];
}
return $this->code;
}
private function createBG(){
//画布
$this->img = imagecreatetruecolor($this->width, $this->height);
$color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
imagefilledrectangle($this->img,0,0,$this->width,$this->height,$color);
$black = imagecolorallocate($this->img, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
$strx = rand(20, 8);
for ($i = 0; $i < $this->codelen; $i++) {
$strpos = rand(1, 6);
imagestring($this->img, 5, $strx, $strpos, substr($this->createCode(), $i, 1), $black);
$strx += rand(8, 14);
}
return $this->img;
}
private function outPut(){
ob_clean();
Header('Content-type:image/png');
imagepng($this->createBG());
imagedestroy($this->createBG());
}
public function doimg(){
$this->outPut();
}
}
$validate=new validateCode();
echo $validate->doimg();/
?>
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询