在 Laravel 中使用干预图像在保持原始比例的同时调整最大宽度/高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40467909/
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
Resizing with a max width/height while keeping the original ratio using Intervention Image in Laravel
提问by Felix Maxime
I want to constrain an image only if they exceed eithermaximum dimensions while keeping the original ratio intact.
我只想在图像超过任一最大尺寸时限制图像,同时保持原始比例不变。
So let's say my parameters are a max height and width of 600.
所以假设我的参数是最大高度和宽度 600。
An image of 1000x1000 would become 600x600, simple enough.
1000x1000 的图像将变成 600x600,很简单。
An image of 2000x1000 would become 600x300. This would mean that the highest of the two values becomes 600, while the other gets constrained proportionally.
2000x1000 的图像将变为 600x300。这意味着两个值中的最高值变为 600,而另一个值按比例受到约束。
Something like this
像这样的东西
$image->resize(600, 600, function ($constraint) {
$constraint->aspectRatio();
});
What would be the best way to go about this?
解决这个问题的最佳方法是什么?
EDIT:
编辑:
As per the comments, I tried this:
根据评论,我试过这个:
$medium = Image::make($file);
$medium->resize(null, 500, function ($constraint) {
$constraint->aspectRatio();
});
$medium->resize(500, null, function ($constraint) {
$constraint->aspectRatio();
});
$medium->save( public_path('/uploads/artistUploads/medium-' . $filename , 90) );
This does not work. Only the first resize is applied, which in this case is width.
这不起作用。仅应用第一个调整大小,在本例中为宽度。
HOWEVER, turns out the original code does work. I simply assumed it wouldn't, but it does.
然而,原来的代码确实有效。我只是假设它不会,但确实如此。
回答by Saumya Rastogi
As according to the Image Intervention Docs, you can do this in 3 simple ways
根据Image Intervention Docs,您可以通过 3 种简单的方式做到这一点
// resize the image to a width of 300 and constraint aspect ratio (auto height)
$img->resize(300, null, function ($constraint) {
$constraint->aspectRatio();
});
// resize the image to a height of 200 and constraint aspect ratio (auto width)
$img->resize(null, 200, function ($constraint) {
$constraint->aspectRatio();
});
// prevent possible upsizing
$img->resize(null, 400, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
Hope this helps...
希望这可以帮助...
回答by Chris Palmer Breuer
I know I am somewhat late to the race, but I have the answer you are looking for:
我知道我的比赛有点晚了,但我有你正在寻找的答案:
$width = 600; // your max width
$height = 600; // your max height
$img = IMG::make($uploaded_file);
$img->height() > $img->width() ? $width=null : $height=null;
$img->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
});
An image of 1000x1000 would become 600x600.
An image of 2000x1000 would become 600x300. This would mean that the highest of the two values becomes 600, while the other gets constrained proportionally.
1000x1000 的图像将变为 600x600。
2000x1000 的图像将变为 600x300。这意味着两个值中的最高值变为 600,而另一个值按比例受到约束。
This is what this code does. Hope I can help someone.
这就是这段代码的作用。希望我能帮助别人。
回答by Alexey Mezenin
You can use widen()
and heighten()
methods.
您可以使用widen()
和heighten()
方法。
widen()
:
widen()
:
Resizes the current image to new width, constraining aspect ratio. Pass an optional Closure callback as third parameter, to apply additional constraints like preventing possible upsizing.
将当前图像调整为新的宽度,限制纵横比。传递一个可选的闭包回调作为第三个参数,以应用额外的约束,比如防止可能的放大。
heighten()
:
heighten()
:
Resizes the current image to new height, constraining aspect ratio. Pass an optional Closure callback as third parameter, to apply additional constraints like preventing possible upsizing.
将当前图像调整为新高度,限制纵横比。传递一个可选的闭包回调作为第三个参数,以应用额外的约束,比如防止可能的放大。
Or you could use aspectRatio()
constraint. Examples from resize()
documentation:
或者你可以使用aspectRatio()
约束。resize()
文档中的示例:
// resize the image to a width of 300 and constrain aspect ratio (auto height)
$img->resize(300, null, function ($constraint) {
$constraint->aspectRatio();
});
// resize the image to a height of 200 and constrain aspect ratio (auto width)
$img->resize(null, 200, function ($constraint) {
$constraint->aspectRatio();
});
回答by catalin87
This is my template for doing similar work
这是我做类似工作的模板
<?php
namespace App\ImageSize;
use Intervention\Image\Image;
use Intervention\Image\Filters\FilterInterface;
class Large implements FilterInterface
{
public function applyFilter(Image $image)
{
$w = $image->width();
$h = $image->height();
if($w > $h) {
$image->resize(1000, null, function ($constraint) {
$constraint->aspectRatio();
});
} else {
$image->resize(null, 1000, function ($constraint) {
$constraint->aspectRatio();
});
}
return $image;
}
}
回答by Kriss Robert
So I was building a image preview component and I found out that if you set the max-width, max-height to the max values, then set the width,height to auto, the image aspect ratio stays intact.
https://codepen.io/kriss-robert/pen/aaNaZR?editors=1100
所以我正在构建一个图像预览组件,我发现如果将 max-width、max-height 设置为最大值,然后将 width、height 设置为 auto,图像纵横比将保持不变。
https://codepen.io/kriss-robert/pen/aaNaZR?editors=1100
max-width: 100%;
max-height: 100%;
width: auto;
height: auto;
Hope that this is of any help to anyone :D
希望这对任何人都有帮助:D