Mohammadsgh's avatar

upload image and resize

hi how to upload image file and create resize image file from image file uploaded ?

0 likes
11 replies
Mohammadsgh's avatar

how to use Intervention Image for my Question?please help me?

RachidLaasri's avatar
Image::make('public/foo.jpg')->resize(320, 240)->insert('public/watermark.png');
mstnorris's avatar

@Mohammadsgh what have you tried? What you're trying to achieve consists of a few steps.

  1. Get the file from the client, I'd suggest starting with a simple file input or if you're more familiar with this sort of thing; dropzonejs.
<input type="file" name="file">
  1. Make sure your form has enctype="multipart/form-data" otherwise it won't accept images.
<form action="/upload" method="POST" enctype="multipart/form-data">
    <input type="file" name="file">
    ...
    <button type="submit">Submit</button>
</form>
  1. Create the routes to respond to the POST request.
post('upload', ['as' => 'upload_path', 'uses' => 'MyController@upload']);
  1. Process the request as you normally would

MyController.php

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

    $name = time() . $file->getClientOriginalName(); // prepend the time (integer) to the original file name

    $file->move('uploads', $name); // move it to the 'uploads' directory (public/uploads)

    // create instance of Intervention Image
    $img = Image::make('public/foo.jpg')

    // resize image to fixed size
    // See the docs - http://image.intervention.io/api/resize
    $img->resize(300, 200);

    // The below part is optional, this is if uploads "belongTo" a "User"
    // so you automatically insert the relation, if you don't need it, just
    // remove it.
    $user->uploads()->create([
        'original_name' => $name
    ]);

    return 'done';
}
  1. That's it.
1 like
Mohammadsgh's avatar

i,m using from this codes.return error from this line $image = Image::make('public/upload/news/' .$extension);

content error: Whoops, looks like something went wrong. Symfony\Component\Debug\Exception\FatalErrorException in /var/www/html/shop/app/Http/Controllers /User/NewsController.php line 159 Call to undefined method Faker\Provider\Image::make()

if($request->ajax()) { $validator = Validator::make($request->all(),$rules,$messages);

        if($validator->fails())
        {
            return Response::make( array_merge(['test'=>404],$validator->errors()->all()));
        } else {
            if(Input::file('image')->isValid())
            {
                $extension = Input::file('image')->getClientOriginalExtension();
                $fileName = rand(11111,99999).'_'.time().'.'.$extension;
                Input::file('image')->move(public_path('upload/news'), $fileName);
                $image = Image::make('public/upload/news/' .$extension);
                $image->resize(20,20);
                $image->save('public/upload/news/1.jpg');

                $link = '<div class="alert alert-success">'. '/upload/news/' . $fileName . '</div>';

                return Response::make([$link]);
            } else {
                return Response::make(['<div class="alert alert-danger">عکس معتبر نیست</div>',]);
            }
        }
    }
Mohammadsgh's avatar

hi mstnorris abdelkbir noting my account. abdelkbir and you and other Friends can not help me .I still remain a problem how to resolve this error? Call to undefined method Faker\Provider\Image ::make()

line error is : $image = Image::make('public/upload/news/' .$extension);

mstnorris's avatar

That's because you're importing the wrong Class.

I think you need to add the following instead of Faker\Provider\Image.

Use:

use Intervention\Image\ImageManagerStatic as Image;
Mohammadsgh's avatar

use Intervention\Image\ImageManagerStatic as Image; not work this code.what i do?please help me? :|

SYPOMark's avatar

@mstnorris We've been struggling with allowing a customer to upload an image along with some other information about a product in a form added to the settings screen for a little while now, and it looks to us as if there's a few more files to create/edit than just a Blade, a JS file, the /spark/src/Http/routes.php file and setting up a Controller.

We've been trying to follow the way that a new user photo is uploaded. This involves Interactions as well. Wouldn't any form that involved uploading an image/any kind of file have to do that as well?

Apologies, we're getting nowhere with this after nearly a week's work.

With kind regards,

Mark

Please or to participate in this conversation.