PHP干预图像:调整图像大小以适合最短边到纵横比。

3
我该如何使用Intervention Image调整图像大小并保持长宽比,但使图像的最短边适应所需的调整比例。
例如,将800x400的图像调整为100x100,将调整为200x100。
我尝试过这个:
$image->resize($width, $height, function ($constraint) {
    $constraint->aspectRatio();
});

但它会调整最长的一边以适应(例如,100x50)。

我觉得很难理解你需要什么。你能否更清楚地说明一下:我有这个,但我需要这个。 - Pedro Lobito
@PedroLobito 如果我对一个800x400的图像执行resize(100, 100)操作,它会被调整为100x50。我希望它可以调整为200x100。 - Petah
明白了,我会看一下。 - Pedro Lobito
1个回答

8

宽度设置为null

$height = 100;
$image = Image::make('800x400.jpg')->resize(null, $height, function ($constraint) {
    $constraint->aspectRatio();
});
$image->save('200X100.jpg', 60);

以编程的方式来说,只需找到较大的那一侧并将其设置为 null,即:
$width = 100;
$height = 100;
$image = Image::make('400x800.png');
$image->width() > $image->height() ? $width=null : $height=null;
$image->resize($width, $height, function ($constraint) {
    $constraint->aspectRatio();
});
$image->save('100x200.jpg', 60);

Pedro,weight 应该改为 width。此外,您可以考虑在函数中添加 "$constraint->upsize();" 以防止放大。 - Peter Matisko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接