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

时间:2012-05-13 00:03:31

标签: php image curl header filesystems

我正试图确定我是否应该直接

1)让PHP获取图像二进制并输出(带标题为图像类型)图像,例如:

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

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

echo $image;

或者我应该

2)标题重定向到图像的URL路径

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

一些初步问题:

在第一个中,这是否会导致PHP必须将图像存储在内存中才能输出?

在第二种情况下,这是否会导致客户端上的任何错误无法跟踪PHP标头重定向?

1 个答案:

答案 0 :(得分:0)

我反对标题 - >位置解决方案,因为您将向服务器添加额外的请求。 请尝试使用此解决方案:

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

此解决方案应具有最小的开销和内存占用。