如何在不使用任何HTML的情况下显示图像。
我们说我有一些受保护的图片,这些图片可能只会向某些用户显示。 受保护的映像位于目录images / protected / NoOneCanViewMeDirectly.png
中然后我们在目录images / ShowImage.php中有一个PHP文件,它检查是否允许用户查看图像。
如何仅使用PHP显示图像。
如果您不确定我的意思,这就是我想要显示图像的方式。 http://gyazo.com/077e14bc824f5c8764dbb061a7ead058
解决方案不是echo '<img src="images/protected/NoOneCanViewMeDirectly.png">';
答案 0 :(得分:0)
您可以使用以下语法标出图像
header("Content-type: image/png");
echo file_get_contents(__DIR__."/images/protected/NoOneCanViewMeDirectly.png");
答案 1 :(得分:0)
if (<here your logic to show file or not>)
$path = $_SERVER["DOCUMENT_ROOT"].<path_to_your_file>; // to get it correctly
$ext = explode(".", $path);
header("Content-Type: image/".array_pop($ext)); // correct MIME-type
header('Content-Length: ' . filesize($path)); // content length for most software
readfile($path); // send it
}
注意:1。image/jpg
不是正确的MIME类型,而是使用image/jpeg
(需要额外检查),2。readlfile()
is better而不是echo file_get_contents()
对于二进制数据,3。总是尝试to provide content length用于浏览器和其他软件
答案 2 :(得分:-2)
您可以使用imagepng
将png图像输出到浏览器:
$im = imagecreatefrompng("images/protected/NoOneCanViewMeDirectly.png");
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);