0

我的应用程序通过http get请求发送urlencoded或base64encoded字符串,该字符串包含图像数据,大多数是从php文件下载的,但我不知道php如何从我的字符串中下载urlencoded或base64encoded的图像,请帮帮我伙计们……我迷路了

4

1 回答 1

2

向用户提供图像

// we assume $imageData is PNG
$image = urldecode($imageData);
// or
$image = base64_decode($imageData);

// in case of force download change Content-Type: image/png to
// application/octet-stream and Content-Disposition: inline to attachment
header('Content-type: image/png');
header('Content-Disposition: inline; filename=' . md5($image) . '.png');
header('Content-Length: ' . strlen($image));

echo $image;
exit;

如果您不知道,问题在于检测正确的 Content-Type。但是浏览器应该能够自行自动检测它。

关于缓存
PHP 的一些说明会隐式发送标题,这会阻止浏览器缓存检索到的数据。我建议您手动设置这些标头(Cache-Control、Expires、Pragma)。为了正常工作,每个图像都必须由唯一的 URL 提供。还要尽量避免开始会话。在具有公共访问权限的访问量很大的网站上,您可以轻松地用冗余会话文件淹没网络服务器。

将图像保存到文件

$image = urlencode($imageData);
// or
$image = base64_decode($imageData);

if (!file_put_contents('abs/path/to/save/file.png', $image)) {
    throw new Exception('Image could not be saved');
}
于 2012-06-13T17:34:48.833 回答