imagepng 出现问题

发布于 2024-09-15 18:20:03 字数 1564 浏览 7 评论 0原文

我正在与 PHP 的 GD 库作斗争。

我编写了一个名为 foo.php 的脚本,它输出一个 png:

header('Content-type:image/png');
$img = imagecreatefrompng($url) or die('bad url:'.$url);
imagepng($img);
imagedestroy($img);

它工作正常。它的目的是接受 GET 参数,然后输出适当的图表:(

例如)foo.php?id=2 在任何浏览器中放置一个漂亮的图表。

这是我的问题:

在另一个脚本(baz.php)中,我想使用readfile或类似的东西来获取由foo.php创建的图像并拥有baz。 php 将其发送到浏览器。 但无论我尝试什么,当我调用 baz.php 时,它似乎都不起作用

baz.php 中的示例:

switch($id) {
    case '1':
         readfile('foo.php?id=1');
         break;
    case '2':
         readfile('foo.php?id=2');
         break;
    // and so on...
}

我收到一条错误消息:

failed to open stream: No such file or directory...

如果我输入完整的网址或路径:

readfile('http://localhost/dev/foo.php?id=1');
readfile('C:/xampp/htdocs/dev/foo.php?id=1');

...我得到同样的错误。

如果我将标头添加到 baz.php:

header('Content-type:image/png');
readfile($url);

在 Firefox 中,我得到“图像”http://localhost/dev/ baz.php”无法显示,因为它包含错误。在 Chrome 中,它显示一个大小为 27.82kb、尺寸为 0x0 的损坏图像

allow_url_fopen 已打开,正如我提到的,foo.php 已打开。 php 生成 png 没有任何问题;我似乎无法进入 baz.php,

我可以,例如只需输入:

header("Location: foo.php?id=1");

它会重定向并输出图像,但我不会。想要进行 302 重定向,我需要 baz.php 将图像推送到浏览器。如果我将文件保存为静态文件,它也能正常加载。只是似乎不想处理动态文件。

非常感谢任何帮助。

I am struggling with PHP's GD library.

I have written a script called foo.php which outputs a png:

header('Content-type:image/png');
$img = imagecreatefrompng($url) or die('bad url:'.$url);
imagepng($img);
imagedestroy($img);

It works fine. Its purpose is to accept a GET parameter and then spit out the appropriate graph:

(e.g.) foo.php?id=2 puts a nice graph in any browser.

Here's my problem:

In another script (baz.php), I'd like to use readfile or something similar to take the image created by foo.php and have baz.php send it to the browser. But no matter what I try, it won't seem to work when I call baz.php

Example from baz.php:

switch($id) {
    case '1':
         readfile('foo.php?id=1');
         break;
    case '2':
         readfile('foo.php?id=2');
         break;
    // and so on...
}

I get an error saying:

failed to open stream: No such file or directory...

If I put in the full url or the path:

readfile('http://localhost/dev/foo.php?id=1');
readfile('C:/xampp/htdocs/dev/foo.php?id=1');

...I get the same error.

If I add the header to baz.php:

header('Content-type:image/png');
readfile($url);

In firefox I get "The image "http://localhost/dev/baz.php" cannot be displayed, because it contains errors. In Chrome it shows a broken image 27.82kb in size with dimensions of 0x0

allow_url_fopen is on, and as I mentioned, foo.php is producing pngs without any problems; I just can't seem to get in out of baz.php, which I need to.

I can, for instance just put:

header("Location: foo.php?id=1");

and it will redirect and output the image, but I don't want to do a 302 redirect, I need baz.php to push the image out to the browser. If I save the file as a static file, it will load that fine as well. It just doesn't seem to want to handle the dynamic file.

Any help is very much appreciated. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风情万种。 2024-09-22 18:20:03

解决了:

问题#1:

您不能使用 php 的 readfile() 来包含由 php 动态生成的 png 如果它位于同一服务器上< /em>。

为什么? 因为 readfile 将包含原始 php 代码,而不是将该 php 代码渲染到图像中。如果您想从另一台服务器调用它,则 readfile 可以正常工作。

因此,您可以包含/需要该文件(这样它将被渲染为 png),但是...

问题 #2:

您不能直接包含带有参数/查询字符串的文件(例如以下代码会报错,无法打开文件:

include('baz.php?id=1'); //this won't work

解决方案:

  • 在 GET 字符串中手动设置参数(例如 $_GET['id'] = 1;
  • 包含文件: include( 'baz.php');

另请注意:Apache 的 virtual() 命令也不适用于 GET,因为仅传递 QUERY_STRING($_GET 是从父脚本复制的) ):

PHP.net 对 virtual() 的描述

Figured it out:

Issue #1:

You cannot use php's readfile() to include a png that is generated dynamically by php if it is on the same server.

Why? Because readfile will include the raw php code rather than rendering that php code into an image. If you want to call it from another server, readfile works fine.

So, you can include/require the file instead (so it will be rendered into a png), however...

Issue #2:

You cannot include a file with parameters / query string directly (e.g. the following code will error that it cannot open the file:

include('baz.php?id=1'); //this won't work

Solution:

  • Set the parameter manually in the GET string (e.g. $_GET['id'] = 1;)
  • Include the file: include('baz.php');

Also note: Apache's virtual() command will also not work with GET because only QUERY_STRING is passed along ($_GET is copied from the parent script):

PHP.net's description of virtual()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文