【问题标题】:php creating image cant include file?php创建图像不能包含文件?
【发布时间】:2012-08-02 10:07:40
【问题描述】:
<?php
header('Content-Type: image/png');
$im = @imagecreate(100, 100);
$color = imagecolorallocate($im, 10,10,10);
imagefill($im, 50, 50, $color);
imagePNG($im);
imagedestroy($im);
?>

此代码有效,但如果我放在那里 require_once

require_once('file.php');

为什么不行?

【问题讨论】:

  • 你的 require_once 是否出错?
  • 确保 file.php 与您的脚本文件位于同一目录中,并且不打印任何其他内容
  • 所需文件位于不同文件夹中,例如:require_once("b/file.php");但是如果我需要 file.php 在不同的文件中它可以工作,我看不到任何错误,我只能看到未加载的图像
  • 在你使用header函数之前,检查headers是否已经被发送:header_sent - 这通常是错误的来源,你只能设置headers没有被发送还没有。
  • if (!headers_sent()) { header('Content-Type: image/png'); } 并且它仍在写入图像无法显示,因为它包含错误

标签: php image creation


【解决方案1】:

创建了一个仅包含phpinfo() 的页面“file.php”并通过require_once('file.php'); 调用它。

您的图片似乎没有被视为 png。它没有渲染或被破坏。

【讨论】:

【解决方案2】:

我通过在 CR 和 LF 之前消除包含的 PHP 文件的每一行末尾的任何空格(如修剪指令)来解决它。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center