如何使用 Intervention Image 调整图像大小保持宽高比,但使图像的最短边适合所需的调整大小比。
例如调整大小以适合 100x100 的 800x400 图像将调整为 200x100
我试过这个:
$image->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
});
但它会调整最长边的大小以适应(例如 100x50)。
最佳答案
设置宽度为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);
关于PHP 干预图像调整图像大小以适应最短边长宽比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37401185/