Vija's avatar
Level 1

How to upload and resize image

I want to upload resized image in /thumb folder. please check below code. it's give me unknown error when upload.

$file = Input::file('image');
        
        $destinationPath = 'uploads/business/';
        $filename = $file->getClientOriginalName();
        Input::file('image')->move($destinationPath, $filename);

        $destinationPathThumb = 'uploads/business/thumb/';
        $image_name = $file->getClientOriginalName();
        Input::file('image')->move($destinationPathThumb, $image_name);
        $image = Image::make(sprintf('uploads/business/%s', $image_name))->resize(200, 200)->save();

        return Response::json(['success' => true, 'file' => asset($destinationPath.$filename), 'filename' => $filename]);

If i remove resize code then image uploaded successfully. I have created directory as /public/uploads/business and /public/uploads/business/thumb. Can you please help me to resolve it.

0 likes
2 replies
InaniELHoussain's avatar
$image = Image::make(sprintf('uploads/business/%s', $image_name));
dd($image);

checkout if its filled

Braunson's avatar

@Vija Have you checked your Laravel logs for what the "unknown error" contains? You can also access it via command line running php artisan tail to see it real time.

As I'm unsure what version of Laravel your using or the Image manipulation package, I'm going to assume it's Laravel 5.x and intervention/image

Tutorial: There's a great lesson here on Laracasts about Image Manipulation https://laracasts.com/lessons/laravel-image-manipulation

Refactored Code (try this and watch the logs):

$file = Input::file('image');

// Define our destinations
$destinationPath = 'uploads/business/';
$destinationPathThumb = $destinationPath . 'thumb/';

// What's the original filename
$filename = $file->getClientOriginalName();

// Upload the original
$original = $file->move($destinationPath, $filename);

// Create a thumb sized from the original and save to the thumb path
$thumb = Image::make($original->getRealPath())
                ->resize(200, 200)
                ->save($destinationPathThumb . $filename);

// Respond with json
return Response::json([
    'success'   => true, 
    'file'      => asset($destinationPath . $filename), 
    'filename'  => $filename
]);

Please or to participate in this conversation.