【问题标题】:PHP Intervention Image Resize image to fit shortest side to aspect ratioPHP 干预图像调整图像大小以适应最短边长宽比
【发布时间】:2016-09-20 22:27:31
【问题描述】:

如何使用Intervention Image 调整图像大小,保持纵横比,但使图像的最短边符合所需的调整大小比例。

例如调整大小以适合 100x100 的 800x400 图像将调整为 200x100

我试过了:

$image->resize($width, $height, function ($constraint) {
    $constraint->aspectRatio();
});

但它会调整最长边的大小以适合(例如 100x50)。

【问题讨论】:

  • 我很难理解您需要什么。能否请您更清楚一点:我有这个,但我需要这个。
  • @PedroLobito 如果我 resize(100, 100) 将 800x400 图像调整为 100x50。我希望它调整为 200x100
  • 明白。我去看看

标签: php image-processing image-resizing


【解决方案1】:

设置宽度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);

【讨论】:

  • 佩德罗,重量应该是宽度。另外,您可以考虑添加“$constraint->upsize();”进入函数以防止升迁。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-20
  • 2017-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-05
相关资源
最近更新 更多