PHP - 代码适用于localhost,但不适用于实际站点

时间:2013-11-01 20:49:35

标签: php localhost web-hosting

好的,这是问题所在。此代码适用于localhost,但不适用于实时网站

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

$text = $_SESSION['secure'];
$font_size = 30;
$image_width  = 120;
$image_height = 40;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
$line_color = imagecolorallocate($image, 0, 0, 0);

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, $line_color);
}

imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font2.ttf', $text);
imagejpeg($image);
?>

localhost和webhosting都运行相同的php版本

请在发布任何内容之前先看一下这张图片!

http://s2.postimg.org/etamdsk95/help_me.png

2 个答案:

答案 0 :(得分:0)

尝试查看error.log(APACHE),它也可以 可能会出现会话问题 尝试使用“error_reporting(0);

隐藏所有错误

试试这个:

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

$text = $_SESSION['secure'];
$font_size = 30;

$image_width = 120;
$image_height = 40;

$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
$line_color = imagecolorallocate($image, 0, 0, 0);

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, $line_color);
}

imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font2.ttf', $text);
imagejpeg($image);

?>

但最好的方法是修复错误,所以请看@ error.log

http://php.net/manual/en/function.error-reporting.php

如果您收到任何错误消息,可以告诉我们 - 我们会尽力帮助您:)

答案 1 :(得分:0)

您的代码有一些错误,但这不是PHPGD lib错误,请注意以下代码:

<?php
//session_start();
header('Content-Type: image/jpeg');
//$_SESSION['secure']; disabled, this will lead to notice: undefined index
$text = "he123"; 
$font_size = 30;
$image_width = 120;
$image_height = 40;

$image = imagecreate($image_width, $image_height) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($image, 255, 255, 255); //add background color
$text_color = imagecolorallocate($image, 255, 0, 0); //red for the text
$line_color = imagecolorallocate($image, 0,255,0); // green for the line

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, $line_color);
}

// add ./ to the font name
imagettftext($image, $font_size, 0, 15, 30, $text_color, "./font2.ttf", $text);
imagejpeg($image);
imagedestroy($image); // remember to clear the chace :p
?>

这是output