From 46f71a9f4af7827b09565759dcecaa70abe8c589 Mon Sep 17 00:00:00 2001 From: TheOrangeOne Date: Fri, 26 Aug 2016 11:41:35 +0100 Subject: [PATCH] add multisize favicon and apple files automatically --- pelicanconf.py | 2 ++ plugins/image_resizer.py | 69 +++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 ++ theme/templates/base.html | 7 +++- 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 plugins/image_resizer.py diff --git a/pelicanconf.py b/pelicanconf.py index a68149d..e94ad35 100644 --- a/pelicanconf.py +++ b/pelicanconf.py @@ -30,6 +30,8 @@ INDEX_PROJECTS = links.index_projects() # Extra config REPO = Repo(search_parent_directories=True) BUILD_PRODUCTION = 'BUILD_PRODUCTION' in os.environ +import image_resizer +META_IMAGES = image_resizer.generate() # Disable some pages TAG_URL = False diff --git a/plugins/image_resizer.py b/plugins/image_resizer.py new file mode 100644 index 0000000..78e9e3a --- /dev/null +++ b/plugins/image_resizer.py @@ -0,0 +1,69 @@ +import os +from PIL import Image +from resizeimage import resizeimage + +OUTPUT_DIR = os.path.realpath('theme/static/build/img') +ORIGINAL_IMAGE_PATH = os.path.realpath('content/assets/img/logo-transparent.png') + +FAVICON_SIZES = ( + (16, 16), + (32, 32), + (96, 96), + (128, 128), + (196, 196) +) + +APPLE_SIZES = ( + (152, 152), + (144, 144), + (120, 120), + (114, 114), + (72, 72), + (60, 60), + (57, 57) +) + + +def merge_dicts(*dict_args): + result = {} + for dictionary in dict_args: + result.update(dictionary) + return result + + +def get_size_string(w, h): + return "{0}x{1}".format(w, h) + + +def build_apple_filenames(): + return {s: "apple-touch-icon-{0}.png".format(get_size_string(*s)) for s in FAVICON_SIZES} + + +def build_favicon_filenames(): + return {s: "favicon-{0}.png".format(get_size_string(*s)) for s in APPLE_SIZES} + + +def get_all_files(): + favicon_filenames = build_favicon_filenames() + apple_filenames = build_apple_filenames() + return merge_dicts(favicon_filenames, apple_filenames) + + +def generate(): + print("Removing old files...") + for size, image_file in get_all_files().items(): + try: + os.remove(os.path.join(OUTPUT_DIR, image_file)) + except FileNotFoundError: + continue + + with open(ORIGINAL_IMAGE_PATH, 'rb') as original_image_file: + with Image.open(original_image_file) as original_image: + for size, image_name in get_all_files().items(): + new_image = resizeimage.resize_contain(original_image, size) + new_image.save(os.path.join(OUTPUT_DIR, image_name), original_image.format) + + favicon_image_details = [('icon', get_size_string(*size), filename) for size, filename in build_favicon_filenames().items()] + apple_image_details = [('apple-touch-icon-precomposed', get_size_string(*size), filename) for size, filename in build_apple_filenames().items()] + + return favicon_image_details + apple_image_details diff --git a/requirements.txt b/requirements.txt index 26d71d1..c097b5f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,5 @@ git+https://github.com/ryneeverett/python-markdown-comments.git pelican==3.6.3 pelican-minify==0.9 pyembed-markdown==1.1.0 +python-resize-image==1.1.3 +sh==1.11 diff --git a/theme/templates/base.html b/theme/templates/base.html index 87483b5..3e731bb 100644 --- a/theme/templates/base.html +++ b/theme/templates/base.html @@ -6,13 +6,18 @@ + + {% block metadata %}{% endblock %} + {% for rel, size, filename in META_IMAGES %} + + {% endfor %} + {% block htmltitle %}Page{% endblock %} - {{ SITENAME }} - {% block navbar %}