2017-09-14 71 views
0

我有這個解決方案,通過選擇正在工作的存儲文件夾來上傳用戶配置文件的圖像。 問題是如何將圖像的擴展名播放到銀行,或者如何在控制器中驗證其他擴展名可以幫助我?如何在Laravel 5.2中添加其他圖片擴展?

控制器

public function getAccount() 
{ 
    return view('account', ['user' => Auth::user()]); 
} 

public function postSaveAccount(Request $request) 
{ 
    $this->validate($request, [ 
     'email' => 'required|email|max:100' 
    ]); 

    $user   = Auth::user(); 
    $old_email  = $user->email; 
    $user->email = $request['email']; 


    $user->update(); 
    $file = $request->file('image'); 

    $file_email  = $request['email'] . '-' . $user->id . '.jpg'; 
    $old_file_email = $old_email . '-' . $user->id . '.jpg'; 
    $update = false; 

    if (Storage::disk('local')->has($old_file_email)) { 
     $old_file = Storage::disk('local')->get($old_file_email); 
     Storage::disk('local')->put($file_email, $old_file); 
     $update = true; 
    } 

    if ($file) { 
     Storage::disk('local')->put($file_email, File::get($file)); 
    } 

    if ($update && $old_file_email !== $file_email) { 
     Storage::delete($old_file_email); 
    } 
    return redirect()->route('account'); 
} 

public function getUserImage() 
{ 
    $user = Auth::user(); 
    $image ="". $user->email . '-' . $user->id . '.jpg'; 
    $file = Storage::disk('local')->get($image); 
    return Response::make($file,200,[ 'Content-Type' => 'image/jpeg']); 


} 

路線

Route::get('/account', [ 
'uses' => '[email protected]', 
'as' => 'account' 
]); 

Route::post('/upateaccount', [ 
'uses' => '[email protected]', 
'as' => 'account.save' 
]); 

Route::get('/userimage', [ 
'uses' => '[email protected]', 
'as' => 'account.image' 
]); 

查看

<img src="{{ route('account.image')}}"> 

庫GitLab https://gitlab.com/ronnyere/imageUploadLaravel.git

回答

0

我建議你使用一個擴展存儲上傳的圖片

編碼所有可能的圖像擴展到一個擴展,可以使用Intervention Image

您可以更改擴展名的所有使用上傳的圖片的...

Image::make($request->file('image')->getRealPath())->encode('jpg'); 

您可以更改jpg到另一個擴展以及

相關問題