【问题标题】:Captcha image "broken" on webhost but not on localhost验证码图像在虚拟主机上“损坏”,但在本地主机上没有
【发布时间】:2013-08-09 12:18:21
【问题描述】:

我有一个应该生成代码的验证码脚本。它在我的本地主机上运行良好,但是当我将它上传到我的虚拟主机时,它看起来就像一个损坏的图像。请注意,GD 已安装并可以正常工作(我有一个可以在网络主机上正常工作的图像大小调整脚本)。

这是我的 captcha.php 代码(验证码会话值是在此脚本之外生成的):

<?php header('Content-type: image/jpeg'); ?>
<?php session_start(); ?>
<?php

$text = $_SESSION['captcha'];

//Image specs
$fontsize = 28;
$imagewidth = 130;
$imageheight = 50;

$image = imagecreate($imagewidth, $imageheight);
imagecolorallocate($image, 75, 146, 178);
$textcolor = imagecolorallocate($image, 255, 255, 255);

//Set fonts
$font1 = 'fonts/font1.ttf';
$font2 = 'fonts/font2.ttf';
$font3 = 'fonts/font3.ttf';

//Create 15 random lines
for ($x=1; $x<=15; $x++) {
    $linecolor = imagecolorallocate($image, 206, 228, 244);
    $x1 = rand(1,110);
    $y1 = rand(1,50);
    $x2 = rand(1,110);
    $y2 = rand(1,50);
    imageline($image, $x1, $y1, $x2, $y2, $linecolor);
}

//randomize font
$randfont = rand(1,3);
if ($randfont == 1) { 
    $font = $font1; 
} elseif($randfont == 2) { 
    $font = $font2; 
} else {
    $font = $font3;
}

//Randomize text angle
$angle = rand(-3,3);

imagettftext($image, $fontsize, $angle, 10, 32, $textcolor, $font, $text);
imagejpeg($image);

?>

知道是什么导致了这种奇怪的行为吗?

【问题讨论】:

  • 你上传fonts文件夹了吗?
  • 您能否将用于测试和调试目的的内容标头更改为 text/html,这样您可能会收到错误消息。
  • 您是否启用了错误报告?您的日志中有任何错误吗?可能与正在发送的会话和标头存在冲突。
  • 感谢 safarov,现在可以使用了。字体已上传,但名称不正确。 =( 傻我。抱歉给你添麻烦了!

标签: php captcha


【解决方案1】:

轻松修复。你可能正在linux上运行灯泡。这发生在我身上是因为我的灯没有 gd 模块。 Gd(图形的东西)是即时渲染验证码图片的模块。要修复它,请转到您的 Lamp 服务器的终端并输入:

sudo apt-get install php5-gd

你应该很高兴

【讨论】:

    猜你喜欢
    • 2011-05-13
    • 2019-07-23
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 2018-06-26
    • 2013-03-16
    • 1970-01-01
    相关资源
    最近更新 更多