【问题标题】:Maintain aspect ratio of resized images same as the original image ratio保持调整后图像的纵横比与原始图像比例相同
【发布时间】:2017-11-04 10:32:17
【问题描述】:

我想在 Laravel 5.3 中通过 Intervention\Image 调整我的大小。 使用以下代码:

        $resized = Image::make($image)->resize(640, null, function ($constraint) {
                $constraint->aspectRatio(); })->stream();
        $resized_thumb = Image::make($image)->resize(320, 213)->stream();

我得到这样的图像: http://clasifire.com/listing?category=1(点击第一张图片查看详情会发现其实高度更高)

但是这样的图像会丢失纵横比。 例如,我想要的是 craigslist 上的这个列表:https://sfbay.craigslist.org/d/antiques/search/ata,(向下滚动 3-4 行)

所以我不希望图像被拉伸,而是应该保持纵横比,如果需要,在宽度/高度上留下空白空间。

【问题讨论】:

    标签: php laravel laravel-5 laravel-5.3 intervention


    【解决方案1】:

    你应该添加另一个约束:

    $constraint->upsize();
    

    之后

    $constraint->aspectRatio();
    

    防止图像太小时放大。

    【讨论】:

    • @RameshPareek 为什么不试试看呢?
    • 谢谢。我会尽快通知您。
    • 好的。测试它,不工作。可能是我的这一行正在创建问题`$resized_thumb = Image::make($image)->resize(320, 213)->stream();`,因为我正在指定尺寸。如何只指定宽度并让它决定高度?
    【解决方案2】:

    您可以考虑使用像 Glide 这样的包来动态生成图像(裁剪、大小、效果、水印、缓存等)并保持原始图像不变。 http://glide.thephpleague.com/

    【讨论】:

    • 我的问题是关于干预的。无论如何感谢您推荐一个新的图书馆。
    • Glide 也使用了干预。它基于它。
    • 太棒了!我觉得开悟了
    【解决方案3】:

    你不应该同时指定 with 和 height !仅使用一维和 widen()heighten() 函数之一:

    按高度:

    $resized_thumb = Image::make($image)->heighten(320)->stream();
    

    按宽度:

    $resized_thumb = Image::make($image)->widen(213)->stream();
    

    【讨论】:

      猜你喜欢
      • 2013-06-26
      • 2012-04-15
      • 2011-04-27
      • 1970-01-01
      • 2012-05-01
      • 2012-11-15
      相关资源
      最近更新 更多