1
Fork 0

add multisize favicon and apple files automatically

This commit is contained in:
Jake Howard 2016-08-26 11:41:35 +01:00
parent d45933782e
commit 46f71a9f4a
Signed by: jake
GPG key ID: 57AFB45680EDD477
4 changed files with 79 additions and 1 deletions

View file

@ -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

69
plugins/image_resizer.py Normal file
View file

@ -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

View file

@ -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

View file

@ -6,13 +6,18 @@
<meta http-equiv="Content-Language" content="{{ DEFAULT_LANG }}" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width" />
<link rel="alternate" type="application/atom+xml" href="/{{ FEED_ATOM }}" />
<meta name="application-name" content="{{ SITENAME }}" />
<link rel="shortcut icon" href="/static/img/logo-transparent.png" type="image/png" />
{% block metadata %}{% endblock %}
{% for rel, size, filename in META_IMAGES %}
<link rel="{{ rel }}" sizes="{{ size }}" href="/static/img/{{ filename }}" />
{% endfor %}
<title>{% block htmltitle %}Page{% endblock %} - {{ SITENAME }}</title>
<link rel="stylesheet" href="/static/css/index.css" type="text/css" />
<link rel="shortcut icon" href="/static/img/logo-transparent.png" type="image/png" />
</head>
<body id="page-top">
{% block navbar %}