aleksov's avatar

Issue with Image Intervention when try to upload

I have this controller and trying to post image into /images folder so I write:


 public function store(Requests\VoucherRequest $request)
    {
        
        //$photo= null;
        $file = array('photo' => $request->file('photo'));
        // setting up rules
        $rules = array('photo' => 'required|image|max:10000'); //mimes:jpeg,bmp,png and for max size max:10000
         // doing the validation, passing post data, rules and the messages
        $validator = Validator::make($file, $rules);
        if ($validator->fails()) {
        // send back to the page with the input data and errors
         return redirect()->back()->withErrors(["photo" => "Photo requirments - format: jpg, jpeg, png | max. size: 1 MB"]);
         
        }
        else {
    // checking file is valid.
        if ($request->file('photo')->isValid()) {
        $destinationPath = public_path().'/images'; // upload path
        $extension = $request->file('photo')->getClientOriginalExtension(); 

        // getting image extension
        $ran = str_random(5);
        $photo  = $ran.'.'.$extension; // renameing image
         $request->file('photo')->move($destinationPath, $photo); 
        
         Image::make($request->file('photo'))->resize(300, 200)->save('images/'.$ran.'ORG.'.$extension);

         }
        else {
      return redirect()->back()->withErrors(["photo" => "Photo requirments - format: jpg, jpeg, png | max. size: 1 MB"]);
        }
        }

but now i get only:

NotReadableException in Decoder.php line 20: Unable to read image from file ().

WHat is a problem here? WHy I cant get and rezise image ... everything else works just Image Intevention dont work...

0 likes
10 replies
aleksov's avatar

I try that:

     $img = Image::make($request->file('photo')->getRealPath())->resize(300, 200)->save('images/'.$ran.'ORG.'.$extension);

but now I get:

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

tomopongrac's avatar

Do you in your form have

enctype="multipart/form-data"
aleksov's avatar

Yes I have... this code:

     $request->file('photo')->move($destinationPath, $photo);  

WORKS GOOD

so without resizing works good adn upload photo to folder...

aleksov's avatar

maybe is problem becouse when I add code:

    dd($request->file('photo')->getRealPath());

I get:

"C:\wamp\tmp\php7A0E.tmp"

WHY? This is not my real path!

tomopongrac's avatar

Than add code with path to uploaded image

Image::make($destinationPath . "/" . $photo)->resize(300, 200)->save('images/'.$ran.'ORG.'.$extension);
aleksov's avatar

but I get photo from form ... user upload photo from own computer ...

tomopongrac's avatar

But than you save that foto to server and than you can resize that save photo or i missing something

zachleigh's avatar

This is what I do:

trait SavesImages
{
    /**
     * Reduce filesize and store image.
     *
     * @param  Request $request
     * @param  string  $path 
     * @param  string  $propName
     * 
     * @return array
     */
    public function storeImage(Request $request, $path, $propName = 'image')
    {
        $imagePath = $request->file($propName)->store($path);

        $name = collect(explode('/', $imagePath))->last();

        // image intervention stuff here 
        $image = Image::make(Storage::get($imagePath))->encode('jpg', 50);

        Storage::put($imagePath, $image);

        return [
            'name' => $name,
            'imagePath' => $imagePath
        ];
    }
}

So save the image first, then make the Image Intervention image and process it, and then save it again.

Nagibaba's avatar

try this $img = Image::make(str_replace('\', '/',$request->file('file')));

There may be separator problem

Please or to participate in this conversation.