14

I have a small problem concerning the resizing process of a given image, I am trying to submit a form containing an input type -->file<-- I was able to upload a picture without resizing it, after that I decided to resize that image so I installed the Intervention Image Library using:

composer require intervention/image

then I integrated the library into my Laravel framework

Intervention\Image\ImageServiceProvider::class
'Image' => Intervention\Image\Facades\Image::class

and finally I configured it like following

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

my controller is like the following

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Image; 

class ProjectController extends Controller{

public function project(Request $request){  


    $file = Input::file('file');
    $fileName = time().'-'.$file->getClientOriginalName();

    $file -> move('uploads', $fileName);
    $img=Image::make('public/uploads/', $file->getRealPath())->resize(320, 240)->save('public/uploads/',$file->getClientOriginalName());

}
}

but instead of resizing the pic the following exception is throwed

NotReadableException in AbstractDecoder.php line 302:
Image source not readable
HiDeoo
  • 10,353
  • 8
  • 47
  • 47
KaldoLeb
  • 169
  • 1
  • 1
  • 8
  • Could you please check this [answer](http://stackoverflow.com/questions/33468437/getting-error-notreadableexception-in-abstractdecoder-php-line-302/33469360#33469360)? – AddWeb Solution Pvt Ltd Oct 11 '16 at 05:33
  • The problem in this code is, that $file->getRealPath() always returns false if there is a previous call to $file->move() – shock_gone_wild Oct 11 '16 at 06:20
  • Could it be that you dont have permission (chmod 600)? Or maybe php.ini -> php_value post_max_size (maybe the image is too big)? – Denis Solakovic Oct 11 '16 at 14:00

7 Answers7

14

Shouldn't it be Image::make($file->getRealPath()) instead of Image::make('public/uploads/', $file->getRealPath())?

Image::make() doesn't seem to take two arguments, so that could be your problem.

Try this:

$file = Input::file('file');
$fileName = time() . '-' . $file->getClientOriginalName();

$file->move('uploads', $fileName);

$img = Image::make($file->getRealPath())
    ->resize(320, 240)
    ->save('public/uploads/', $file->getClientOriginalName());

Or if you want to do it without moving the file first, try this:

$file = Input::file('file');
$img = Image::make($file)
    ->resize(320, 240)
    ->save('public/uploads/', $file->getClientOriginalName());
borfast
  • 2,194
  • 1
  • 15
  • 34
  • 2
    Nop, that not working I already tried. ``Image::make($file->getRealPath())`` OR ``Image::make($file)`` give error as ``Image source not readable``. It was working in Laravel 5 but not in L5.2. – Tarunn Oct 05 '16 at 07:00
  • And have you checked if the file is actually there, by manually looking into the directory? Also, try using a debugger to follow the code execution and see what is really happening. – borfast Oct 05 '16 at 08:20
  • @Tarunn I have explanation for that in this answer https://stackoverflow.com/questions/38923863/image-source-not-readable-in-laravel-5-2-intervention-image/45768289#45768289 – Abdalla Arbab Aug 19 '17 at 06:21
3

In L5.2 its not directly possible to get image from Input facade. For that we need to first store the image on server and then give path into Image facade to do operations on image.

Code goes likes this:

if ($request->hasFile('picture') ) {

        $destinationPath = public_path('uploads/user');
        $photoname = date("YmdHis");
        $file_extention = '.'.$request->file('picture')->getClientOriginalExtension();
        $photo = $photoname.$file_extention;
        $file_check = $request->file('picture')->move($destinationPath, $photo);

        $thumb_path = $destinationPath.'/thumbnail/'.$photo;

        $new_filePath =  $destinationPath.'/'.$photo;

        $assets_path = url('uploads/user/');

        $img = Image::make($assets_path.'/'.$photo)->fit(100)->save($thumb_path,40);

        $data['picture'] = $photo;           
    }

I was looking for direct solution i.e. as it was possible previously to take image directly from Input facade. If anyone of you have direct solution, show your code here and I'll reward you this bounty. Cheers.

Tarunn
  • 1,038
  • 3
  • 23
  • 45
1

Uploading a file and resizing it before saving is as easy as that: (Without validation or checks)

You can directly pass an instance of UploadedFile to InterventionImage::make()

public function upload(Request $request)
{
    $file = $request->file('file');

    $filename = $file->getClientOriginalName();

    $img = \Image::make($file);
    $img->resize(320, 240)->save(public_path('uploads/'.$filename))

}

If you want to save original size and resized image:

    $img->save(public_path('uploads/'.$filename))
        ->resize(320, 240)
        ->save(public_path('uploads/thumb_'.$filename));

This was just tested on currently latest 5.2 version with is 5.2.45

[EDIT:]

If you call

$file->move();

Don't use

$file->getRealPath() 

afterwards, because this will return false after calling move()

    $filename = $file->getClientOriginalName();
    $file->move('uploads', $filename);
    dd($file->getRealPath());
shock_gone_wild
  • 6,700
  • 4
  • 28
  • 52
1

This problem can also occur when you don't want to name the uploaded file using

$filename = $file->getClientOriginalName();

method. If you want to create your own uploaded file names, let's say, using the method below

// $filename_without_ext = $request->input('name');
$filename_without_ext = Str::slug($request->input('name'));
$file_extension = pathinfo($logo->getClientOriginalName(), PATHINFO_EXTENSION);
$filename =   time() . '-' . $filename_without_ext . '.' . $file_extension;

If you use Laravel 5.8 and you get that error and you are trying to create your own file name, which is mostly necessary, you can fall into this trap. You should check the name field if it is coming as a form input. For example for the code above if you don't use the Str::slug function, which is a Laravel helper function, like the code with comments, you can run into problems because of the white space that the form field can have.

Mycodingproject
  • 1,031
  • 1
  • 9
  • 13
1

This work fine for me

$file->move($location,$file_name);  
$img = Image::make($location.'/'.$file_name)->fit(100)->save(public_path('thumbnails'.'/'.$file_name),50);
Mostafa EGY
  • 19
  • 1
  • 5
0

This problem occurred when you resize the image after moving it

$file->move('uploads', $fileName);

$file->getRealPath() will return false after moving the image. You need to resize the image before the moving process. That's it ;)

$img=Image::make('public/uploads/', $file->getRealPath())->resize(320, 240)->save('public/uploads/',$file->getClientOriginalName());
$file->move('uploads', $fileName);
Abdalla Arbab
  • 1,360
  • 3
  • 23
  • 29
0

i just solved this problem.

change this line:

$file -> move('uploads', $fileName);

to

$file = $file -> move('uploads', $fileName);

now $file->getRealPath() has a valid value.

hope this works for you.

Saeed
  • 118
  • 1
  • 11