【问题标题】:How to create and use image template with intervention image and Laravel 5.2如何使用干预图像和 Laravel 5.2 创建和使用图像模板
【发布时间】:2016-05-24 00:19:34
【问题描述】:

我在我的 Laravel 5.2 应用程序中使用 Intervention Image library 以及 Image Cache plugin

我一直在使用预定义的模板,没有出现这样的问题:

{{ route('imagecache', ['template' => 'medium', 'filename' => 'image.jpg']) }}"

我在文档中看到,除了默认尺寸小、中和大之外,您还可以创建图像过滤器来创建自定义操作并将它们定义为配置文件中的模板,这样我就可以传递我的模板而不是中姓名。文档引用图像过滤器作为一种方法来做到这一点,但它对于具体如何做到这一点有点粗略。有人知道你是怎么做到的吗?

【问题讨论】:

    标签: laravel laravel-5.2 intervention


    【解决方案1】:

    config/imagecache.php里面有一个templates键,你可以在这里添加自己的。

    例如:

    'templates' => [
        // ...
       'x-large' => 'App\Filters\ExtraLarge',
       // ...
    ],
    

    那么你只需要创建类App\Fitlers\ExtraLarge

    applyFilter() 方法中,您可以根据documentation 调用$image 属性上的任何方法。

    <?php
    
    namespace App\Filters;
    
    use Intervention\Image\Image;
    use Intervention\Image\Filters\FilterInterface;
    
    class ExtraLarge implements FilterInterface
    {
        public function applyFilter(Image $image)
        {
            return $image->fit(1300, 1000);
        }
    }
    

    然后在route helper 中将模板的值设置为x-large

    {{ route('imagecache', ['template' => 'x-large', 'filename' => 'image.jpg']) }}
    

    【讨论】:

      猜你喜欢
      • 2017-06-13
      • 2020-03-19
      • 2017-01-28
      • 1970-01-01
      • 2016-12-19
      • 2023-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多