具有最大的宽度/高度尺寸调整,同时使用干涉图像在Laravel保持原来的比率

问题描述 投票:5回答:5

我想,只有当他们超过任一最大尺寸,同时保持原有比例不变约束的图像。

所以我们可以说我的参数是最大高度和宽度600。

1000×1000的图像将成为600×600,很简单。

的2000x1000的图像将成为600×300。这意味着最高的两个值变为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) );    

这是行不通的。仅第一调整大小被施加,在这种情况下是宽度。

然而,事实证明原来的代码确实工作。我只是认为它不会,但它确实。

php laravel image-manipulation
5个回答
10
投票

由于根据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();
});

希望这可以帮助...


6
投票

我知道我有点晚了比赛,但我有你正在寻找的答案:

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

1000×1000的图像将成为600×600。

的2000x1000的图像将成为600×300。这意味着最高的两个值变为600,而其他被限制比例。

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


1
投票

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

widen()

调整大小的当前图像,以新的宽度,限制纵横比。通过一个可选的关闭回调作为第三个参数,运用像防止可能升迁额外的约束。

heighten()

调整大小的当前图像,以新的高度,限制纵横比。通过一个可选的关闭回调作为第三个参数,运用像防止可能升迁额外的约束。

或者你可以使用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();
});

0
投票

所以我建立一个图像预览组件,我发现,如果你设置的最大宽度,最大高度来的最高值,然后设置宽度,高度自动,图像纵横比保持不变。 https://codepen.io/kriss-robert/pen/aaNaZR?editors=1100

max-width: 100%;
max-height: 100%;
width: auto;
height: auto;

希望这是任何人任何帮助:d


0
投票

这是我做类似的工作模板

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

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.