2015-01-26 40 views
1

我有一个Laravel 4应用程序,它显示来自位于远程服务器上的另一个PHP应用程序的图像。如何从Laravel中的URL缓存图像

我想知道什么是从本地Laravel应用程序缓存来自远程服务器的图像的最佳解决方案。

请注意,这是几乎不可能来/过去的整个images目录从远程服务器复制,因为有超过15万倍的图像,是regurlary更新(每分钟),并有一个数据库相关太(文件名以数据库中的列值)。

我正要使用Rsync来同步两个目录(远离本地),但我还需要在显示图像之前调整图像大小,并以不同于远程服务器上的方式组织图像子目录。

首先,我已经安装了Intervention Image Class包上Laravel和我创建了一个Route

Route::get('photos/item/{size}/{reference}/{photo}', '[email protected]'); 

而在ImgController

public function showImg($size, $reference, $photo) 
{ 
    switch ($size) { 
     case '300x225': 
      $jpg = Image::make('http://myremoteapp.com/uploads/' . $reference . '/' . $photo)->resize(300, 225)->response('jpg'); 
     break; 
    } 
    return $jpg; 
} 

这工作,但它并没有保持图像浏览器的缓存也会产生性能问题,因为每次打开页面时都必须下载和调整图像大小。

我听说过Intervention Image Cache,但我不确定它是否适用于从URL获取的图像。

任何意见和建议如何解决这个以适当的方式将不胜感激。

回答

3

可以使用缓存路线为:

Route::filter('cache', function($route, $request, $response = null) { 
    $key = 'route-' . safe_title(URL::full()); 
    if(is_null($response) && Cache::has($key)) { 
     return Cache::get($key); 
    } elseif(!is_null($response) && !Cache::has($key)) { 
     // Code this here 
     Cache::put($key, $response->getContent(), 30); 
    } 
}); 

    Route::group(array('before' => 'cache', 'after' => 'cache'), function() { 
    Route::get('photos/item/{size}/{reference}/{photo}', '[email protected]'); 
}