2015-06-17 46 views
1

为了简单起见,我试图使用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命令时,它会列出图像并且它也具有适当的权限。

+0

错误的术语。 “在我的域名后面”。它应该是“在我的文档根目录之外”。 –

+0

谢谢,我修正了 –

+0

为什么不使用干预图像它的真棒。看到这个答案类似的问题:http://stackoverflow.com/questions/30875139/cant-get-images-from-storage-laravel-5/30882720#30882720 – Digitlimit

回答

2

该代码正在工作 - 问题是它的发送额外信息。该文件那里,它发现,它被发送。你的形象返回link:

HTTP/1.1 200 OK 
Date: Wed, 17 Jun 2015 22:52:03 GMT 
Server: Apache 
Connection: close 
Content-Type: image/jpeg 

但随后的输出是错误

00000050 3a 20 63 6c 6f 73 65 0d 0a 43 6f 6e 74 65 6e 74 |: close..Content| 
00000060 2d 54 79 70 65 3a 20 69 6d 61 67 65 2f 6a 70 65 |-Type: image/jpe| 
00000070 67 0d 0a 0d 0a 20 ff d8 ff e1 19 57 45 78 69 66 |g.... .....WExif| 
         ^^ 
          `this 20 here, before the ff d8 ff of JPEG. 

所以这个代码(即只对JPEG部分负责)就可以了。

public function getImage($id){ 
    $img = TitanImage::findOrFail($id); 
    $file = $img->path; 
    if (file_exists($file)) { 
     return Image::make($file)->response('jpg'); 
    } 
    else { 
    return "File doesn't exist"; 
    } 
} 

在数据流开头的额外空间可能来自一个额外的空格一些PHP文件开幕<?php标记之前,或关闭标签后,也许。

对于后者,建议您不要关闭<?php标记,即您从不包含“?>”序列。

对于前者,尽量

public function getImage($id) { 
    return "*** is there a space before the asterisks?"; 
} 

,并观察返回的HTML。除非错误出现在TitanImage中,我认为这不太可能,否则在JPEG图像之前发送的星号前应该看到相同的空格。定义getImage()函数的文件可能是一个嫌疑犯。

否则,您可以运行“grep”来查找执行的PHP文件而不是<?php开头。您可以通过将-v标志添加到grep来修改此shameless plug

+0

一旦我明天回到项目的工作,并让你知道未来,我会尝试这个。 –

+0

我只是仔细检查了泰坦项目的每个文件,你知道什么。 '<?php'之前的路线文件中有一个空格,我真的很感谢你@Iserni。 –

+0

不客气。如果你知道它发生了多少次...... :-D – LSerni

0

如果您已经安装了干预图像,然后做到这一点:

public function getImage($id){ 
    $img = TitanImage::findOrFail($id); 
    $file = $img->path; 
    return Image::make($file)->response('jpg'); 
} 

编辑: 我测试了这对我的项目,它完美的作品:

路线:

Route::get('/api/v1/titan/image/{id}',function($id){ 
    return \Image::make(storage_path("/images/$id.jpg"))->response('jpg'); 
}); 

我的项目中的图像位置:

enter image description here

输出当我访问网址:http://laravel.dev/api/v1/titan/image/1

enter image description here

铬元素检验 enter image description here

enter image description here

+1

这就是我正在做的。在我的我只是确保没有空间。 –

+0

图像路径错误或无效。我将包含代码来检查文件是否存在。同时确保你已经启用GD扩展 – Digitlimit

+0

我编辑我的问题,包括检查,看看文件是否存在。我也确认GD扩展已启用。 –