【问题标题】:captcha image not generating验证码图像未生成
【发布时间】:2018-04-23 19:19:26
【问题描述】:

我有一个 .php 文件来为联系表单创建验证码。当我在本地 xampp 测试服务器上对其进行测试时,验证码生成良好。然而,现在我已经将网站上传到我的远程服务器,验证码只生成背景,但不生成验证码。我只是想不通为什么。 这是下面的php

<?php

session_start();

header("Expires: Tue, 01 Jan 2013 00:00:00 GMT"); 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';

for ($i = 0; $i < 5; $i++) 
{
    $randomString .= $chars[rand(0, strlen($chars)-1)];
}

$_SESSION['captcha'] = strtolower( $randomString );


$im = @imagecreatefrompng("captcha-background.png"); 


imagettftext($im, 20, 5, 10, 30, imagecolorallocate ($im, 0, 0, 0), 'larabiefont.ttf', $randomString);

header ('Content-type: image/png');
imagepng($im, NULL, 0);
imagedestroy($im);

?>  

【问题讨论】:

  • 很可能是因为您的服务器上没有所需的字体 (larabiefont.ttf)。
  • 我检查了我的服务器文件并且 (larabifont.ttf) 在那里
  • 它和这个脚本在同一个文件夹中吗?
  • 我查过了
  • 是的,它有效,这一切都很重要。感谢您的帮助

标签: php captcha gdlib


【解决方案1】:
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';

for ($i = 0; $i < 5; $i++) 
{
    $randomString .= $chars[rand(0, strlen($chars)-1)];
}

$_SESSION['captcha'] = strtolower( $randomString );
$im = imagecreatetruecolor(80, 24);
$bg = imagecolorallocate($im, 22, 86, 165);
$fg = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $bg);
imagestring($im, 5, 5, 5,  $randomString, $fg);
header("Cache-Control: no-cache, must-revalidate");
header('Content-type: image/png');
$captchaUrl = 'captchaImage.png';
imagepng($im,$captchaUrl);
imagedestroy($im);

使用此代码创建验证码图像

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多