laravel 图片干预是压缩用户上传图片的好方法吗?
Is laravel image intervention a good way to compress user uploaded images?
我正在检查这个 laravel 库来压缩用户上传的图像。http://image.intervention.io/
我想知道这对用户上传的图片(个人资料图片)是否是个好主意?如果用户上传一张尺寸为 1400x600 的图片,它会被调整为 200x200 怎么办?会不会是拉伸图?
是的,它会被拉伸。你想尝试 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);
干预非常适合处理这个问题。这是一个快速的代码片段,可以让您朝着正确的方向前进:
$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);
}
}
在上面我们循环遍历了一些我们希望将图像调整到的宽度数组。注意我们在下面声明的 $constraints
。 aspectRatio()
确保图像永远不会失去它的纵横比。 ->upsize()
防止图像被放大和丢失分辨率。
您的要求 - 将 1400x600 图片大小调整为 200x200
不合理。
相反,您应该只调整到一维,并确保您的 HTML 可以解决其余问题。例如,在上面我们只观察到 widths
。所以我们假设在您的 $this->sizes()
数组中,其中一个是 200。但是图像的比例不同,所以它没有生成 200x200,而是生成了 200x150。在 HTML 中使用 flexbox 超级轻松地处理:
<div class="flex-box-centered">
<img class="flex-img" src="..."/>
</div>
和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;
}
当您在 $image
中以 Intervention\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);
}
这将以正确的方式调整图像的大小。
嗯,是的,这是更好的选择。由于 Image Intervention 在后台使用 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);
图像质量范围从 0(质量差,文件小)到 100(质量最好,文件大)。
作曲家要求intervention/image
// config/app.php
return [
......
$provides => [
......
......,
'Intervention\Image\ImageServiceProvider'
],
$aliases => [
.....
.....,
'Image' => 'Intervention\Image\Facades\Image'
]
]
/* 控制器代码
/*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*/
我得到了原始图片,然后我将尺寸调整为 50%,以保持更好的比例。所以在用户看来还不错。
$imageDimensions = getimagesize($image);
$width = $imageDimensions[0];
$height = $imageDimensions[1];
$new_width = $imageDimensions[0] * 0.5;
$new_height = ceil($height * ($new_width / $width));
$image = Image::make($image)->resize($new_width, $new_height);
我首先得到正常尺寸,然后进行计算以适当调整它的大小。
我正在检查这个 laravel 库来压缩用户上传的图像。http://image.intervention.io/
我想知道这对用户上传的图片(个人资料图片)是否是个好主意?如果用户上传一张尺寸为 1400x600 的图片,它会被调整为 200x200 怎么办?会不会是拉伸图?
是的,它会被拉伸。你想尝试 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);
干预非常适合处理这个问题。这是一个快速的代码片段,可以让您朝着正确的方向前进:
$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);
}
}
在上面我们循环遍历了一些我们希望将图像调整到的宽度数组。注意我们在下面声明的 $constraints
。 aspectRatio()
确保图像永远不会失去它的纵横比。 ->upsize()
防止图像被放大和丢失分辨率。
您的要求 - 将 1400x600 图片大小调整为 200x200
不合理。
相反,您应该只调整到一维,并确保您的 HTML 可以解决其余问题。例如,在上面我们只观察到 widths
。所以我们假设在您的 $this->sizes()
数组中,其中一个是 200。但是图像的比例不同,所以它没有生成 200x200,而是生成了 200x150。在 HTML 中使用 flexbox 超级轻松地处理:
<div class="flex-box-centered">
<img class="flex-img" src="..."/>
</div>
和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;
}
当您在 $image
中以 Intervention\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);
}
这将以正确的方式调整图像的大小。
嗯,是的,这是更好的选择。由于 Image Intervention 在后台使用 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);
图像质量范围从 0(质量差,文件小)到 100(质量最好,文件大)。
作曲家要求intervention/image
// config/app.php
return [
......
$provides => [
......
......,
'Intervention\Image\ImageServiceProvider'
],
$aliases => [
.....
.....,
'Image' => 'Intervention\Image\Facades\Image'
]
]
/* 控制器代码
/*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*/
我得到了原始图片,然后我将尺寸调整为 50%,以保持更好的比例。所以在用户看来还不错。
$imageDimensions = getimagesize($image);
$width = $imageDimensions[0];
$height = $imageDimensions[1];
$new_width = $imageDimensions[0] * 0.5;
$new_height = ceil($height * ($new_width / $width));
$image = Image::make($image)->resize($new_width, $new_height);
我首先得到正常尺寸,然后进行计算以适当调整它的大小。