2017 © Pedro Peláez
 

craft-plugin craft3-imgix

Use Imgix with Craft

image

superbig/craft3-imgix

Use Imgix with Craft

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 6 Versions
  • 119 % Grown

The README.md

imgix plugin for Craft CMS 3.x

Use imgix with Craft, (*1)

Screenshot, (*2)


Requirements

This plugin requires Craft CMS 3.0.0-beta.23 or later., (*3)

Installation

To install the plugin, follow these instructions., (*4)

  1. Open your terminal and go to your Craft project:, (*5)

    cd /path/to/project
    
  2. Then tell Composer to load the plugin:, (*6)

    composer require superbig/craft3-imgix
    
  3. In the Control Panel, go to Settings → Plugins and click the “Install” button for imgix., (*7)

Configuring imgix

Copy config.php into Crafts config folder and rename it to imgix.php., (*8)

Then map your Asset Source handle to your imgix domain, according to the example., (*9)

This plugin will lookup the Asset image's source handle, and figure out which imgix domain to use. If a URL string is passed, it will use the first domain in the config file., (*10)

<?php
   return [
       // imgix API key
       'apiKey'         => '',

       // Volume handles mapped to imgix domains
       'imgixDomains'   => [],

       // imgix signed URLs token
       'imgixSignedToken' => '',

       // Lazy load attribute prefix
       'lazyLoadPrefix' => '',
   ];

Using imgix

{% set transforms = [
    {
        width: 400,
        height: 300
    },
    {
        width: 940,
        height: 520
    },
    {
        width: 1400,
    },
] %}

{% set defaultOptions = {
    sharp: 10
} %}

{% set firstImage = craft.imgix.transformImage( asset, { width: 400, height: 350 }) %}
{% set secondImage = craft.imgix.transformImage( asset, transforms) %}
{% set thirdImage = craft.imgix.transformImage( asset, { width: 1920, ratio: 16/9}) %}
{% set fourthImage = craft.imgix.transformImage( asset, transforms, defaultOptions) }

{# Image tag #}
{{ firstImage.img() }}

{# Get url for the first image #}
{{ firstImage.getUrl() }}

{# Image tag w/ srcset + tag attributes #}
{{ secondImage.srcset({ width: 700, alt: 'your alt text here' }) }}

{# Image tag w/ srcset + default options for each transform #}
{{ fourthImage.srcset( {} ) }}

{# Image tag w/ lazyload #}
{{ firstImage.img({ lazyLoad: true }) }}

{# Image tag w/ srcset + lazyLoad #}
{{ secondImage.srcset({ lazyLoad: true }) }}

{# See transformed results #}
{{ dump(secondImage.transformed) }}

To use with Element API, you can call the service directly:, (*11)

<?php

use craft\elements\Entry;
use craft\helpers\UrlHelper;
use superbig\imgix\Imgix;

return [
    'endpoints' => [
        'news.json' => [
            'elementType' => Entry::class,
            'criteria' => ['section' => 'news'],
            'transformer' => function(Entry $entry) {
                $asset = $entry->featuredImage->one();
                $featuredImage = Imgix::$plugin->imgixService->transformImage( $asset, [ 'width' => 400, 'height' => 350 ]);

                return [
                    'title' => $entry->title,
                    'url' => $entry->url,
                    'jsonUrl' => UrlHelper::url("news/{$entry->id}.json"),
                    'summary' => $entry->summary,
                    'featuredImage' => $featuredImage,
                ];
            },
        ],
    ]
];

Lazy Loading

To replace src and srcset with data-src and data-srcset for javascript-based lazy loading, add the lazyLoad attribute to transformImage()., (*12)

If you need to prefix with something other than data-, you can set the configuration value lazyLoadPrefix in craft/config/imgix.php., (*13)

Alternatively, you may use the native loading attribute loading="lazy" on your image tag as in this example: {{ image.srcset({ loading: 'lazy' }) }}., (*14)

Roadmap

  • Look into improving srcset/API
  • Look into built-in image editor and focal points
  • Improve docs

Brought to you by Superbig, (*15)

The Versions

21/07 2018

dev-master

9999999-dev

Use Imgix with Craft

  Sources   Download

MIT

The Requires

 

cms imgix craftcms craft-plugin craft

21/07 2018

2.0.4

2.0.4.0

Use Imgix with Craft

  Sources   Download

MIT

The Requires

 

cms imgix craftcms craft-plugin craft

25/01 2018

2.0.3

2.0.3.0

Use Imgix with Craft

  Sources   Download

MIT

The Requires

 

cms imgix craftcms craft-plugin craft

08/12 2017

2.0.2

2.0.2.0

Use Imgix with Craft

  Sources   Download

MIT

The Requires

 

cms imgix craftcms craft-plugin craft

04/12 2017

2.0.1

2.0.1.0

Use Imgix with Craft

  Sources   Download

MIT

The Requires

 

cms imgix craftcms craft-plugin craft

30/10 2017

2.0.0

2.0.0.0

Use Imgix with Craft

  Sources   Download

MIT

The Requires

 

cms imgix craftcms craft-plugin craft