爲了簡單起見,我試圖使用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