繁体   English   中英

如何在 laravel 中上传之前调整图像大小?

[英]How to resize image before upload in laravel?

我想在将图像上传到数据库之前调整其大小。 我以这种方式存储图像

  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);

        }

现在,我想在上传之前调整图像大小。 我尝试使用干预 package,我试试这个

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);

        }

我得到了这个错误

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

在上传之前调整图像大小并设置具有唯一名称的图像的最佳方法是什么?

1- 在Image::make()中使用getRealPath() )

2-将图像保存在特定路径中。

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));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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