如何打印captcha图像的功能


How to print a captcha image by function?

我创建了一个名为createCaptcha的函数

function createCaptcha()
{
    $ranStr = md5(microtime());
    $ranStr = substr($ranStr, 0, 6);
    $_SESSION['cap_code'] = $ranStr;
    $newImage = imagecreatefromjpeg("views/cap_bg.jpg");
    $txtColor = imagecolorallocate($newImage, 0, 0, 0);
    imagestring($newImage, 5, 5, 5, $ranStr, $txtColor);
    imagejpeg($newImage);
}

但是当我调用这个函数时,它显示了一些奇怪的字符!!!!

<label for="captchacheck">Word Verification : </label>
Type the characters in the picture below<br /><br/>
<div id="captchadiv">
<img id="captchaimg" src="<?php createCaptcha(); ?>" /></div><br /><br />
<input type="text" name="captchacheck" id="captchacheck" style="width:200px;"/>
<br /><br />

在函数内部添加内容类型后,

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

整个页面变成图像

但我只想在img标签

中起作用

任何帮助都将感激....

考虑一下,img标记的src属性指向图像文件,而不是图像数据。因此,将createCaptcha()函数放在某个文件中,如capcha .php,并将src设置为该文件。<img id="captchaimg" src="captcha.php" />