[英]Can I “echo” a .jpg image through php without processing it?
想象一下:
<img src="/image.php?image=5.jpg" />
现在,在image.php
中:
header('content-type: image/jpeg');
$image = imagecreatefromjpeg($_GET['image']);
imagejpeg($image,NULL,100);
这行得通,但是脚本以这种方式加载图像,处理它,然后回显它。 这可以在不处理图像的情况下完成吗?
我想这样做的原因是我不想让人们知道图像的位置,因此我不想将完整路径写入img
src 属性。
我只需要将原始图像发送到浏览器,但不会透露它们的真实位置。
是的你可以。 只需readfile
而不是imagecreatefromXXX
+ imagejpeg
。
header('Content-Type: image/jpeg');
$src = /* process $_GET['image'] to recover the path */;
readfile($src);
/* process $_GET['images'] to recover the path */
部分意味着您需要对输入进行任何清理以避免有人请求禁止的文件。 如果您的脚本输入是一个文件路径,这可能意味着从预定义的列表中检查、剥离可能的目录分隔符、检查正则表达式等。另一种方法是将路径存储在数据库中并传递给脚本一个简单的 id,并且用它恢复文件路径。 这可能是一个更好的主意,因为用户不会在脚本 URL 上看到任何文件路径的提及(如果你只是传递一个路径,人们实际上可以猜测文件在哪里,这就是你想要阻止的)。
像http://www.php.net/manual/en/function.readfile.php这样的东西怎么样
从例子
<?php
$file = 'monkey.gif';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.