PHP 干预图像调整图像大小以适应最短边长宽比

标签 php image-processing image-resizing

如何使用 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/

相关文章:

PHP HTML 表单编码+签名?

c++ - 将 alpha channel 添加到 opencv Mat

ios - 检测 UIImage 中的颜色并保存 CGPoints

c++ - 如何将 C++ 结构数组传递给 CUDA 设备?

javascript - 调整 html5 Canvas 中图像的大小

php - 如何在谷歌图表功能中显示来自mysql的数据?

php - 文本输出中的斜杠

php - 如何在 CI 和 Laravel 之间设置路由

从照片库快速调整图像(ALAssets)的缩略图

ios - 无论分辨率如何,自动布局缩放总是会产生相同的结果吗?