Captcha图像”;破碎的“;在webhost上,但不在localhost上


Captcha image "broken" on webhost but not on localhost

我有一个captcha脚本,它应该生成一个代码。它在我的本地主机上运行得很好,但当我把它上传到我的网络主机时,它看起来就像一个坏图像。请注意,GD已经安装并运行(我有一个调整图像大小的脚本,在网络主机上运行良好)。

以下是我的captcha.php代码(captcha会话值是在这个脚本之外生成的):

<?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);
?>

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

简单修复。您可能正在linux上运行lamp。这种情况发生在我身上是因为我的灯没有gd模块。Gd(图形化的东西)是在飞行中渲染captcha图片的模块。要修复它,请转到灯服务器上的终端并键入:

sudo apt-get install php5-gd

你应该很好去