使用最大 width/height 调整大小,同时使用 Laravel 中的干预图像保持原始比例

Resizing with a max width/height while keeping the original ratio using Intervention Image in Laravel

我只想在图像超过 任一 最大尺寸时限制图像,同时保持原始比例不变。

假设我的参数是最大高度和宽度 600。

一张 1000x1000 的图片会变成 600x600,很简单。

一张 2000x1000 的图片会变成 600x300。这意味着两个值中的最大值变为 600,而另一个按比例受到限制。

像这样

            $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) );    

这不起作用。仅应用第一次调整大小,在本例中为宽度。

然而,原来的代码确实有效。我只是假设它不会,但确实如此。

您可以使用 widen() and heighten() 方法。

widen():

Resizes the current image to new width, constraining aspect ratio. Pass an optional Closure callback as third parameter, to apply additional constraints like preventing possible upsizing.

heighten():

Resizes the current image to new height, constraining aspect ratio. Pass an optional Closure callback as third parameter, to apply additional constraints like preventing possible upsizing.

或者您可以使用 aspectRatio() 约束。 resize() 文档中的示例:

// 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();
});

根据 Image Intervention Docs,您可以通过 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();
});

An image of 1000x1000 would become 600x600.

An image of 2000x1000 would become 600x300. This would mean that the highest of the two values becomes 600, while the other gets constrained proportionally.

这就是这段代码的作用。希望我能帮助到别人。

这是我做类似工作的模板

<?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;

    }
}