这是用类封装的一个验证图 我的问题是如何在另外一个页面上调用它<?php
class ImageCode{
private $randcode; //随机字符
private $randcodelength; //随机字符长度
private $imgWidth; //随机字符宽度
private $imgHeight; //随机字符高度
private $line = 5; //干扰线
private $code = array("q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m",
"Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M",
"1","2","3","4","5","6","7","8","9","0");
private $codelength; //数组的长度 private $image; //图片
private $background; //背景
private $foreground; //前景 function __construct(){
$this->randcodelength = 5;
$this->imgWidth = 80;
$this->imgHeight = 20;
$this->codelength = count($this->code)-1; //获取数组长度当随机范围 $this->createrandcode();
$this->createimg();
$this->createString();
$this->drawline($this->line);
$this->ending(); }
function createrandcode(){
for($i=0;$i<$this->randcodelength;$i++){ //初始化验证码
$this->randcode .= $this->code[rand(0,$this->codelength)];
}
}
function createimg(){ //创建图片 前景 背景颜色
$this->image = imagecreatetruecolor($this->imgWidth,$this->imgHeight);
$this->background = imagecolorallocate($this->image,0,0,0);
$this->foreground = imagecolorallocate($this->image,255,255,255);
}
function createString(){ //创建字符串
imagestring($this->image,5,rand(5,35),0,$this->randcode,$this->foreground);
}
function drawline($some){
for($i=0;$i<$some;$i++){
imageline($this->image,rand(1,$this->imgWidth),rand(1,$this->imgHeight),rand(1,$this->imgWidth),rand(1,$this->imgHeight),$this->foreground);
}
}
function ending(){
ob_clean(); //抛弃缓存内容
header("Content-type: image/jpeg");
imagejpeg($this->image);
}
}
?>
下面是输出页面<?php
include("ImageCode.php");
$imgcode = new ImageCode();
?><!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=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
function RefreshCode(){
  alert("HelloWorld");
}
</script>
</head><body>
<img src="ImageCode.php" onclick="RefreshCode()"/>
</body>
</html>
这里我不知道怎么写 因为貌似是靠new ImageCode();来输出的验证图 而且不管怎么写 点击那个图片也没关联到show方法alert()也不出来请高人指教

解决方案 »

  1.   

    我用了个方法 再创建以个 页面
    ImageCodeServer.php
    <?php
    include("ImageCode.php");
    $image = new ImageCode();
    ?>
    然后输出页面就 test.php
    <img src="ImageCodeServer.php" onclick="RefreshCode(this)"/>
    这么写就没问题了
      

  2.   

    <img src="ImageCodeServer.php?mr=112" onclick="RefreshCode(this)"/>
    在路劲ImageCodeServer.php?后价格随机数,当相应点击事件时在改变这个src连接地址的随机数就可以实现点击切换验证码了!
    在ImageCodeServer.php
    这个文件中就应经画好了验证码了