How to get image from resources in Laravel?

后端 未结 3 2029
醉话见心
醉话见心 2021-02-19 04:36

I upload all user files to directory:

/resources/app/uploads/

I try to get image by full path:

http://localhost/resources/app/u         


        
相关标签:
3条回答
  • 2021-02-19 05:03

    You can make a route specifically for displaying images.

    For example:

    Route::get('/resources/app/uploads/{filename}', function($filename){
        $path = resource_path() . '/app/uploads/' . $filename;
    
        if(!File::exists($path)) {
            return response()->json(['message' => 'Image not found.'], 404);
        }
    
        $file = File::get($path);
        $type = File::mimeType($path);
    
        $response = Response::make($file, 200);
        $response->header("Content-Type", $type);
    
        return $response;
    });
    

    So now you can go to localhost/resources/app/uploads/filename.png and it should display the image.

    0 讨论(0)
  • 2021-02-19 05:11

    You may try this on your blade file. The images folder is located at the public folder

    <img src="{{URL::asset('/images/image_name.png')}}" />
    

    For later versions of Laravel (5.7 above):

    <img src = "{{ asset('/images/image_name.png') }}" />
    
    0 讨论(0)
  • 2021-02-19 05:27

    Try {{asset('path/to/your/image.jpg')}} if you want to call it from your blade

    or

    $url = asset('path/to/your/image.jpg'); if you want it in your controller.

    Hope it helps =)

    0 讨论(0)
提交回复
热议问题