生成验证码图像但不生成数字

时间:2018-06-18 21:24:03

标签: php html apache xampp captcha

这是generate.php代码:

<?php
ob_start();
session_start();
header('content-type:image/jpeg');
$text=$_SESSION['secure'];
$font_size=30;
$width=100;
$height=40;
$image=imagecreate($width, $height);
imagecolorallocate($image, 0, 0, 0);
$text_color=imagecolorallocate($image, 255, 255, 255);
$font='Consolas.ttf';
for($x=1; $x <= 30; $x++) {
    $x1 = rand(1, 100);
    $y1 = rand(1, 100);
    $x2 = rand(1, 100);
    $y2 = rand(1, 100);
    imageline($image, $x1, $y1, $x2, $y2, $text);
}
imagettftext($image, $font_size, 0, 15, 30, $text_color, $font, $text);
$imgSrc="out.png";
imagepng($image, $imgSrc);
?>

这是用于显示图像的HTML代码,此处out.png已成功生成并加载到页面中,但无法找到数字。

<div class="form-group mx-auto text-center">
    <input type="hidden" name="cap_hidden" value="<?php echo $_SESSION['secure']; ?>">
    <img src="out.png">
</div>

我找到了一个类似的问题,但它没有帮助我,这就是我在这里发布的原因。 问题是图像仅使用线条生成,但没有数字可见。

2 个答案:

答案 0 :(得分:0)

我想图像上有文字,但它是白色的 也许你可以改变文本的颜色 http://php.net/manual/de/function.imagecolorallocate.php

  

这些参数是0到255或十六进制之间的整数   介于0x00和0xFF之间。

0为黑色,255为白色。

您可能希望查看实例 http://php.net/manual/en/function.imagettftext.php

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

您也可以在以下位置查看您的脚本:

$image=imagecreate($width, $height);
imagecolorallocate($image, 0, 0, 0);
$text_color=imagecolorallocate($image, 255, 255, 255);

imagecolorallocate($image, 0, 0, 0);的结果未在任何地方分配。

答案 1 :(得分:0)

看起来字体没有正确加载。

  

根据PHP使用的GD库版本,当fontfile不以前导/开头时,然后.ttf将附加到文件名,库将尝试沿库定义的字体路径搜索该文件名。 PHP: imagettftext - Manual

相关问题