53

Product support

Get help in the plugin support forum.

Categories

Features

  • Responsive images
  • Automatic WebP conversion
  • Focuspoint
  • Inline SVG helper function

The auto-resize middleware turns this

<img width="500" src="/storage/app/media/image.jpg">

into this

<img width="500" src="/storage/app/media/image.jpg" srcset="/storage/temp/public/be7/4d6/0cc/image__400.jpg 400w, /storage/temp/public/be7/4d6/0cc/image__768.jpg 768w, /storage/temp/public/be7/4d6/0cc/image__1024.jpg 1024w" sizes="(max-width: 500px) 100vw, 500px">

It automatically creates resized copies of the image and serves the most fitting one to your visitor. All image copies are saved in your public temp path. Remote file systems are currently untested.

If the visitor's web browser supports the WebP image format, this plugin will automatically generate and serve highly optimized WebP images.

The images are generated on the first page load. Depending on the source image size this may take a few seconds. Subsequent page loads will be faster since the images are only resized once.

See the Documentation tab for the configuration and usage details.

Test results

I have tested this plugin on a page with 20 hd wallpapers from pixabay.

Viewport width Transferred file size
1920 px 21.8 MB
1024 px 3.1 MB
768 px 2.0 MB
400 px 0.8 MB

Bug reports

It is very likely that there will be bugs with some specific html markup. If you encounter such a bug, please report it.

Future plans

  • Exclude/Include-Filters
  • Maybe a component to enable the middleware only on some pages

Responsive images

Configuration

Three image sizes are created by default: 400, 768 and 1024 pixels.

You can change these values by changing the settings in the backend.

Alternative src and srcset attributes

If you want to use an alternative src attribute you can change this via the backend settings page.

This is useful if you are using a plugin like jQuery.lazyLoad where the image is initially linked via a data-original attribute.

If your plugin requires an alternative srcset attribute (like verlok/LazyLoad) this can also be specified via the backend settings.

Global class attributes

If you want to add a class to every processed image you can configure this via the backend settings.

This is useful if you want to add Bootstrap's img-responsive class to all images on your website.

Pre-generate images

You can use the php artisan responsive-images:generate command to pre-generate responsive images. The command uses October's pages.menuitem.* events to build a list of all available URLs and pre-generates all images used on these pages.

Test results

I have tested this plugin on a page with 20 hd wallpapers from pixabay.

Viewport width Transferred file size
1920 px 21.8 MB
1024 px 3.1 MB
768 px 2.0 MB
400 px 0.8 MB

Automatic WebP conversion

This plugin provides an option to automatically convert all images to the WebP image format if the visiting Browser signals support for it.

Be aware that each WebP image is created on-demand with the first page view that requests it. This might lead to slow page load times for your first visitors. To prevent this, warm up the image cache by visiting every page at least once (by using a linkchecker) or use the php artisan responsive-images:generate -v console command.

To make use of this feature, enable it via October's backend settings. If you are using Apache with .htaccess support, the plugin will serve WebP images to supported browsers automatically.

If you do not use Apache, you have to configure your server to do the following:

  1. Check if image/webp is present in the Accepts http header
  2. Check if the requested image ends in jp(e)g or png
  3. Check if the requested image + a .webp exists
  4. If it does, serve it
  5. If it does not, redirect the request to plugins/offline/responsiveimages/webp.php?path=${REQUEST_URI}

The webp.php helper script will generate any missing WebP images so they can be served directly on the next visit.

Apache + .htaccess

This is the default .htaccess configuration that gets applied automatically:

# Added by default! No need to add this manually
<ifModule mod_rewrite.c>
    RewriteEngine On

    # If the Browser supports WebP images, and the .webp file exists, use it.
    RewriteCond %{HTTP_ACCEPT} image/webp
    RewriteCond %{REQUEST_URI} \.(jpe?g|png)$
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.webp -f
    RewriteRule (.+)$ $1.webp [T=image/webp,E=accept:1]

    # If the Browser supports WebP images, and the .webp file does not exist, generate it.
    RewriteCond %{HTTP_ACCEPT} image/webp
    RewriteCond %{REQUEST_URI} \.(jpe?g|png)$
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.webp !-f
    RewriteRule (.+)$ %{DOCUMENT_ROOT}/plugins/offline/responsiveimages/webp.php?path=$1 [L]
</ifModule>

Other servers

We did not invest in the proper WebP detection configuration for other server software. Based on the .htaccess example above and the webp-detect repo you should be able to figure out what config is needed.

If you have a working example please add it via a PR to this README!

Focuspoint

This feature has two components to it:

Backend

In the backend, the file upload widget is extended with a simple focus point selector.

To enable this extension simply set focuspoint: true to any fileupload widget in your plugin's fields.yaml. This feature is off by default.

Once it is enabled you can click on an uploaded image to select the focus point.

fields:
    images:
        label: Images
        mode: image
        useCaption: true
        thumbOptions:
            mode: crop
            extension: auto
        span: full
        type: fileupload
        # Enable the focus point selector
        focuspoint: true

focuspoint-configform

Frontend

You can use the new focus method on any File model to get the source to a focus point image.

The focus method has the exact same API as the thumb method, you can specify a height, width and a mode.

<img src="{{ image.focus(200, 300, 'auto') }}" alt="">

This call will result in the following HTML:

<img src="/storage/temp/public/a9f/2bd/159/offline-focus_30_400_500_50_50_0_0_auto__400.jpg" 
     alt="" 
     class="focuspoint-image" 
     style="width: 100%; height: 100%; object-fit: cover; object-position: 30% 80%;">

You can disable the injection of the inline styles via the plugin's backend settings.

If you want to use any of the existing focus point JS libraries you can also define a custom container that will be place around the image. The focus coordinates can be injected as custom data-* attributes.

All of these settings are available on the plugin's backend settings page.

<div class="focuspoint-container" data-focus-x="50" data-focus-y="30">
    <img src="/storage/temp/public/a9f/2bd/159/offline-focus_30_400_500_50_50_0_0_auto__400.jpg" 
         alt="" 
         class="focuspoint-image" 
         data-focus-x="50"
         data-focus-y="30"
     >
 </div>

Browser-Compatibility

Be aware that object-fit is not supported in IE without using a polyfill.

Inlining SVG images

This plugin registers a simple svg helper function that enables you to inline SVG images from your project.

<!-- search in theme directory -->
<div class="inline-svg-wrapper">
    {{ svg('assets/icon.svg') }}
</div>

<!-- start with a / to search relative to the project's root -->
<div class="inline-svg-wrapper">
    {{ svg('/plugins/vendor/plugin/assets/icon.svg') }}
</div>

Using variables

Aside from inlining the SVG itself the helper function will also pass any variables along to the SVG and parse it using October's Twig parser. This means you can easily create dynamic SVGs.

<!-- icon.svg -->
<svg fill="{{ fill }}" width="{{ width | default(800) }}"> ...
<!-- You can pass variables along as a second parameter -->
<img src="{{ svg('/plugins/xy/assets/icon.svg', {fill: '#f00', width: '200'}) }}">
  • Found the plugin useful on 28 Nov, 2019

    Muse have plugin that we put on every october install. You can continue to write your image tags as usual and it make them responsive with some magic. Very very great time saver.

    The recent options about inline SVG and focuspoint is the icing on the cake.

  • Found the plugin useful on 23 Jul, 2016

    Speed up my sites!

  • Found the plugin useful on 23 Mar, 2016

    It breaks the <script type="text/template"> on Responsiv.Uploader (which is the file upload/image upload plugin). See below, if originally there was open and close <div></div>, close tags are not displayed.

    <script type="text/template" id="uploaderTemplateimageUploader"> <div> </script>

2.2.3

Optimized handling of custom src attributes

Nov 28, 2019

2.2.2

Performance improvements (thanks to @mauserrifle)

Nov 22, 2019

2.2.1

Optimized WebP implementation

Nov 17, 2019

2.2.0

Added support for automatic WebP conversion

Nov 16, 2019

2.1.3

Another minor bugfix release

Oct 16, 2019

2.1.2

Fixed Settings form layout

Oct 16, 2019

2.1.1

Fixed bug where images were not resized under certain conditions

Oct 16, 2019

2.1.0

Added focuspoint feature

Oct 05, 2019

2.0.12

Reverted previous change until October's PNG resize bug is fixed (https://github.com/octobercms/library/pull/396)

May 06, 2019

2.0.11

Store copy of original image in the temp folder as well (makes image optimizations possible without the need to modify the original image, thanks to @mauserrifle)

Apr 29, 2019

2.0.10

Re-implemented compatibility fix (thanks to @mauserrifle)

Apr 25, 2019

2.0.9

Reverted previous change since it introduced performance issues

Apr 25, 2019

2.0.8

Fixed php-gd compatibility problem (thanks to @mauserrifle)

Apr 25, 2019

2.0.7

Allow `| media` and `| theme` filters in the `svg` helper function

Mar 07, 2019

2.0.6

Optimized image matching to also include images with custom attributes before the src attribute

Feb 25, 2019

2.0.5

Added missing relations for Theme and SVGInliner classes

Feb 22, 2019

2.0.4

Added `svg` helper function to inline SVGs (see README for usage)

Feb 20, 2019

2.0.3

Fixed handling of relative protocol urls

Jan 14, 2019

2.0.2

Fixed problem when using custom src attributes

Jan 06, 2019

2.0.1

Ignore image tags that don't have a src attribute

Dec 07, 2018

2.0.0

Implemented new image replacement technique (fixes lots of compatibility problems with other plugins)

Nov 29, 2018

1.2.0

Added `responsive-images:generate` artisan command to pre-generate all image sizes (thanks to kiselli)

Nov 27, 2018

1.1.9

Fixed resizing of image paths that contain spaces (thanks to adamo)

Oct 30, 2018

1.1.8

Optimized support for installations that serve multiple domains

Sep 03, 2018

1.1.7

Optimized support for multi-byte character strings (thanks to sergei3456)

Jul 25, 2018

1.1.6

Use correct app url to determine if an image is external or local

Jul 05, 2018

1.1.5

Reverted multi-byte optimization since the change removes the DOCTYPE while parsing the html

Jun 10, 2018

1.1.4

Optimized support for multi-byte character strings (thanks to sergei3456)

Jun 08, 2018

1.1.3

Added french translation (thanks to damsfx)

Jun 08, 2018

1.1.2

Fixed processing of relative pahts when October runs in a subdirectory

Jun 08, 2018

1.1.1

Added compatibility with current edgeUpdate builds

Oct 28, 2016

1.1.0

Added settings page, support for lazy-loading plugins and responsive class attributes

Oct 28, 2016

1.0.4

Fixed handling of filenames containing spaces (Thanks to webeks!)

Oct 18, 2016

1.0.3

Added alternative-src config option to support jQuery.lazyLoad plugin

Feb 02, 2016

1.0.2

Fixed encoding problems

Jan 14, 2016

1.0.1

First version of ResponsiveImages

Dec 12, 2015