2015-10-16 99 views
1

我的用户上传存储在storage/profile_picture/user1.png中的个人资料图片。我使用Filesystem和Storage类来做到这一点。Laravel图片默认

要检索图像我用{!! Html::image(route('profile.thumbnail', $user->profilepic_filename), "Your Picture Here", ['class'=>'img-responsive']) !!}

在我的控制器我有

public function thumbnail($filename) 
{ 
    $user = User::where('profilepicture_filename', '=', $filename)->firstOrFail(); 
    $file = Storage::disk('local_profile')->get($user->profilepicture_filename); 


    //$file = URL::asset('/images/default_profilepicture.png'); //doesn't work 

    return (new Response($file, 200))->header('Content-Type', $mime); 

} 

}

我想获得一个默认的形象,如果没有找到资料图片或没有上传。我该怎么做?

感谢,

ķ

回答

0

这样的事情我只想覆盖您的User型号上的访问器(又名getter)。

http://laravel.com/docs/master/eloquent-mutators#accessors-and-mutators

任何数据库列,如profilepicture_filename可它的使用get___Attribute方法,其中___是驼峰列名检索后进行操作

class User 
{ 
    /** 
    * @return string 
    */ 
    public function getProfilepictureFilenameAttribute() 
    { 
     if (! $this->attributes['profilepicture_filename'])) { 
      return '/images/default_profilepicture.png'; 
     } 

     return $this->attributes['profilepicture_filename']; 
    } 
} 

现在你只需要做

<img src="{{ asset($user->profilepicture_filename) }}"> 

它会显示他们的图片或默认,如果他们没有。您不再需要缩略图路线。

1

你可能只是做在您查看:

@if(!file_exist($file->name)) <img src="/path/to/default.png"> @else <img src="{{$file->name}}"> @endif 

或在你的控制器:

if(!$file) 
    { 
     $file = '.../default/blah.png'; 
    }