Giter Site home page Giter Site logo

bnomei / kirby3-srcset Goto Github PK

View Code? Open in Web Editor NEW
43.0 3.0 3.0 1.69 MB

Kirby 3 Plugin for creating lazyloading image srcset

Home Page: https://forum.getkirby.com/t/kirby-3-srcset-lazyloading-image-srcset-element/23575

License: MIT License

PHP 79.97% HTML 2.13% Vue 17.56% JavaScript 0.35%
kirby3 kirby3-cms kirby3-plugin srcset images lazysizes kirbytag editor-block img lazyload

kirby3-srcset's Introduction


With Kirby 3.6 introducing support for WebP & AVIF images and the lazy-attribute being well supported by modern browsers this plugin has in my opinion become obsolete. Thus I will not continue to work on it.

Kirby 3 Srcset

Release Downloads Build Status Coverage Status Maintainability Twitter

Kirby 3 Plugin for creating lazyloading image srcset

This plugin extends the core (image: )-Kirbytag. All params that work for (image: ) can be used with (lazysrcset: ) yielding the same output. By default the plugin does add the following:

  • lazyloading class to the img element
  • data-src image based on the params of the tag width, height, quality. This replaces the src attribute.
  • data-srcset attribute with srcsets generated by Kirbys core $file->srcset() method
  • data-sizes="auto" attribute
  • data-thumb-srcset attribute printing the srcset array as json (only when debugging)
  • data-ratio attribute with ratio of the image to parent of img element
  • It registers a custom markdown component since needs to wrap and unwrap the markdown.

TOC

  1. Commercial Usage
  2. Installation
  3. Usage Kirbytag
  4. Usage Editor Block
  5. Usage PHP
  6. Setup
  7. Settings
  8. FAQ
  9. Image Ratio

Commercial Usage


Support open source!

This plugin is free but if you use it in a commercial project please consider to sponsor me or make a donation.
If my work helped you to make some cash it seems fair to me that I might get a little reward as well, right?

Be kind. Share a little. Thanks.

‐ Bruno
 
M O N E Y
Github sponsor Patreon Buy Me a Coffee Paypal dontation Buy a Kirby license using this affiliate link

Installation

  • unzip master.zip as folder site/plugins/kirby3-srcset or
  • git submodule add https://github.com/bnomei/kirby3-srcset.git site/plugins/kirby3-srcset or
  • composer require bnomei/kirby3-srcset

Usage Kirby Tag

image lazysrcset
alt, caption, class, height, imgclass, link, linkclass, rel, target, text, title, width sizes, lazy, prefix, autosizes, quality, figure
# like image tag
(image: myfile.jpg)
(lazysrcset: myfile.jpg)
(lazysrcset: myfile.jpg link: mypdf.pdf)
(lazysrcset: myfile.jpg class: myclass)

# changing width, height and/or quality of src (not srcset)
(lazysrcset: myfile.jpg width: 640 height: 480 quality: 70)

# different lazy class
(lazysrcset: myfile.jpg lazy: lazy)

# different or no prefix
(lazysrcset: myfile.jpg prefix: )

# remove wrapping figure element
(lazysrcset: myfile.jpg figure: false)

# remove the ratio information
(lazysrcset: myfile.jpg ratio: false)

# sizes from config
(lazysrcset: myfile.jpg sizes: default)
(lazysrcset: myfile.jpg sizes: breakpoints)

# sizes are supported in various formats
# string, number(s), with and without px, comma and brackets
(lazysrcset: myfile.jpg sizes: 320 640 960)
(lazysrcset: myfile.jpg sizes: [320, 640, 960])
(lazysrcset: myfile.jpg sizes: 320px, 640px, 960px)

Usage Editor Block

srcset editor block

Specifically allow plugin block

When you specifically define which blocks are allowed in an editor field you need to add the plugin block like this:

fields:
 text:
   label: Editor
   type: editor
   allowed: 
     - h1
     - paragraph
     - srcset # matches name of plugin

Usage PHP

echo $page->image('ukulele.jpg')->lazysrcset();

// 320 and 1200
echo $page->image('ukulele.jpg')->lazysrcset('default');
 
// 576, 768, 992, 1200
echo $page->image('ukulele.jpg')->lazysrcset('breakpoints');
 
// 320, 640, 960
echo $page->image('ukulele.jpg')->lazysrcset(['sizes' => [320, 640, 960]]);

// other options
echo $page->image('ukulele.jpg')->lazysrcset([
    // lazysrcset
    'sizes' => [320, 640, 960],
    'lazy' => 'lazyloading-with-flickity',
    'prefix' => 'data-flickity-lazyload-',
    'figure' => false,
    'autosizes' => false,
    // image
    'width' => 640,
    'height' => 480,
    // ...
]);

Setup

Config

You need to define srcsets. The plugin will use these as well as the core $file->srcset() function.

/site/config/config.php

return [
    'thumbs' => [
        'srcsets' => [
            'default' => [320, 1200],
            'breakpoints' => [576, 768, 992, 1200],
        ],
    ],
];

JS

Add aFarkas/lazysizes to your js dependencies and RTFM. You can also use lozad.js with little adjustments in the settings.

Minimal CSS for auto-sizes detection

You will need some css to make the lazysizes lib automatic size detection work.

figure { width: 100%; }
img[data-sizes="auto"] { display: block; width: 100%; }

IMPORTANT: The css must be applied to the DOM before the JS is executed. Make sure the order is right and they are not async applied. With a lib like muicss/loadjs you can load them asyn and apply them in proper order. Otherwise you will face blurry images on first load.

CSS for hinted image ratio

By default the images ratio is hinted by the plugin to minimize reflows and avoid page jumps. You just need to add the following css to you codebase.

TIP: you can use a different class name if you set ratio to a different string. If you set either ratio or figure to false the ratio hints will disappear.

CSS

/* class name must matches the value of `ratio` setting. */
.lazysrcset-ratio {  
    position: relative;
}
.lazysrcset-ratio > img {
    display: block;    
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
}
.lazysrcset-ratio > img:after {
    display: block;
    width: 100%;
    height: 0;
    content: '';
}

tailwind.css

.lazysrcset-ratio {
    @apply relative;
    & > img {
        @apply absolute top-0 left-0 w-full h-full block;
        &:after {
            content: '';
            @apply block h-0 w-full;
        }
    }
}

Each figure element will be prefixed with a style element defining the ratio. For example the css style for an image with 16/9 ratio followed by its figure image element would look like this:

without figcaption: figure > img

<style>.lazysrcset-ratio[data-ratio="56.25"]{padding-bottom:56.25%;}</style><figure data-ratio="56.25" class="lazysrcset-ratio"><!--- image tag with srcset --></figure>

with figcaption: figure > ((div > img) + figcaption)

<style>.lazysrcset-ratio[data-ratio="56.25"]{padding-bottom:56.25%;}</style><figure><div data-ratio="56.25" class="lazysrcset-ratio"><!--- image tag with srcset --></div><figcaption>with caption</figcaption></figure>

with nonce

You can set a custom nonce using the options or install the security headers plugin.

<style nonce="A-NONCE-HERE">...

Settings

bnomei.srcset. Default Description
lazy lazyload bool or string. additional class for imgclass param
prefix data- bool or string. prefix before srcset and src when doint lazy loading
autosizes auto bool or string. related to data-sizes attribute
figure true bool. wrap image in figure or not
ratio lazysrcset-ratio bool or string. adds data-ratio to parent of img
nonce null null, string or callback. default nonce for style element

FAQ

  • Does the plugin override the (image: )-Kirbytag? No and it never will.
  • Why is the Javascript library for lazy loading not included? Since that should be part of the websites build chain.
  • IE11 support? You will need a Picture Polyfill.
  • Why is there no sizes attribute? It is not defined since js lib lazysizes can create these on-the-fly based on actual screen size of image. see autosizes setting.
  • Lazyloading with Flickity? Do 'prefix' => 'data-flickity-lazyload-'.
  • Where is the picture element support? This plugin v2.x.x versions could do picture element. I removed that and focused on a better kribytag and lazyloading to make the plugin simpler again.

Works well with

Disclaimer

This plugin is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please create a new issue.

License

MIT

It is discouraged to use this plugin in any project that promotes racism, sexism, homophobia, animal abuse, violence or any other form of hate speech.

kirby3-srcset's People

Contributors

bnomei avatar robinscholz avatar tasinttttttt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

kirby3-srcset's Issues

Page breaks when Editor plugin is not installed

As soon as I install the plugin into a new Kirby installation that doesn't have the Editor installed, all I get when opening the site in a browser is a blank page, no error messages at all.

Support multiple image formats for the same file

As Kirby 3.6 will support AVIF and Webp image files natively it would be great to have an option to automatically provide the most optimized image format of an image source as possible. With a tag it is possible to define multiple sources and Kirby can convert them with the thumb method.Having lazy srcset as a plugin could make it very easy to always provide the best image file based on the browser capabilities.

[FAQ] Override default Kirbytext Image tag

Question
Override default Kibytext image tag

(image: myfile.jpg)

Answer
Overriding even just as an option might conflict with various other plugins.
Create a kirbytext:before hook and do a text replace if you really want this.
Or just use the srcset tag right from the start.

(srcset: myfile.jpg)
(srcset: myfile.jpg preset: breakpoints)
(srcset: myfile.jpg lazy: true)
(srcset: myfile.jpg preset: breakpoints lazy: true)

/site/config/config.php

return [
    'hooks' => [
        'kirbytags:before' => function ($text, $data, $options) {
            return str_replace('(image:', '(srcset:', $text);
        }
    ]
]

Classes are not added when using the Kirby tag

When I use the kirbytag in connection with a class (srcset: 01.jpg class: w-1/2), the class is not added to the picture (or image) element.

When I use the kirbytag in connection with a class and a link (srcset: 01.jpg class: w-1/2 link: 01.jpg), the class is added to the enclosing a tag.

Editor field missing since v3.3.7

since version 3.3.7 the lazysrcset editor field was missing.

i changed the version to 3.3.6 in the composer.json and that fixed it.

i am sorry that i cannot give any further details since i don't know what causes the problem.

i am using the latest version of kirby3 (aug 29 2020) and editor (aug 29 2020) and the php version is 7.4

lazy loading lib

add documentation thats lazy loading lib is not included and why.

Markdown Extra <srcsetplugin> custom tag breaks grid layout

I've an issue with the custom tag that will be injected if markdown extra is enabled. This inevitability is an issue when working with css grid as it breaks the layout. Support for custom tags is limited, srcsetplugin is anyway not a valid custom tag without a dash, and it causes issues with css preprocessing and linting when I try to add a display:contents; to it, taking it out of the render path.

What is the purpose of the tag, isn't there a more standardised option or can it be disabled?

Only calls imgSrcset()

Hi!

Thanks so much for your work on this plugin - been using it since the release of Kirby 3 and it's been great. With the new update it doesn't seem to be calling the picture tag snippet, I've tried all the file methods and all of them seem to only make an img srcset tag - no picture tag or sources.

I fiddled around a bit in the index.php, swapping out places you call the snippets and this seems to be the case - I got it to call the picture tag by snippet referenced on this line:

'plugin-srcset-img' => __DIR__ . '/snippets/srcset-img.php',

Hopefully you can figure out why.

Srcset with comma instead of period

I'm using your plugin and everything is fine, but I'm using it for a french website where we use commas instead of periods for decimals. That breaks the css and I don't know what to do.

<style>.lazysrcset-ratio[data-ratio="37,51"]{padding-bottom:37,51%;}</style>

kirby3-srcset breaks distantnative/retour-for-kirby

I'm not sure what the cause is, so i don't have a lot to go from. If bnomei/kirby3-srcset ist installed, either via composer or direct download, distantnative/retour-for-kirby will break.
It disappears from the menu in the panel and the page for retour goes blank.

I confirmed this in a blank plainkit install. I am on kirby v3.3.6 with php v7.3.11.

If someone could point to a place i could look and provide more information, i'd be glad to do so!

Can the issue be recreated, or is this something on my site?

lazysrcset can not create srcset from null

I've just updated some submodules in a Kirby3 web site.

I noticed that this change has broken my usage of kirby3-srcset: v3.3.3...v3.3.4

I get:
lazysrcset can not create srcset from null

If I change back to v.3.3.3 the images load again.

I'm creating lazysrcset tags in a Kirby text field:
(lazysrcset: real-estate-banner.png imgclass: radius)

This works:
(image: real-estate-banner.png imgclass: radius)

Were there any other changes to make with v3.3.4 and up?

Warning with Editor Plugin

I'm getting a warning when installing your plugin:

Warning: include([…]site/plugins/kirby3-srcset/classes/Kirby/Editor/../../../tests/site/plugins/editor/lib/Block.php): failed to open stream: No such file or directory in […]/kirby/config/helpers.php on line 496

thought it might be because I haven't installed the Editor Beta yet, but it is also happens if the Editor Plugin is available. Might be related to an autoloader issue as I'm using kirby-cms-path and kirby-plugin-path to alter my composer setup.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.