Intervention / Image Upload Error {{ Image source not readable }}
Asked Answered
T

6

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>
Tented answered 22/3, 2016 at 14:57 Comment(2)
provide your destination file path inside save methodBournemouth
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 :)Mockup
M
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());
Mailbag answered 22/3, 2016 at 15:40 Comment(2)
Thanks a lot Thiago Alves.Tented
Using getRealPath() return an error says try getRealPath() on array Using Laravel livewireTerresaterrestrial
P
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. :)

Plover answered 24/3, 2020 at 8:5 Comment(1)
thanks a lot for this helpful and truly guided answerOrangeism
E
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);

Easley answered 22/4, 2017 at 13:32 Comment(0)
C
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

Cyrilla answered 10/11, 2022 at 11:4 Comment(0)
N
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.

Nyctophobia answered 16/11, 2022 at 7:54 Comment(0)
S
-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.

Surrogate answered 27/3, 2022 at 10:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.