我正在通过我编写的createCaptcha函数创建一个Captcha会话。在Web服务中,我调用这个函数来创建一个Captcha会话和图像,然后我想发送Captcha图像或显示Web服务创建的Captcha图片,用户可以通过Web服务发送它的值,但我面临错误。实际上,当我通过firefox插件soap_client测试它时,我得到的不是指向该图像的链接,而是编码的结果。
知道我哪里错了吗?
captcha功能代码:
<?php
session_start();
function createcaptcha($imgname)
{
/* Attempt to open */
$im = @imagecreatefromjpeg($imgname);
/* See if it failed */
if(!$im)
{
/* Create a black image */
$code=rand(1000,9999);
$_SESSION["code"]=$code;
$im = imagecreatetruecolor(50, 24);
$bg = imagecolorallocate($im, 22, 86, 165);
$fg = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $bg);
/* Output an captcha code */
imagestring($im, 5, 5, 5, $code, $fg);
}
return $im;
}
GetCaptcha肥皂类:
session_start();
include_once('captcha.php');
class getCaptcha extends DBM
{
public function getcaptcha()
{
//creating and generating captcha image and code
$img = createcaptcha('captcha.png');
//encoding to base64
//$base64 = base64_encode($img);
$path = $img;
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
$save = "/captchafile/captcha.png";
imagepng($base64, $save);
$captcha=$save;
$this->strResult = "";
$this->strResult.="<captcha>";
$this->strResult.="<captcha>$captcha</captcha>";
$this->strResult.="</captcha>";
}
function __toString()
{
if($this->strResult!=""){
return $this->strResult;
}
return "";
}
}
您需要给imagepng()
函数另一个参数来保存图片,然后使用Get_file_content()
函数获取图像文件的内容,然后将其编码到base64,以xml
格式通过webservice
发送。
include_once('captcha.php');
class getCaptcha extends DBM
{
public function getcaptcha($dump)
{
//creating and generating captcha image and code
$img = createcaptcha("Captcha.png");
imagepng($img,"Captcha.png");
imagedestroy($img);
$captchafile = file_get_contents('./Captcha.png');
//encoding to base64
$base64 = base64_encode($captchafile);
$captcha=$base64;
$this->strResult = "";
$this->strResult.="<captcha>";
$this->strResult.="<captcha>$captcha</captcha>";
$this->strResult.="</captcha>";
}
function __toString()
{
if($this->strResult!=""){
return $this->strResult;
}
return "";
}
}