Skip to content

Commit

Permalink
Added lazy loading
Browse files Browse the repository at this point in the history
  • Loading branch information
andyzg committed Feb 24, 2018
1 parent 36dc601 commit 523da3f
Show file tree
Hide file tree
Showing 37 changed files with 194 additions and 329 deletions.
360 changes: 90 additions & 270 deletions config.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ <h2 class="header">{{ site.full_name }}</h2>
<div class="footer">Made by <a class="author"
href="http://andyzhang.net/">Andy Zhang</a></div>
</body>
<script
<script
src="https://code.jquery.com/jquery-3.2.1.js"
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lazyload.js"></script>
<script src="js/transition.js"></script>
<script src="js/zoom.min.js"></script>
<script src="js/gallery.js"></script>
Expand Down
66 changes: 40 additions & 26 deletions js/gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@ class Renderer {
sectionElem.appendChild(header);
return sectionElem;
}

createImageElement(photo, width, height, spacing) {
var image = new Image();

image.style.width = width;
image.style.height = height;
image.style.marginBottom = spacing;
image.onload = onImageLoad;
image.setAttribute("data-action", "zoom");

if (photo.isCompressed()) {
// Lazy loading + a compressed image
image.setAttribute("data-original", photo.originalSrc());
image.setAttribute("data-src", photo.compresedSrc());
image.src = photo.placeholderSrc();
image.classList.add('lazyload');
} else {
// Original
image.src = photo.src();
}

return image;
}
}

/**
Expand Down Expand Up @@ -109,19 +132,10 @@ class VerticalRenderer extends Renderer {
* Creates one photo
*/
createPhotoElement(photo, width, config) {
var image = new Image();

image.src = photo.src();
image.style.width = px(width);
image.style.height = px(photo.height(width));
image.style.marginBottom = px(config.spacing);
image.onload = onImageLoad;

if (photo.isCompressed()) {
image.setAttribute("data-original", photo.originalSrc());
}

return image;
return this.createImageElement(photo,
px(width),
px(photo.height(width)),
px(config.spacing));
}

/**
Expand Down Expand Up @@ -303,21 +317,15 @@ class HorizontalRenderer extends Renderer {

for (var i = 0; i < photos.length; i++) {
var photo = photos[i];
var image = new Image();
image.src = photo.src();
image.style.width = px(photo.width(finalHeight));
image.style.height = px(finalHeight);
image.onload = onImageLoad;
var image = this.createImageElement(photo,
px(photo.width(finalHeight)),
px(finalHeight),
px(0));

if (i !== 0) {
image.style.marginLeft = px(config.spacing);
}

image.setAttribute("data-action", "zoom");
if (photo.isCompressed()) {
image.setAttribute("data-original", photo.originalSrc());
}

rowElem.appendChild(image);
}
return rowElem;
Expand All @@ -334,15 +342,13 @@ class Photo {
this._width = p.width;
this._height = p.height;
this._is_compressed = p.compressed;
this.placeholder_path = p.placeholder_path;
this.compressed_path = p.compressed_path;

this.aspectRatio = this._width / parseFloat(this._height);
};

src() {
if (this._is_compressed) {
return this.compressed_path;
}
return this.path;
}

Expand All @@ -354,6 +360,14 @@ class Photo {
return this.path;
}

compresedSrc() {
return this.compressed_path;
}

placeholderSrc() {
return this.placeholder_path;
}

width(height) {
return height * this.aspectRatio;
}
Expand Down
1 change: 1 addition & 0 deletions js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function reqListener() {
}
var config = new Config(JSON.parse(this.responseText), configuration);
renderer.render(config);
lazyload();
}


Expand Down
Binary file added photos/Architecture/DSCF7277.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/Architecture/DSCF7382.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/Architecture/DSCF7399.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/California/DSCF1894.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/California/DSCF2338.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/California/DSCF3168.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/California/DSCF3621.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/California/DSCF4538.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/People/DSCF0589.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/People/DSCF0802.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/People/DSCF0989.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/People/DSCF1981.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/People/DSCF3276.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/People/DSCF3315.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/People/DSCF3356.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/People/DSCF5996.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/People/DSCF6786.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/People/IMG_7888.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/Sunset/DSCF3620.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/Sunset/DSCF5784.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/Sunset/DSCF6512.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/Travel/IMG_7438.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/Travel/IMG_7575.placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added photos/Travel/IMG_7593.placeholder.jpg
Binary file added photos/Travel/IMG_9227.placeholder.jpg
Binary file added photos/Travel/IMG_9675.placeholder.jpg
Binary file added photos/Travel/IMG_9863.placeholder.jpg
Binary file added photos/Travel/IMG_9966.placeholder.jpg
11 changes: 9 additions & 2 deletions setup.command
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

# Create a duplicate of each photo, and then minify them
if [[ "$OSTYPE" == "darwin"* && -x "$(command -v sips)" ]]; then
# sips is available, duplicate all of the images first
python tools/duplicate.py
# sips is available
# low res version of image
python tools/duplicate.py min
sips -Z 640 photos/**/*.min.jpeg &>/dev/null
sips -Z 640 photos/**/*.min.png &>/dev/null
sips -Z 640 photos/**/*.min.jpg &>/dev/null

# placeholder image for lazy loading
python tools/duplicate.py placeholder
sips -Z 64 photos/**/*.placeholder.jpeg &>/dev/null
sips -Z 64 photos/**/*.placeholder.png &>/dev/null
sips -Z 64 photos/**/*.placeholder.jpg &>/dev/null
fi

python tools/setup.py
59 changes: 35 additions & 24 deletions tools/duplicate.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
import os
import re
import shutil
import sys

PHOTO_PATH = './photos/'


def is_image_path(path):
return path.endswith('jpg') or \
path.endswith('jpeg') or \
path.endswith('png')


def to_min_path(path):
# Oh god this code sucks, plz fix
if path.endswith('jpg'):
return path.replace('.jpg', '.min.jpg')
if path.endswith('jpeg'):
return path.replace('.jpeg', '.min.jpeg')
if path.endswith('png'):
return path.replace('.png', '.min.png')

for folder in os.listdir(PHOTO_PATH):
# Ignore other files like .DS_Store
if not os.path.isdir(PHOTO_PATH + folder):
continue

for f in os.listdir(PHOTO_PATH + folder):
path = PHOTO_PATH + folder + '/' + f
if is_image_path(path) and '.min.' not in path:
min_path = to_min_path(path)
shutil.copy(path, min_path)
return re.search(r'\.(jpe?g|png)$', path)


def get_min_path(path):
return re.sub(r'\.(png|jpe?g)$', '.min.\g<1>', path)


def get_placeholder_path(path):
return re.sub(r'\.(png|jpe?g)$', '.placeholder.\g<1>', path)


def get_path(path, ext):
return re.sub(r'\.(png|jpe?g)$', '.' + ext + '.\g<1>', path)


def is_original(path):
return '.min.' not in path and '.placeholder.' not in path


def main(ext):
for folder in os.listdir(PHOTO_PATH):
# Ignore other files like .DS_Store
if not os.path.isdir(PHOTO_PATH + folder):
continue

for f in os.listdir(PHOTO_PATH + folder):
path = PHOTO_PATH + folder + '/' + f
if is_image_path(path) and is_original(path):
min_path = get_path(path, ext)
shutil.copy(path, min_path)

if __name__ == '__main__':
main(sys.argv[1])
23 changes: 17 additions & 6 deletions tools/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
PHOTO_PATH = PATH + RELATIVE_PATH


def is_original(path):
return '.min.' not in path and '.placeholder.' not in path and is_image_path(path)


def is_not_min_path(path):
return not is_min_path(path) and is_image_path(path)

Expand All @@ -29,18 +33,24 @@ def get_directories():


def is_image_path(path):
return path.endswith('jpg') or \
path.endswith('jpeg') or \
path.endswith('png')
return re.search(r'\.(jpe?g|png)$', path)


def get_placeholder_path(path):
return get_path(path, 'placeholder')


def get_min_path(path):
return re.sub(r'\.(png|jpe?g)$', '.min.\g<1>', path)
return get_path(path, 'min')


def get_path(path, ext):
return re.sub(r'\.(png|jpe?g)$', '.' + ext + '.\g<1>', path)


def get_images(path):
items = os.listdir(PHOTO_PATH + '/' + path)
filtered_items = list(filter(is_image_path, items))
filtered_items = list(filter(is_original, items))

result = []
for img in filtered_items:
Expand All @@ -56,7 +66,8 @@ def get_images(path):
'height': height,
'path': './' + RELATIVE_PATH + '/' + path + '/' + img,
'compressed_path': get_min_path(p),
'compressed': has_compressed
'compressed': has_compressed,
'placeholder_path': get_placeholder_path(p)
})
return result

Expand Down

0 comments on commit 523da3f

Please sign in to comment.