Laravel 图像干预是一种压缩用户上传图像的好方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37430287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:52:04  来源:igfitidea点击:

Is laravel image intervention a good way to compress user uploaded images?

phpimagelaravelimage-compression

提问by Jim Peeters

I was checking out this laravel library to compress user uploaded images.http://image.intervention.io/

我正在检查这个 laravel 库来压缩用户上传的图像。http://image.intervention.io/

I was wondering if this is a good idea for user uploaded images (for profilepictures)? What if the user uploads a picture with a size of 1400x600 and it will be resized to 200x200? Will it be a stretched out image?

我想知道这对于用户上传的图片(用于个人资料图片)是否是一个好主意?如果用户上传大小为1400x600的图片,它会被调整为200x200怎么办?它会是一个拉伸的图像吗?

回答by Alexey Mezenin

Yes, it will be stretched. You want to try fit()method:

是的,它会被拉伸。你想尝试的fit()方法:

Combine cropping and resizing to format image in a smart way. The method will find the best fitting aspect ratio of your given width and height on the current image automatically, cut it out and resize it to the given dimension.

结合裁剪和调整大小以智能方式格式化图像。该方法将自动在当前图像上找到给定宽度和高度的最佳长宽比,将其剪切并调整到给定尺寸。

// crop the best fitting 1:1 ratio (200x200) and resize to 200x200 pixel
$img->fit(200);

回答by Ohgodwhy

Intervention is great for handling just this. Here's a quick code snippet to get you going in the right direction:

干预非常适合处理这个问题。这是一个快速代码片段,可让您朝着正确的方向前进:

$resize_to = $this->sizes(); //some arbitrary array

//generate our custom image sizes
foreach ($resize_to as $idx => $width) {
    if ($img = Image::make($file->getRealPath())) {
        $img->resize(width, null, function ($constraint) {
            $constraint->aspectRatio();
            $constraint->upsize();
        });
        $img->encode($encoding_options);
        $img->save($path);
    }
}

In the above we're looping over some array of widths that we want to have images resized to. Pay attention to the $constraintsthat we've declared below. The aspectRatio()ensures the image never loses it's aspect ratio. The ->upsize()prevents images from being upsized and losing the resolution.

在上面我们循环了一些我们想要调整图像大小的宽度数组。注意$constraints我们在下面声明的。的aspectRatio()保证了图像不会失去它的纵横比。在->upsize()从防止图像被升迁,失去分辨率。

What you're asking for - resizing a 1400x600 image to 200x200is unreasonable.

您所要求的 - 将 1400x600 图像调整200x200为不合理。

Instead, you should just resize to one dimension and make sure that your HTML can account for the rest. For example, in the above we only observe widths. So we'll assume in your $this->sizes()array, one of them is 200. But the image was a different ratio, so it didn't produce a 200x200, it produced a 200x150. Handle that in the HTML by using a flexbox super easily:

相反,您应该只将大小调整为一维,并确保您的 HTML 可以解决其余的问题。例如,在上面我们只观察widths。所以我们假设在你的$this->sizes()数组中,其中一个是 200。但是图像的比例不同,所以它没有产生 200x200,它产生了 200x150。使用 flexbox 非常轻松地在 HTML 中处理它:

<div class="flex-box-centered">
   <img class="flex-img" src="..."/>
</div>

And the CSS:

和 CSS:

.flex-box-centered {
    display:flex;
    width:200px;
    height:200px;
    justify-content:center;
    align-items:center;
    background:#fff;
}

.flex-img {
    display:flex;
    width:100%;
    height:auto;
}

And here's a jsfiddle illustrating this concept

这是一个说明这个概念jsfiddle

回答by Filip Koblański

When you have loaded image in $imageas Intervention\Image\Imageyou can easily do it like this:

当您加载图像时$imageIntervention\Image\Image您可以像这样轻松地做到这一点:

// $height and $width are your max values
if ($image->width < $image->height) {
    $image->resize(null, $height, true, false);
} else {
    $image->resize($width, null, true, false);
}

which will resize your image in the right way.

这将以正确的方式调整您的图像大小。

回答by James Norman

well, yes it is better option. Since Image Intervention uses Imagick in the background. I would recommend you to use Image Intervention. You can also compress images when you saving them.

嗯,是的,这是更好的选择。由于图像干预在后台使用 Imagick。我建议您使用图像干预。您还可以在保存图像时压缩图像。

// open and resize an image file
$img = Image::make('public/foo.jpg')->resize(300, 200);

// save file as jpg with medium quality
$img->save('public/bar.jpg', 60); 

image quality ranges from 0 (poor quality, small file) to 100 (best quality, big file).

图像质量范围从 0(质量差,文件小)到 100(质量最好,文件大)。

回答by Tushar Ahire

composer require intervention/image

作曲家需要干预/图像

// config/app.php

// 配置/app.php

return [
    ......
    $provides => [
        ......
        ......,
        'Intervention\Image\ImageServiceProvider'
    ],
    $aliases => [
        .....
        .....,
        'Image' => 'Intervention\Image\Facades\Image'
    ]
]

/* Controller Code

/* 控制器代码

    /*With Image Compression*/

            $image = $request->file('profile_image');
            $fileExtension   = strtolower($image->getClientOriginalExtension()); 
            $file_name       = sha1(uniqid().$image.uniqid()).'.'.$fileExtension;
            $destinationPath = 'uploads/profile_image/';
                $img = Image::make($image->getRealPath());
                $img->resize(140, 140, function ($constraint) {
                $constraint->aspectRatio();
            })->save($destinationPath.$file_name);

    /*End With Image Compression*/