2015-10-28 60 views
3

我想有一個路由需要圖像尺寸和來源,並通過timthumb PHP腳本返回裁剪和調整圖像。定義路由,以顯示通過timthumb PHP腳本圖像laravel

我把timthumb.php文件的文件夾中公共目錄,我寫了這個路線:

Route::get('/showImage/{w}/{h}/{src}', function ($w , $h , $src) { 

    $img = 'public/plugins/timthumb/timthumb.php?src='.$src.'&w='.$w.'&h='.$h; 
    return Response::make($img, 200, array('Content-Type' => 'image/jpeg')); 
})->where('src', '[A-Za-z0-9\/\.\-\_]+'); 

但沒有發生。

我如何可以訪問timthumb.php文件,並將其發送所需的參數,並得到結果圖像?

更新: 這是公共目錄和timthumb和圖像文件夾的佈局結構:

enter image description here

根據,我嘗試:

$img = 'public/plugins/timthumb/timthumb.php?src=../../'.$src.'&w='.$w.'&h='.$h; 

和:

$img = 'public/plugins/timthumb/timthumb.php?src=public/'.$src.'&w='.$w.'&h='.$h; 

但沒有人不爲波紋管URL工作:

http://localhost:8000/showImage/100/200/upload/slideshow/slide2.jpg 
+0

是什麼沒有任何反應意味着什麼? – Tim

+0

意味着,當我嘗試這個簡單的圖像,只是一個破碎的圖像圖標顯示在鉻。我嘗試_http:// localhost:8000/showImage/100/200/upload/slideshow/slide2.jpg_,同時給出所有參數並且_upload/slideshow/slide2.jpg_存在但未顯示。 –

+0

我認爲問題在於,您的路徑與您正在使用的腳本有關。因此它在公共尋找你的圖像/插件/ timthumb /上傳... – Tim

回答

3

我建議使用league/glide。手錶Using Glide in Laravel即可開始使用。

+0

它支持像timthumb這樣的緩存嗎? –

+0

林不知道。 Timthumb開發人員不提供對它的支持(https://www.binarymoon.co。uk/projects/timthumb/timthumb-technical-support /)。我確定你可以做聯合/滑翔所需的所有圖像處理。 –

+2

@ahmad作者在這裏滑翔。是的,它確實支持緩存,這對Glide的工作原理來說非常重要。有關更多信息,請參閱文檔[此處](http://glide.thephpleague.com/1.0/config/source-and-cache/)。 – Jonathan

2

更好的方式使用intervention/image 安裝該軟件包以便使用timthumb圖像Laravel。

更新您的路線:

Route::get('showImage/{w}/{h}/{src}', function ($w , $h , $src) 
{ 

    $img_path = public_path().'/'.$src; 
    $img = Image::make($img_path)->resize($w, $h); 

    return $img->response('jpg'); 
})->where('src', '[A-Za-z0-9\/\.\-\_]+'); 

或圖像緩存

安裝intervention/imagecache

Route::get('showImage/{w}/{h}/{src}', function ($w , $h , $src) 
    { 

     $img_path = public_path().'/'.$src; 
     $img = Image::cache(function($image)use($w,$h,$img_path) { 
       return $image->make($img_path)->resize($w, $h); 
      }); 
     return Response::make($img, 200, ['Content-Type' => 'image/jpeg']); 
    })->where('src', '[A-Za-z0-9\/\.\-\_]+'); 

圖片URL將如下:

HTT p:// < <域名>>/showImage/800/400/Desert.jpg

+0

更新了我的問題,我試圖干預了,但它有許多緩存問題,不支持高速緩存非常好,而這是對我來說非常重要。 –

+0

對緩存問題使用干預/ imagecache包。 –