Php 使用Laravel中的干预图像,以最大宽度/高度调整大小,同时保持原始比率
我希望仅当图像超过最大尺寸时才对其进行约束,同时保持原始比例不变 假设我的参数最大高度和宽度为600 1000x1000的图像将变成600x600,非常简单 2000x1000的图像将变成600x300。这意味着两个值中的最高值将变为600,而另一个值将按比例受到约束 像这样的Php 使用Laravel中的干预图像,以最大宽度/高度调整大小,同时保持原始比率,php,laravel,image-manipulation,Php,Laravel,Image Manipulation,我希望仅当图像超过最大尺寸时才对其进行约束,同时保持原始比例不变 假设我的参数最大高度和宽度为600 1000x1000的图像将变成600x600,非常简单 2000x1000的图像将变成600x300。这意味着两个值中的最高值将变为600,而另一个值将按比例受到约束 像这样的 $image->resize(600, 600, function ($constraint) { $constraint->aspectRatio()
$image->resize(600, 600, function ($constraint) {
$constraint->aspectRatio();
});
最好的办法是什么
编辑:
根据评论,我尝试了以下方法:
$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) );
这是行不通的。仅应用第一个调整大小,在本例中为宽度
然而,原来的代码确实有效。我只是假设它不会,但它会。你可以使用和方法
加宽()
:
将当前图像调整为新宽度,并约束纵横比。将可选的闭包回调作为第三个参数传递,以应用其他约束,如防止可能的升迁
加高()
:
将当前图像调整为新高度,并约束纵横比。将可选的闭包回调作为第三个参数传递,以应用其他约束,如防止可能的升迁
或者可以使用aspectRatio()
约束。文件中的示例:
// 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();
});
根据,您可以用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();
});
希望这能有所帮助……我知道我比赛有些晚了,但我有你想要的答案:
$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();
});
1000x1000的图像将变成600x600
2000x1000的图像将变成600x300。这意味着
两个值中的最高值变为600,而另一个值变为600
按比例约束
这就是代码的作用。希望我能帮助别人。所以我正在构建一个图像预览组件,我发现如果将“最大宽度”、“最大高度”设置为“最大值”,然后将“宽度”、“高度”设置为“自动”,图像纵横比将保持不变
希望这对任何人都有帮助:D这是我做类似工作的模板
<?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;
}
}
为什么要加宽和加高?“我在试着缩小规模。@FelixMaxime,你为什么认为这是为了扩大规模?)因为字典里对这些术语的定义。也许它们在干预中意味着其他的东西。@FelixMaxime,这些方法正是您所需要的,比如带有约束的resize()
。我的意思是,不是真的。你是说我应该先限制高度然后限制宽度?在最大值为600的2000x1000场景中,这会使其达到600x300吗?@FelixMaxime Yup在定义宽度的情况下,宽度参数被破坏,解决方案将是600x300,而如果高度被定义且宽度为null,则高度参数被破坏为1200x600。这不起作用。它只做第一个(在本例中是宽度)。感谢您发布您的发现,它帮助我在这个“解决方案”中脱颖而出,例如:如果您上载10000px 10000px图像-用户需要下载很多MB;)我没想到。。这是一个问题:))硬编码srcset会有所帮助,但您确实有一个优点。再说一次,我不认为一个图像会大于4k。这与图像大小调整本身无关。有时,我们使用图像大小调整用于web以外的其他目的。我正在使用它创建PDF文件,其中包含用户上传的图像,因此CSS ish解决方案对我来说不起作用!真正理解这个问题的人给出了很好的回答。我已经能够进一步简化它:$resized=($img->height()>$img->width()?$img->health($max\u height):$img->width($max\u width))代码>
<?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;
}
}