28

I am trying to add a profile image upload in Laravel 5.1. I used the Intervention/Image Package but when I try to upload the image I get this error:

NotReadableException in AbstractDecoder.php line 302: Image source not readable

This is my PhotoController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Image;
use Input;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class PhotoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index() {}

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create() {}

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $img = Image::make($request->file('photo')); 
        $img->save('image.png');  
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id) {}

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id) {}

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id) {}

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id) {}
}

This is my html form:

<header>
    <div class="student_profile_sub_header w100">
        <div class="container ccenter">
            <div class="student_profile_name">
                <h4>{{$student->name}} {{$student->surname}}</h4>
            </div>
            <div class="student_profile_image">
                <img src="{{asset('assets/profile_image.png')}}">
            </div>

            <form method="POST" action="../student/profile/imageupload">
                {!! csrf_field() !!}
                <input type="file" name="photo">
                <input type="submit" value="Upload Image" name="submit">
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </form>
        </div>
    </div>
</header>
2
  • provide your destination file path inside save method Commented Mar 22, 2016 at 15:27
  • In my case, the problem was with the image name spell, because I was transferred from windows to Linux and Linux is case sensitive, so then I changed the name and now it working :) Commented Apr 12, 2022 at 4:29

6 Answers 6

32

Add the following parameter in your form tag:

enctype="multipart/form-data"

And change for this in make:

$img = Image::make($request->file('photo')->getRealPath());
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot Thiago Alves.
Using getRealPath() return an error says try getRealPath() on array Using Laravel livewire
18

This is not to answer the OP's question, but to answer someone else's who is here with the same question in a different context.

If you are using the File System method like storeAs() then you are actually uploading your file under /storage/app/public/ from /public/ scope. If so, you will need to run the following command:

php artisan storage:link

This command will create a shortcut link to the /storage directory under /public directory, and things should work normally.

Bonus

I faced a situation where I did not have any SSH access to the server and I could not upload a symlink to the cPanel, I made a temporary route like the one below and hit it once:

Route::get('/symlink', function() {
    Artisan::call('storage:link');
    echo "Storage Symbolink link is created<br>";
});

When I was done, I removed the route. :)

1 Comment

thanks a lot for this helpful and truly guided answer
3

Try using laravel collective, Make sure you have checked files to true if you are using laravel collective

{{ Form::open(['route'=>'your-route', 'class'=>'form',**'files'=>true**]) }} 
{{ Form::close()}}`

, then when requesting your file in store function add the getRealPath() like this Image::make(Input::file('artist_pic')->getRealPath())->resize(120,75);

Comments

0
  $filenamewithextension = $picture->getClientOriginalName();
            $filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
            $extension = $picture->getClientOriginalExtension();
            $thumbnail = $filename . '_thumbnail_' . time() . '.' . $extension;
            $original_picture = $filename . '_original_' . time() . '.' . $extension;
            $picture->storeAs('public/profile_images', $original_picture);
            $picture->storeAs('public/profile_images/thumbnail', $thumbnail);
            $thumbnailpath = public_path('storage/profile_images/thumbnail/' . $thumbnail);
            $this->createThumbnail($thumbnailpath, 150, 93);
            $picture_path = public_path('storage/profile_images/' . $original_picture);
            $this->createThumbnail($picture_path, 550, 340);
            $profile_pic = new Profile_Picture();
            $profile_pic->user_id = Auth::user()->id;
            $profile_pic->picture_path = $original_picture;
            $profile_pic->thumbnail = $thumbnail;
            $profile_pic->default = 0;
            $profile_pic->isVerified = 1;
            $profile_pic->save();

Also check that your corresponding paths are correct , that is the image path where you are storing the image and where you are fetching the image from are simmilar

Comments

0

in my case, the image does not exist in its path, while putting the laravel intervention code. Make sure your image exists in that path.

Comments

-1

It was

$img=Image::make('photo')->resize(300, 200);

I changed it to

$img=Image::make($request->file('photo'))->resize(300, 200);

And work for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.