2017-06-22 52 views
1

对于旅行应用程序,移动应用程序需要从其城市代码获取每个城市的默认图像。如果未找到图像,则返回默认图像Laravel干预

例如:example.com/imageCache/thumbnail/JFK.png

凡缩略图自定义过滤器定义为:

/** 
* Sample filter for image manipulation 
* via image cache 
*/ 

namespace App\ImageFilters; 


use Intervention\Image\Filters\FilterInterface; 
use Intervention\Image\Image; 
use Intervention\Image\ImageManagerStatic; 

class Thumbnail implements FilterInterface 
{ 

    /** 
    * Applies filter to given image 
    * 
    * @param Image $image 
    * @return Image 
    */ 
    public function applyFilter(Image $image) 
    { 
     //TODO: Do something to check if the image doesn't exist. 

     $gradient = ImageManagerStatic::make(public_path('images/gradient.png')); 
     return $image->fit(200, 200)->insert($gradient,'center')->blur(); 
    } 
} 

的应用然而抛出404调用此函数甚至之前。

我想显示一个默认图像,如果图像没有找到。

在此先感谢。

+0

我认为THRE是一个问题,在你的路线! – Maraboc

+0

我们可以看到make命令吗? – Ohgodwhy

+0

它适用于存在的图像。只要图像不存在,它就会崩溃。 –

回答

1

这种情况下的URL操作可能不起作用。

写路线getCityImage/{cityCode}为:

public function getCityImage($cityCode){ 

    if(file_exists('path_to_city_images/'.$cityCode.'.png'){ 
     $image = Intervention\Image\Image::make('path_to_city_images/'.$cityCode.'.png'); 
     return $image->filter(new Thumbnail()); 
     } 
     else { 
     return $your_default_image; 
     } 

} 
相关问题