在 Laravel 5.8 中上传之前调整图像大小。

huangapple go评论109阅读模式
英文:

Resize image before uploading in Laravel 5.8

问题

如何在上传之前将图像调整为最大边为500px的大小?

英文:

I have this function for upload image via api in Laravel:

  1. private function handleImage($image)
  2. {
  3. $exploded = explode(',', $image);
  4. $decoded = base64_decode($exploded[1]);
  5. if (Str::contains($exploded[0], 'jpeg')) {
  6. $extension = 'jpg';
  7. } else {
  8. $extension = 'png';
  9. }
  10. $fileName = Str::random() . '.' . $extension;
  11. $path = public_path() . '/images/products/' . $fileName;
  12. $file = file_put_contents($path, $decoded);
  13. $image = '/images/products/' . $fileName;
  14. return $image;
  15. }

How can I resize image to size with max side 500px before uploading?

答案1

得分: 1

You can try the Intervention Image package in Laravel to resize before uploading.

  1. 安装该包:

    composer require intervention/image

  2. 在你的文件开头添加以下代码来导入所需的类:

    use Intervention\Image\ImageManagerStatic as Image;

    use Illuminate\Support\Str;

  3. 修改 handleImage 方法如下:

    1. private function handleImage($image)
    2. {
    3. $exploded = explode(',', $image);
    4. $decoded = base64_decode($exploded[1]);
    5. $image = Image::make($decoded);
    6. // 调整图像大小,最长边不超过500像素
    7. $image->resize(500, null, function ($constraint) {
    8. $constraint->aspectRatio();
    9. $constraint->upsize();
    10. });
    11. // 根据原始图像格式设置文件扩展名
    12. if (Str::contains($exploded[0], 'jpeg')) {
    13. $extension = 'jpg';
    14. } else {
    15. $extension = 'png';
    16. }
    17. $fileName = Str::random() . '.' . $extension;
    18. $path = public_path() . '/images/products/' . $fileName;
    19. $image->save($path);
    20. return '/images/products/' . $fileName;
    21. }

https://github.com/Intervention/image

希望对你有所帮助。

英文:

You can try the Intervention Image package in Laravel to resize before uploading.

  1. Install the package:

    composer require intervention/image

  2. Add the below code at the beginning of your file to import the required classes:

    use Intervention\Image\ImageManagerStatic as Image;

    use Illuminate\Support\Str;

  3. Modify the handleImage method like below:

    1. private function handleImage($image)
    2. {
    3. $exploded = explode(',', $image);
    4. $decoded = base64_decode($exploded[1]);
    5. $image = Image::make($decoded);
    6. // Resize the image to a maximum size of 500px on the longest side
    7. $image->resize(500, null, function ($constraint) {
    8. $constraint->aspectRatio();
    9. $constraint->upsize();
    10. });
    11. // Set the file extension based on the original image format
    12. if (Str::contains($exploded[0], 'jpeg')) {
    13. $extension = 'jpg';
    14. } else {
    15. $extension = 'png';
    16. }
    17. $fileName = Str::random() . '.' . $extension;
    18. $path = public_path() . '/images/products/' . $fileName;
    19. $image->save($path);
    20. return '/images/products/' . $fileName;
    21. }

https://github.com/Intervention/image

Hope it may help

huangapple
  • 本文由 发表于 2023年3月31日 18:21:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897418.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定