sparwelt/imgix-bundle

在 Symfony 和 Twig 中生成和转换 URL 以及响应式图片

安装次数: 4,474

依赖者: 0

建议者: 0

安全性: 0

星标: 1

关注者: 3

分支: 0

类型:symfony-bundle

0.5.0 2019-12-18 09:42 UTC

README

Build Status Coverage Status

Symfony 的 Imgix Bundle

安装

composer require sparwelt/imgix-bundle

基本配置

sparwelt_imgix:
  cdn_configurations:
    my_cdn:
        cdn_domains: ['my.imgix.net']
        sign_key: '12345'

  image_filters:
    my_basic_filter:
        src: {w: 30, h: 60}

基本用法

{# url generation #}
{{ imgix_url('/dir/test.png', {w: 100, h: 200}) }}
{# "https://my.imgix.net/dir/test.png?w=100&h=200" #}

{# image generation #}
{{ imgix_image('/dir/test.png', {src: {w: 100, h: 200}}) }};
{# <img src="https://my.imgix.net/dir/test.png?w=100&h=200"> #}

{# html conversion #}
{{ imgix_html('<li><img src="/test.png"><\li><li><img src="/test2.png">', {src: {w: 100, h:  200}}) }}
{# <li><img src="https://my.imgix.net/test.png?h=200&w=100"><\li><li><img src="https://my.imgix.net/test2.png?h=200&w=100"> #}

响应式用法

{# image generation #}
{{ imgix_image('/dir/test.png', {
            src: {h: 150, w: 300},
            srcset: {
                100w: {h: 300, w: 600},
                500w: {h: 600, w: 900},
            },
            sizes: '(min-width: 900px) 1000px, (max-width: 900px)'
}) }}
{# <img src="https://test.imgix.net/dir/test.png?h=150&w=300"
        srcset="https://test.imgix.net/dir/test.png?h=300&w=600 100w, https://test.imgix.net/dir/test.png?h=600&w=900 500w"
        sizes="(min-width: 900px) 1000px, (max-width: 900px)"> #}

{# image generation #}
  <img srcset="{{imgix_attr('test.png', {
    '2x': {w: 400, h: 200}
    '3x': {w: 600, h: 300}
  }) }}">

{# <img srcset="https://test.imgix.net/test.png?h=200&w=400 2x, https://test.imgix.net/test.png?h=300&w=600 3x"> #}

懒加载

{{ imgix_image('/dir/test.png', {
    'src': {h: 30, w: 60},
    'srcset': 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==',
    'data-srcset': {
        '100w': {'h': 60, 'w': 120}
        '500w': {'h': 90, 'w': 180},
    },
    'data-sizes': 'auto',
    'class': 'lazyload',
}) }}
{# <img src="https://test.imgix.net/dir/test.png?h=30&w=60" 
       srcset="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
       data-srcset="https://test.imgix.net/dir/test.png?h=60&w=120 100w, https://test.imgix.net/dir/test.png?h=90&w=180 500w"
       data-sizes="auto" 
       class="lazyload">
#}

命名过滤器

为了避免每次使用都重复过滤器,可以一次性配置命名过滤器,并按名称调用

sparwelt_imgix:
  image_filters:
    my_basic_filter:
        src: {w: 30, h: 60}
    my_blur_lazyload_filter:
        src: {w: 30, h: 60}
        data-src: {h: 30, w: 60, blur: 1000}
        data-srcset:
            100w: {h: 60, w: 120}
            500w: {h: 90, w: 180}
        data-sizes: 'auto'
        class: 'lazyload'
{{ imgix_image('dir/test.png', 'my_basic_filter') }}
{# <img src="https://test.imgix.net/dir/test.png?h=30&w=60"> #}

{{ imgix_image('dir/test.png', 'my_blur_lazyload_filter') }}
{# <img src="https://test.imgix.net/dir/test.png?h=30&w=60" 
       data-src="https://test.imgix.net/dir/test.png?blur=1000h=30&w=60"
       data-srcset="https://test.imgix.net/dir/test.png?h=60&w=120 100w, https://test.imgix.net/dir/test.png?h=90&w=180 500w"
       data-sizes="auto" 
       class="lazyload">
#}

{{ imgix_url('dir/test.png', 'my_basic_filter.src') }}
{# https://test.imgix.net/test.png?h=30&w=60 #}

{{ imgix_url('dir/test.png', 'my_basic_filter.src', {q: 75}) }}
{# https://test.imgix.net/test.png?h=30&w=60&q=75 #}

{{ imgix_attr('dir/test.png', 'my_blur_lazyload_filter.data-srcset') }}
{# https://test.imgix.net/dir/test.png?h=60&w=120 100w, https://test.imgix.net/dir/test.png?h=90&w=180 500w #}

{{ imgix_html($html, 'my_blur_lazyload_filter') }}
{# ... replaces all images with responsive ones #}

多个 CDN 配置

图像路径将按照从上到下的顺序与配置进行评估,直到找到合适的匹配项。可以为同一配置指定多个域名(域名分片)。

sparwelt_imgix:
  cdn_configurations:
        # matches images with source domain 'mysite.com' (including subdomains)
        # AND path beginning with 'uploads/' OR 'media/'
        # Relative urls won't match
        source_domains_and_pattern:
            cdn_domains: ['my-cdn-1.imgix.net']
            source_domains: ['mysite.com']
            path_patterns: ['^[/]uploads/', '^[/]media/']

        # matches images with source domain exactly 'www3.mysite.com' OR 'www4.mysite.com'
        # Relative urls won't match
        source_sub_domain:
            cdn_domains: ['my-cdn-2.imgix.net']
            source_domains: ['www3.mysite.com', 'www4.mysite.com']

        # matches images with source domain 'mysite.com' (including subdomains)
        # Relative urls won't match
        source_domains:
            cdn_domains: ['my-cdn-3.imgix.net']
            source_domains: ['mysite.com']

        # matches images with source domain 'mysite.com' (including subdomains)
        # AND relative urls (because of the 'null')
        source_domains_and_null:
            cdn_domains: ['my-cdn-4.imgix.net']
            source_domains: ['mysite.com', null]

        # matches relative urls only, where path begins with 'uploads/'.
        # Absolute urls won't match.
        pattern:
            cdn_domains: ['my-cdn-5.imgix.net']
            path_patterns: ['^[/]pattern/']

        # matches relative urls only, where path begins with 'sign-key/'.
        # appends sign-key to the generated url (recommended)
        sign_key:
            cdn_domains: ['my-cdn-6.imgix.net']
            path_patterns: ['[^]/sign-key/']
            sign_key: '12345'

        # matches relative urls only, where path begins with 'shard-crc/'.
        # Will choose the cdn domains by the hash of the path (recommended)
        shard_crc:
            cdn_domains: ['my-cdn-7.imgix.net', 'my-cdn-8.imgix.net']
            path_patterns: ['^[/]shard-crc/']
            shard_strategy: 'crc' #default

        # matches relative urls only, where path begins with 'shard-cycle/'.
        # Will rotate between the 2 cdn domains (increase costs)
        shard_cycle:
            cdn_domains: ['my-cdn-9.imgix.net', 'my-cdn-10.imgix.net']
            path_patterns: ['^[/]shard-cycle/']
            shard_strategy: 'cycle'

        # default parameters can be added, useful for cache bursting or automatic formatting
        default_parameters:
            cdn_domains: ['my-cdn-11.imgix.net']
            path_patterns: ['^[/]shard-cycle/']
            default_query_params:
              cb: '1234'
              auto: 'quality'

        # disable parameters generation
        # (useful for development/testing environments)
        bypass_dev:
            cdn_domains: ['my-dev-domain.local']
            source_domains: ['my-dev-domain.local']
            generate_filter_params: false,
            use_ssl: false

        # matches all relative urls
        my_default:
            cdn_domains: ['my-cdn-12.imgix.net']