【发布时间】: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,现在可以使用了。字体已上传,但名称不正确。 =( 傻我。抱歉给你添麻烦了!