phpimagelaravel

Can't get images from storage laravel 5


I'm trying to put my images into a slideshow i have on my index. the images are saved on storage/app , and then the rest of the path I retrieve from my database.

I managed to output the correct path on the <img src> tag, and the title gets output like it's supposed to, but the image won't.

I send and array from the controller and then cycle through it on my view:

Controller:

public function index()
{
    $projects = Project::all()->take(4);
    return view('index' , compact('projects'));
    
}

Index view

@foreach($projects as $project)
{{$storagePath  = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix()}}
<li><img src="{{$storagePath.$project->fetchMedia->first()->public_name}}"    title=" {{$project->name}}"></li>
        @endforeach

The HTML output is the correct path : "/home/vagrant/project/storage/app/projects/Domaa6.jpg"

but it doesn't show the image.

I already tried to use the HTML::image helper but had no success either.

Is there a way to get it working like it is or do I need to create a new controller to manipulate my images?

The fetchMedia is the function on my model that returns my Media.

public function fetchMedia()
{
    return $this->hasMany('App\Media');
}

Solution

  • What you are trying to do is not possible in the storage directory but possible only in public directory, also exposing the path or URL to your laravel storage directory creates some vulnerability and its bad practice

    However there is a way to workaround it:

    First, you need an Image package to generate the image dynamically when a certain route is referenced, I recommend intervention/image which is has many methods that makes image manage a breeze.

    To achieve this take the following steps:

    1. Install Intervention Image:

    Add Intervention Image to yourr composer.json and the do composer update

    "require": {
            "php": ">=5.5.9",
            "laravel/framework": "5.1.*",
            "intervention/image": "2.*",
            "intervention/imagecache": "2.*"
    
            .....
    

    In the $providers array add the service providers for this package.

    'Intervention\Image\ImageServiceProvider'
    

    Add the facade of this package to the $aliases array.

    'Image' => 'Intervention\Image\Facades\Image'

    Configure Intervention Image in Laravel 5

    $ php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"
    

    Publish configuration in Laravel 4

    $ php artisan config:publish intervention/image
    

    For more installation detail https://github.com/Intervention/image

    After you install intervention/image you can do something like this:

    Image::make($path=storage_path('stock-photos/image1.jpg'));

    2. Setup a route that will handle image requests

    Route::get('stock-photos/{image}', function($image){
    
        //do so other checks here if you wish
    
        if(!File::exists( $image=storage_path("stock-photos/{$image}") )) abort(404);
    
        return Image::make($image)->response('jpg'); //will ensure a jpg is always returned
    });
    

    3. Then later in the view you can do:

    @foreach($projects as $project)
        <li>
            <img src="{{url('stock-photos/'. $project->fetchMedia->first()->public_name)}}" title="{{$project->name}}">
        </li>
    @endforeach