Skip to content

Commit

Permalink
Add LQIP support for IE11
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed Jul 12, 2018
1 parent 7688586 commit 3ccaa3d
Show file tree
Hide file tree
Showing 23 changed files with 575 additions and 207 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ You can specify which resource are not managed by the service worker by specifyi

## V2.4.1

- Load images using LQIP technique
- Add a service worker for administrator with no caching because admin requests were still cached by the website service worker
- Make lazyloaded images indexabl using noscript tag
- Make lazyloaded images indexable using noscript tag
- force file name generation whenever the settings are changed
- Add new breakpoint 1920px for responsive images and css background images

Expand Down
6 changes: 6 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ cd $DIR
#
#
#
uglifyjs --warn --comments all --ecma=5 --beautify beautify=true,preamble='"/* do not edit this file! */"' --output ./imagesloader.js -- ./imagesloader.es6
uglifyjs --warn --comments all --ecma=5 --beautify beautify=true,preamble='"/* do not edit this file! */"' --output ./imagesnojs.js -- ./imagesnojs.es6
#
#
uglifyjs --ecma=8 --compress passes=3,unsafe_proto=true,warnings=true,pure_funcs=["console.log","console.info"]\
--warn --mangle --keep-fnames --output ./loader.min.js -- ./loader.js
uglifyjs --ecma=8 --compress passes=3,unsafe_proto=true,warnings=true,pure_funcs=["console.log","console.info"]\
--warn --mangle --keep-fnames --output ./cssloader.min.js -- ./cssloader.js
uglifyjs --ecma=8 --compress passes=3,unsafe_proto=true,warnings=true,pure_funcs=["console.log","console.info"]\
--warn --mangle --keep-fnames --output ./imagesloader.min.js -- ./imagesloader.js
uglifyjs --ecma=8 --compress passes=3,unsafe_proto=true,warnings=true,pure_funcs=["console.log","console.info"]\
--warn --mangle --keep-fnames --output ./imagesnojs.min.js -- ./imagesnojs.js
#
#
#
Expand Down
27 changes: 23 additions & 4 deletions css/images.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,37 @@
-ms-filter: progid:DXImageTransform.Microsoft.Blur(PixelRadius='20');
filter: blur(20px);
}
.image-placeholder-no-js {
position: absolute;
opacity: .01;
visibility: hidden;
}
.image-placeholder-wrapper {
position: relative;
display: inline-block;
filter: blur(0);
-ms-filter: progid:DXImageTransform.Microsoft.Blur(PixelRadius='20');
filter: blur(0px);
transition: filter 0.2s ease-out;
}
.image-placeholder-wrapper > img {
position: static;
}
.image-placeholder-wrapper > .image-placeholder-svg {
position: absolute;
display: block;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
.image-placeholder-wrapper > .image-placeholder-svg svg {
max-width: 100%;
max-height: 100%;
}
.image-placeholder-wrapper :not(.image-placeholder-complete) {
-ms-filter: progid:DXImageTransform.Microsoft.Blur(PixelRadius='20');
filter: blur(20px);
}
.image-placeholder-wrapper :not(.image-placeholder-complete) .image-placeholder-opacity {
opacity: 0;
}
.image-placeholder-wrapper .image-placeholder-opacity {
transition: opacity 0.2s ease-out;
display: block;
Expand Down
89 changes: 58 additions & 31 deletions css/images.less
Original file line number Diff line number Diff line change
@@ -1,32 +1,59 @@
.image-placeholder-lqip {
-ms-filter: progid:DXImageTransform.Microsoft.Blur(PixelRadius='20');
filter: blur(20px);
}

.image-placeholder-wrapper {
position: relative;
display: inline-block;
filter: blur(0);
transition: filter 0.2s ease-out;
:not(.image-placeholder-complete) {
filter: blur(20px);
.image-placeholder-opacity {
opacity: 0;
}
}
.image-placeholder-opacity {
transition: opacity 0.2s ease-out; // background: #fff;
display: block;
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
.image-placeholder-element {
display: block;
width: 100%;
height: 100%;
background: 0 0 / cover no-repeat;
}
}
.blur-mixin (@value: 20) {
& {
-ms-filter: progid:DXImageTransform.Microsoft.Blur(PixelRadius='20'); // filter: url(#blur-lqip);
filter: blur(~"@{value}px");
}
}

.image-placeholder-lqip {
.blur-mixin ();
}

.image-placeholder-no-js {
position: absolute;
opacity: .01;
visibility: hidden;
}

.image-placeholder-wrapper {
position: relative;
display: inline-block;
>img {
position: static;
}
>.image-placeholder-svg {
position: absolute;
display: block;
left: 0;
top: 0;
width: 100%;
height: 100%;
svg {
max-width: 100%;
max-height: 100%;
}
}
.blur-mixin (0);
transition: filter 0.2s ease-out;
:not(.image-placeholder-complete) {
.blur-mixin ();
.image-placeholder-opacity {
// opacity: 0;
}
}
.image-placeholder-opacity {
transition: opacity 0.2s ease-out; // background: #fff;
display: block;
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
.image-placeholder-element {
display: block;
width: 100%;
height: 100%;
background: 0 0 / cover no-repeat;
}
}
}
1 change: 1 addition & 0 deletions gzip.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public function onAfterRoute() {
$debug = empty($this->options['debug']) ? '.min' : '';

$document->addCustomTag('<style type="text/css" data-position="head">'.file_get_contents(__DIR__.'/css/images.css').'</style>');
$document->addCustomTag('<script data-position="head" data-ignore="true">'.file_get_contents(__DIR__.'/imagesnojs'.($debug || !empty($this->options['minifyjs']) ? '.min' : '').'.js').'</script>');

$document->addScript('plugins/system/gzip/js/dist/lib'.$debug.'.js');
$document->addScript('plugins/system/gzip/js/dist/lib.images'.$debug.'.js');
Expand Down
4 changes: 3 additions & 1 deletion gzip.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<license>Released under dual license LGPL v3 | MIT</license>
<authorEmail>[email protected]</authorEmail>
<authorUrl>www.tbela.net</authorUrl>
<version>2.4</version>
<version>2.4.1</version>
<description>PLG_SYSTEM_GZIP_XML_DESCRIPTION</description>
<files>
<folder>css</folder>
Expand All @@ -29,6 +29,8 @@
<filename>cssloader.min.js</filename>
<filename>imagesloader.js</filename>
<filename>imagesloader.min.js</filename>
<filename>imagesnojs.js</filename>
<filename>imagesnojs.min.js</filename>
<filename>worker_version</filename>
<filename plugin="gzip">gzip.php</filename>
</files>
Expand Down
4 changes: 2 additions & 2 deletions helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2559,7 +2559,7 @@ public static function parseImages($body, array $options = []) {
if ($src !== '') {

$class = !empty($attributes['class']) ? $attributes['class'].' ' : '';
$attributes['class'] = $class.'image-placeholder image-placeholder-'.strtolower($options['imagesvgplaceholder']);
$attributes['class'] = $class.'image-placeholder image-placeholder-no-js image-placeholder-'.strtolower($options['imagesvgplaceholder']);

$attributes['src'] = $src;
$attributes['data-src'] = $file;
Expand Down Expand Up @@ -2657,7 +2657,7 @@ public static function parseImages($body, array $options = []) {

if (isset($noscript['class'])) {

$noscript['class'] = trim(str_replace(['image-placeholder-lqip', 'image-placeholder-svg', 'image-placeholder'], '', $noscript['class']));
$noscript['class'] = trim(str_replace(['image-placeholder-lqip', 'image-placeholder-svg', 'image-placeholder', 'image-placeholder-no-js'], '', $noscript['class']));

if (empty($noscript['class'])) {

Expand Down
178 changes: 178 additions & 0 deletions imagesloader.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// @ts-check

/**
* lazy image laoder
* @package GZip Plugin
* @copyright Copyright (C) 2005 - 2018 Thierry Bela.
*
* dual licensed
*
* @license LGPL v3
* @license MIT License
*/

LIB.ready(function(undef) {
// intersection-observer.min.js

if (!("srcset" in new Image())) {
// try {

document.body.insertAdjacentHTML(
"beforeend",
"<svg xmlns=http://www.w3.org/2000/svg width=1 height=1>" +
"<defs>" +
"<filter id=blur-lqip width=100% height=100% >" +
"<feGaussianBlur stdDeviation=20 />" +
'</filter>' +
'</defs>'+
'</svg>'
);
}

function lazyload() {
LIB.images.lazy(".image-placeholder").on({

/**
*
* @param {HTMLImageElement} img
* @param {HTMLImageElement} oldImage
*/
preload: function(img, oldImage) {

const legacy = !("currentSrc" in img);

if (!legacy) {
oldImage.insertAdjacentHTML(
"beforebegin",
'<span class=image-placeholder-wrapper><span class=image-placeholder-opacity><span class=image-placeholder-element style="background-image:url(\'' +
(img.currentSrc || img.src) +
"')\">"
);
}

else {
oldImage.insertAdjacentHTML(
"beforebegin",
"<span class=image-placeholder-wrapper><span class=image-placeholder-svg><svg width=100% height=100% version=1.1 xmlns=http://www.w3.org/2000/svg >" +
'<image xlink:href="' +
(img.currentSrc || img.src) +
'" width=100% height=100% filter=url(#blur-lqip) x=0 y=0 />"'
);
}

const container = oldImage.previousElementSibling;

if (legacy) {

if ( typeof window.CustomEvent != "function" ) {

function CustomEvent ( event, params ) {
params = params || { bubbles: false, cancelable: false, detail: undefined };
const evt = document.createEvent( 'CustomEvent' );
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
return evt;
}

CustomEvent.prototype = window.Event.prototype;
}

const svg = container.querySelector('svg');
// const svgImage = container.querySelector('svg image');
function resize () {

const height = this.height;
const width = this.width;

svg.setAttribute('height', height);
svg.setAttribute('width', width);

// svgImage.setAttribute('height', height);
// svgImage.setAttribute('width', width);
}

img.addEventListener('sourcechange', resize);
img.addEventListener('load', resize);
}

oldImage.classList.remove(
"image-placeholder-lqip",
"image-placeholder-svg",
"image-placeholder"
);
container.insertBefore(oldImage, container.firstElementChild);
},
load: function(img, oldImage) {
if (oldImage.dataset.src != undef) {
oldImage.src = oldImage.dataset.src;
// oldImage.removeAttribute("data-src");
}

if (oldImage.dataset.srcset != undef) {
oldImage.srcset = oldImage.dataset.srcset;
// oldImage.removeAttribute("data-srcset");
}

setTimeout(function() {
let container = oldImage;

oldImage.removeAttribute("data-srcset");
oldImage.removeAttribute("data-src");

while (
container != undef &&
!container.classList.contains(
"image-placeholder-wrapper"
)
) {
container = container.parentElement;
}

container.classList.add("image-placeholder-complete");

setTimeout(function() {

// if (container.parentElement != null) {

container.parentElement.insertBefore(
oldImage,
container
);

container.parentElement.removeChild(container);
// }

}, 10);
}, 10);
}
});
}

if (
!(
"IntersectionObserver" in window &&
"IntersectionObserverEntry" in window &&
"intersectionRatio" in window.IntersectionObserverEntry.prototype
)
) {
const script = document.createElement("script");
/*script.onreadystatechange =*/ script.onload = lazyload;
script.defer = true;
script.async = true;
script.src = "{script-src}";
document.body.appendChild(script);
} else {
if (!("isIntersecting" in window.IntersectionObserverEntry.prototype)) {
Object.defineProperty(
window.IntersectionObserverEntry.prototype,
"isIntersecting",
{
get: function() {
return this.intersectionRatio > 0;
}
}
);
}

lazyload();
}
});
Loading

0 comments on commit 3ccaa3d

Please sign in to comment.