doulangbi6869 2012-05-13 00:03
浏览 29

PHP标头图像类型输出与位置重定向到fs中的二进制

I'm trying to determine if I should just directly

1) have PHP fetch an image binary and output (with header as image-type) image, eg:

/* $image = ... insert curl function to fetch image */

header('Content-Type: image/png');

echo $image;

or if I should have

2) a header redirect to the URL path of the image

header('Location: http://domain.com/pathtoimage/image.png');

Some initial questions:

In the first, would that amount to any overhead with PHP having to have the image in memory in order to output it?

In the second, would this lead to any errors on clients that somehow cannot follow the PHP header redirect?

  • 写回答

1条回答 默认 最新

  • douren8379 2013-04-02 12:58
    关注

    I'm against header -> location solution as you will add an extra request to your server. Try this solution instead:

    if (file_exists($file)) {
            header('Content-Type: image/png');
            readfile($file);
            exit;
        }
    

    This solution should have minimal overhead and memory occupation.

    评论

报告相同问题?