Inspired by https://dri.es/optimizing-site-performance-by-lazy-loading-images#1 . Lazy module already has feature of having placeholder, but unfortunately it will always be the same image, but it will be nice to have just low resolution image with the same aspect ratio and content as the original image.
That is why I suggest adding additional parameter to the lazy loading formatter where user can select the image style that will represent the placeholder image. User will control itself how "bad" or "good" the image should be, by selecting self defined image style. It could be 1x1 image or 10x6 or any other that will represent the original image better. This way the lazy loading will be Instagram like with some tweaks in styling.
Patch is attached.
| Comment | File | Size | Author |
|---|---|---|---|
| #15 | lazy-placeholder_image_styles_fix_empty-3131157-15.patch | 1.22 KB | pianomansam |
| #14 | Lazytest2.mov | 6.65 MB | adamjuran |
| #14 | Lazytest1.mov | 4.91 MB | adamjuran |
| #7 | lazy_placeholder_image_style.png | 161.16 KB | osman |
| #4 | test_lazy_with_placeholder.mp4 | 2.62 MB | breidert |
Comments
Comment #2
a.dmitriiev commentedComment #3
a.dmitriiev commentedComment #4
breidert commentedThe patch works as expected on a clean Drupal 8 installation.
The new image formatter has a new option to set a placeholder image style. The placeholder is correctly rendered.
However, since the module has the following CSS rule, the placeholder is not shown:
This CSS would need to be removed or overridden in a theme to show the placeholder.
Here is a short video of a page reload with placeholder and simulated slow network:
https://www.drupal.org/files/issues/2020-05-23/test_lazy_with_placeholder.mp4
I suggest we make this configurable configurable in the module. This could be a setting in /admin/config/content/lazy
What do you thing about this @osman?
Moving this to RTBC, because styles can be overridden in theme.
Comment #6
osmanHi @breidert,
I think this is a very good idea, I like it very much. And thank you for the patch.
I also RTBC, hence committed it on 8.x-3.x.
Thank you :)
Comment #7
osmanI'm actually now working on a tune-up for this, building on top of your idea.
Here's a preview:
Comment #9
osmanI wanted to keep the data URI use optional, as it may not be ideal for all cases. same as the placeholder image style selection.
If a placeholder image style is selected it would be used for that field.
If no placeholder image style is selected, the module settings apply for that field (admin/config/content/lazy): Shared settings > Placeholder image URL.
Or if an image URL or data URI defined, it would be used for all fields not using placeholder image style option.
Now, when an image style is selected for the placeholder image, Use data URI option becomes available.
Comment #12
adamjuran commented@osman are you still working on this or is ready for review?
Comment #13
osmanHi @adamjuran it is ready for a review/test-drive :)
Comment #14
adamjuran commentedLazytest1 is with standard 100x100 thumbnail as placeholder while logged out.
Lazytest2 is with a 5px wide new image style as placeholder while logged in.
In admin UI, "Enable lazy-loading" checkbox is there, as is data URI checkbox. Both work as expected.
Moving back to RTBC.
Comment #15
pianomansam commentedThe commit in #8 removed some important empty checks in the third-party settings in lazy_preprocess_field(). These empty checks are important for anyone upgrading from previous version of dev. Without the empty checks, I'm getting these notices:
Attached is a screenshot of the change in #8 that removed these empty checks.
Also attached is a patch that restores the empty checks.
Comment #16
pianomansam commentedComment #18
osmannice catch @pianomansam. and thanks for the patch.