为了简单起见,我试图使用php来显示图像,以帮助尝试并防止任何类型的安全漏洞。我已经通过一个PHP脚本读取图像,我想要渲染它,所以我不想去http://example.com/path/to/uploads/dir/image.jpg
,我想让用户能够去http://example.com/path/to/script/image1
,并让他们能够看到图像。获取图像以使用php呈现
这样我就可以将图像路径(http://example.com/path/to/uploads/dir/
)隐藏在我的文档根目录之外(无法由域路径完全访问)。因此,如果域的路径是/path/to/public_html/
,那么我需要将图片上传到/path/to/uploads/
,以便在冲浪我的域时无法导航到上传目录。
我的问题是图片我一直想渲染,不会渲染,我不确定为什么。 MD5校验和是相同的,并且HTTP头是相似的。图像的direct path,与图像的desired path相比。
我使用Laravel作为一个框架/骨干网,但我不认为要多,我的代码的问题要呈现的图像低于
public function getImage($id){
$img = TitanImage::findOrFail($id);
// header('Content-type: image/jpeg');
// header('Content-Length: ' . filesize($img->path));
// header('Accept-Ranges: bytes');
// echo file_get_contents($img->path);
$file = $img->path;
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
// exit();
return;
}
我也用webconfs检查的头两个所需的图像端点。
编辑1
在问候有关intervention/image
包,我改变了我的代码,我仍然得到同样的结果,意见之一;然而,内容长度已经改变,它变得更长了。
public function getImage($id){
$img = TitanImage::findOrFail($id);
// header('Content-type: image/jpeg');
// header('Content-Length: ' . filesize($img->path));
// header('Accept-Ranges: bytes');
$file = $img->path;
return Image::make(trim($file,' '))->response();
}
编辑2
在此编辑,@Digitlimit提到的检查,看是否存在图像。这是我现在的代码。
public function getImage($id){
$img = TitanImage::findOrFail($id);
$file = $img->path;
if(file_exists($file))
return Image::make($file)->response('jpg');
else
return 'File doesnt exist';
}
我也做了测试,看看如果我只是返回$file
代替Image::make
情况下会发生什么,我得到了正确的路径,即。当我去SSH到服务器和cd
到路径减去图像名称路径存在和有权限。当我执行ls -la
命令时,它会列出图像并且它也具有适当的权限。
错误的术语。 “在我的域名后面”。它应该是“在我的文档根目录之外”。 –
谢谢,我修正了 –
为什么不使用干预图像它的真棒。看到这个答案类似的问题:http://stackoverflow.com/questions/30875139/cant-get-images-from-storage-laravel-5/30882720#30882720 – Digitlimit