简体   繁体   中英

How to resize image before upload in laravel?

I want to resize image beofre upload it into database. I store image in this way

  if ($image) {
     $image_name = hexdec(uniqid());
       $ext      = strtolower($image->getClientOriginalExtension());
       $image_full_name = $image_name . '.' . $ext;
       $upload_path     = 'foods/';
       $upload_path1    = 'images/foods/';
       $image_url       = $upload_path . $image_full_name;
       $success         = $image->move($upload_path1, $image_full_name);

        }

Now, I want to resize image before uploading it. I try to use intervation package, I try this

if ($image) {
     $image_name = hexdec(uniqid());
       $ext      = strtolower($image->getClientOriginalExtension());
       $image_full_name = $image_name . '.' . $ext;
       $upload_path     = 'foods/';
       $upload_path1    = 'images/foods/';
       $image_url       = $upload_path . $image_full_name;
       $img = Image::make($image)->resize(300, 200);
       $img->save($upload_path1, 60, $image_full_name);

        }

and I got this error

 "Encoding format (1691942233153059.jpg) is not supported."

What is the best way to resize image before upload and also set image with unique name?

1- use getRealPath() inside Image::make()

2- save image in particular path.

if($request->hasFile('image')) {
    $image       = $request->file('image');
    $file_name    = $image->getClientOriginalName();
    $image_resize = Image::make($image->getRealPath());              
    $image_resize->resize(300, 100);
    $image_resize->save(public_path('img/' .$file_name));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM