liip_imagine 是否将 jpeg/jpg/png 文件转换为 webp? - symfony php 7.4.9

Does liip_imagine convert jpeg/jpg/png file to webp? - symfony php 7.4.9

所以我想将项目中的现有图像从 jpeg/jpg/png 转换为 .webp 扩展名。我已经读过使用 imagine for it https://github.com/liip/LiipImagineBundle/pull/1307,但是 它不起作用,我的 liip_imagine 看起来像这样:

liip_imagine:
    driver: "imagick"
    default_filter_set_settings:
        format: webp
    webp:
        generate: true
        quality: 80
        cache: ~
        data_loader: ~
        post_processors: [ ]
    resolvers:
        profile_photos:
            web_path:
                # use %kernel.project_dir%/web for Symfony prior to 4.0.0
                web_root: "%kernel.project_dir%/web"
                cache_prefix: "cache"
    filter_sets:
        cache: ~
        my_thumbnail:
            quality: 80
            filters:
                thumbnail: { mode: inset }
                post_processors:
                    optipng:
                        strip_all: true
                        level: 3
        my_thumbnail_webp:
            format: webp
            quality: 80
            filters:
                thumbnail: { mode: inset }
                post_processors:
                    optipng:
                        strip_all: true
                        level: 3

然后我有这个:

<picture>
  <source srcset="{{ '/path/to/image.png' | imagine_filter('my_thumbnail_webp') }}" type="image/webp">
  <source srcset="{{ '/path/to/image.png' | imagine_filter('my_thumbnail') }}" type="image/png"> 
  <img src="{{ '/path/to/image.png' | imagine_filter('my_thumbnail') }}">
</picture>

但是没用!我该如何解决我的问题? 在网站上我仍然有 /path/to/image.png - 所以它不会改变任何东西,我想要 /path/to/image.png.webp 我应该将我项目中的每个 img 文件转换为 webp 并再次上传还是什么?

你就快完成了!您错过了路径中的 asset 方法:

<img src="{{ asset('/relative/path/to/image.jpg') | imagine_filter('my_thumb') }}" />

所以应该是:

<picture>
  <source srcset="{{ asset'/path/to/image.png') | imagine_filter('my_thumbnail_webp') }}" type="image/webp">
  <source srcset="{{ asset('/path/to/image.png') | imagine_filter('my_thumbnail') }}" type="image/png"> 
  <img src="{{ asset('/path/to/image.png') | imagine_filter('my_thumbnail') }}">
</picture>

如果您需要在png/jpg中创建webp文件,请在liip_imagine.yaml中添加:

liip_imagine:
  webp:
    generate: true

这不需要更改网络服务器即可使用它们