Search code examples
phpimagelaraveluploadeloquent

uploading an image with laravel not working,


I have a question regarding the upload of an image with laravel. I have done an upload button in my blade file to upload image. I follow all the steps on the site : http://image.intervention.io/getting_started/installation#laravel I have searched answer for 2 hours on google seing that a lot of people had the same problem as mine...
So basically, in my controller, I am able to catch my image with the request doing a dd() but there is a problem at the top of the page. The use Image is not working and of course also "Image::make , resize etc... I try different settings but none of them are working....

Someone have any idea how to proceed ? here my file:

blade file :

   <div class="col-md-8">
      <form  method="POST" enctype="multipart/form-data">
         {{ csrf_field() }}
          <div class="content">
            <h1>Create a new artist or band:</h1>
                <div class="form-group">
                    <div class="col-lg-6">
                        <input class="form-control" type="text" name="artistName" placeholder="Artist Name" >
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-lg-6">
                        <input class="form-control" type="text" name="musicGenre" placeholder="Music genre" >
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-lg-6">
                        <input type="file" name="picture">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-lg-6">
                        <a class="btn btn-primary" href="{{route('show-artist') }}" > Back </a>
                        <button class="btn btn-success" type="submit"> {{ 'Submit' }}</button>
                    </div>
                </div>
        </div>
        @if (count($errors))
            <div class="form-group">
                <div class="alert alert-danger">
                    <ul>
                        @foreach($errors->all() as $error)
                            <li> {{  $error }}</li>
                        @endforeach
                    </ul>
                </div>
            </div>
        @endif
    </form>
</div>

Here my controller :

namespace projetconcert\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use projetconcert\Http\Requests\ArtistRequest;
use projetconcert\Artist;
use Intervention\Image\Image as Image;
/*use Image;*/





class ArtistController extends Controller
{
    /**
     * Display a listing of the differents artists & bands.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = Artist::all();
        return view('artists.artist',
                    [
                        'artists'=>$data,
                    ]
        );
    }

    /**
     * @Get("/create-artist")
     */
    public function getCreateArtist()
    {
        return view ('artists.create.edit');
    }

    /**
     * @Post("/create-artist")
     *
     * @param Request $request
     * @param ArtistRequest $artistRequest
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postCreateArtist(Request $request, ArtistRequest $artistRequest)
    {


        $artistRequest-> validated($request);

        try
        {
            $name = $request->input('artistName');
            $musicgenre = $request->input('musicGenre');
            $picture = $request->file('picture');

            $filename = time(). '.' . $picture->getClientOriginalExtension();

            Image::make($picture)->resize(300, 300)->save(public_path('../images/uploads/pictures/' . $filename));


            $artist = new Artist();
            $artist->artist_name = $name;
            $artist->music_genre = $musicgenre;
            $artist->picture = $filename;
            $artist->save();

            flash()->success('Your artist or band have been created !');
        }
        catch (\Exception $e)
        {

            flash()->error("Your artist or band haven't been created !");

            return redirect()->back();
        }

        return redirect()->route('show-artist');
    }


}

Thanks in advance !!


Solution

  • So apparently the problem was this error->

    gd GD Support => enabled GD headers Version => 2.2.5 GD library Version => 2.2.5 gd.jpeg_ignore_warning => 1 => 1 GDMSESSION => ubuntu $_SERVER['GDMSESSION'] => ubuntu

    I solved it following this procedure :

    sudo apt-get update

    sudo apt-get install php7.1-gd

    sudo /opt/lampp/lampp restart

    I hope it could help someone who have the same issue :-)

    Thanks again for your several answers.