【问题标题】:PHP imagettfext not displaying workingPHP imagettftext不显示工作
【发布时间】:2011-07-31 23:43:23
【问题描述】:

我曾经使用脚本通过更新表单来更改图像上的文本。

当我在预先安装了所有模块的共享主机上时,它一开始工作得很好。

我搬到了一个 vps,我必须自己安装所有东西,现在脚本不再工作了。

php页面可以显示,但是当我更新时,文本不像以前那样显示了。

以下是共享主机上仍然有效的页面示例: http://nyanpuffle.com/auntarctic/密码是1234

以下是 vps 上无法正常工作的页面示例: http://www.clubpenguincheatsy.com/cptrackers/auntarctic/密码是1234

这里是使用的编码的副本:

<TITLE>Tracker</TITLE>
<body bgcolor="#4b7bb2">
<font face="arial">
<center>
<?php
$submit = $_POST['submit']; // Gets If It has been Submitted
if($submit){ // If submitted do this.
$password = $_POST['password']; // Get the password they submitted
$status = $_POST['status']; // Get the status they submitted
$server = $_POST['server']; // Get the server they submitted
$room = $_POST['room']; // Get the room they submitted
//Now We Secure it.
if($password == "1234"){
//Now We Generate the image and stuff.
$im = imagecreatefrompng("in.png");
// Make RGB Colour For Writing
$colour = imagecolorallocate($im, 255, 255, 255); // I want mine in red(This is black). So I am just
//Gonna find out the rgb code
$font = 'BurbankBigRegular-Bold.ttf'; // 
//This ^ Is our font, It has to be the exact name and in the right folder.
//Draw Text
imagettftext($im, 20, 0, 100, 68, $colour, $font, $status);
imagettftext($im, 20, 0, 95, 95, $colour, $font, $server);
imagettftext($im, 20, 0, 85, 127, $colour, $font, $room);
//Create Image
imagepng($im,'out.png');
// Destroy - Save Server Resources
imagedestroy($im);
echo "<b> Updated! </b>";
}else
{
echo "<p><b>Incorrect Password!</b></p>";
}
}
//This form remebers what they put, so they dont have to keep typing it in.
// $_SERVER['PHP_SELF'] Means Get this document to submit to.
// If you know html this will be familiar.
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" />
<p>Password: <input type="password"  name="password" id="password" value="<?php echo $_POST['password']; ?>"/></p>
<p>Status: <select name="status"> <option value="">Select...</option>
  <option value="Online"> Online</option>
  <option value="Offline"> Offline</option>
  <option value="Tracking..."> Tracking</option>
</select>
</p>
<p>Server: <input type="text" id="server"  name="server" value="<?php echo $_POST['server']; ?>"/></p>
<p>Room: <input type="text" id="room" name="room" value="<?php echo $_POST['room']; ?>"/></p>
<p><input type="submit" id="submit" name="submit"/></p>
</form>

<p>Tracker:</p>
<p><img src="out.png" alt="Tracker" /></P>

<a href="http://ClubPenguincheatsy.com/cptrackers"s>Back to Tracker Dashbaord</a>
</center>

VPS 在 Ubuntu 上运行,我安装了 PHP、MYSQL 和 Apache。

【问题讨论】:

  • 能否在调用 out.php 时尝试缓存保护机制?切换到 "/> 后,你能更新一下事情的表现吗?
  • 我把显示的img源改成上面的代码,还是没有显示文字。
  • 主要问题是文本不再出现在新主机上
  • 能不能调整代码直接渲染PNG,去掉周围的HTML(别忘了把header content-type扔到最上面)?关键是要确保没有发生奇怪的 PHP 错误。
  • 这很奇怪,因为确切的编码工作在共享主机上(由 fatcow 托管)但是当我尝试在 VPS 上使用它时,文本没有更新。

标签: php image text dynamic tracker


【解决方案1】:

您的原始代码在所有变量上抛出Undefined variable: 错误,您应该在使用它们之前始终设置变量,这包括 $_POST 变量,您应该在开发时启用错误报告error_reporting(E_ALL),然后在您更改时也更改error_reporting(0)上传到实时站点,然后您会看到停止处理/更新图像的错误。

我已经通过一些更改修复了您的代码:

<?php 
header("Cache-Control: no-cache, must-revalidate"); //remind the browser not to cache the image
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
error_reporting(E_ALL); //Change to 0 when live
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Tracker</title>
  </head>

  <body bgcolor="#4b7bb2">
   <font face="arial">
   <center>
 <?php
    $password=''; //pre-set thos vars
    $status='';
    $server='';
    $room='';

    if(isset($_POST['submit'])){
        $password = $_POST['password'];
        if($password == "1234"){
            $status = $_POST['status'];
            $server = $_POST['server'];
            $room = $_POST['room'];

            $im = imagecreatefrompng("in.png");
            $colour = imagecolorallocate($im, 255, 255, 255);
            $font = 'BurbankBigRegular-Bold.ttf'; //

            imagettftext($im, 20, 0, 100, 68, $colour, $font, $status);
            imagettftext($im, 20, 0, 95, 95, $colour, $font, $server);
            imagettftext($im, 20, 0, 85, 127, $colour, $font, $room);
            imagepng($im,'out.png');
            imagedestroy($im);
            $result="<b> Updated! </b>";
        }else{
            $result="<p><b>Incorrect Password!</b></p>";
        }
    }
    echo (isset($result))?$result:'';
    ?>
    <form action="" method="POST" />
    <p>Password: <input type="password"  name="password" value="<?php echo htmlentities($password); ?>"/></p>

    <p>Status: <select name="status">
    <option value="">Select...</option>
    <?php 
    echo "<option value=\"Online\""; if($status=='Online'){ echo" selected=\"selected\"";} echo"> Online</option>\n";
    echo "<option value=\"Offline\""; if($status=='Offline'){ echo" selected=\"selected\"";} echo"> Offline</option>\n";
    echo "<option value=\"Tracking...\""; if($status=='Tracking...'){ echo" selected=\"selected\"";} echo"> Tracking</option>\n";
    ?>
    </select>
    </p>

    <p>Server: <input type="text" name="server" value="<?php echo htmlentities($server); ?>"/></p>
    <p>Room: <input type="text" name="room" value="<?php echo htmlentities($room); ?>"/></p>
    <p><input type="submit" name="submit"/></p>
    </form>

    <p>Tracker:</p>
    <p><img src="out.png" alt="Tracker" /></P>

    <a href="http://ClubPenguincheatsy.com/cptrackers"s>Back to Tracker Dashbaord</a>
</center>
</body>
</html>

注意表格中htmlentities()$_SERVER['PHP_SELF'] 的不使用。

编辑: 取自http://php.net/manual/en/function.imagettftext.php

您希望使用的 TrueType 字体的路径。

取决于 PHP 使用的 GD 库的版本,何时 fontfile 不以前导 / 开头,然后 .ttf 将附加到 文件名和库将尝试搜索该文件名 沿着库定义的字体路径。

当使用低于 2.0.18 的 GD 库版本时,空格 字符,而不是分号,被用作“路径分隔符” 对于不同的字体文件。无意使用此功能会 导致警告消息:警告:找不到/打开字体。为了 这些受影响的版本,唯一的解决方案是将字体移动到 不包含空格的路径。

在许多情况下,字体与脚本位于同一目录中 使用它,以下技巧将缓解任何包含问题。

<?php
// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));
// Name the font to be used (note the lack of the .ttf extension)
$font = 'BurbankBigRegular-Bold';
?>

【讨论】:

  • 上面的代码也适用于共享主机,但不适用于 VPS。为了使它工作,我需要在我的 PHP 中配置一些特定的东西吗?共享主机示例:nyanpuffle.com/auntarctic VPS 示例:clubpenguincheatsy.com/cptrackers/auntarctic
  • 有人说这是因为 out.png 在网络服务器上不可写。但是将权限更改为 777 似乎也无济于事
  • 非常抱歉,但我无能为力,您的代码和我的回答者是 GD 的最基本用法,我查看了您的 phpinfo,乍一看它看起来不错,但只能看到您的版本GD 是 1.2.42 这可能是问题,因为最新版本是 1.5.4 libpng.org/pub/png/libpng.html 就像其他人所说的确保您的权限正确。如果您的 apache 处理程序是 DSO 模式,也可以更改 suPHP,这样您的脚本不会以 (nobody) 身份运行,而是作为您的主机用户名,然后 chmod 0755 用于文件夹和 0644 用于文件,除此之外我不知道有什么问题。对不起
  • 是的,这可能很简单。我认为这与我只是将数据库和目录移动到新服务器有关。您不会碰巧知道使用更新表单更新图像的任何其他方法吗?
【解决方案2】:

$font = 'BurbankBigRegular-Bold.ttf';

替换为:

define ("_DOC_ROOT", dirname( __FILE__ ) ."/" );
$font = _DOC_ROOT.'dir_u_know/dir_u_know/BurbankBigRegular-Bold.ttf';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多