diff --git a/.env.dev.example b/.env.dev.example
index 2f24378c..71cc11a1 100644
--- a/.env.dev.example
+++ b/.env.dev.example
@@ -52,3 +52,15 @@ EMAIL_USE_SSL=false
# Set this to true when initializing certbot for domain, false when not
CERTBOT_INIT=false
+
+# Preview image generation can be computing and storage intensive
+# ENABLE_PREVIEW_IMAGES=True
+
+# Specify RGB tuple or RGB hex strings,
+# or use_dominant_color_light / use_dominant_color_dark
+PREVIEW_BG_COLOR=use_dominant_color_light
+# Change to #FFF if you use use_dominant_color_dark
+PREVIEW_TEXT_COLOR=#363636
+PREVIEW_IMG_WIDTH=1200
+PREVIEW_IMG_HEIGHT=630
+PREVIEW_DEFAULT_COVER_COLOR=#002549
diff --git a/.env.prod.example b/.env.prod.example
index a82499a3..e132d216 100644
--- a/.env.prod.example
+++ b/.env.prod.example
@@ -52,3 +52,15 @@ EMAIL_USE_SSL=false
# Set this to true when initializing certbot for domain, false when not
CERTBOT_INIT=false
+
+# Preview image generation can be computing and storage intensive
+# ENABLE_PREVIEW_IMAGES=True
+
+# Specify RGB tuple or RGB hex strings,
+# or use_dominant_color_light / use_dominant_color_dark
+PREVIEW_BG_COLOR=use_dominant_color_light
+# Change to #FFF if you use use_dominant_color_dark
+PREVIEW_TEXT_COLOR=#363636
+PREVIEW_IMG_WIDTH=1200
+PREVIEW_IMG_HEIGHT=630
+PREVIEW_DEFAULT_COVER_COLOR=#002549
diff --git a/bookwyrm/context_processors.py b/bookwyrm/context_processors.py
index a6c535c2..f2661a19 100644
--- a/bookwyrm/context_processors.py
+++ b/bookwyrm/context_processors.py
@@ -1,18 +1,20 @@
""" customize the info available in context for rendering templates """
-from bookwyrm import models
-from bookwyrm.settings import STATIC_URL, STATIC_PATH, MEDIA_URL, MEDIA_PATH
+from bookwyrm import models, settings
def site_settings(request): # pylint: disable=unused-argument
"""include the custom info about the site"""
- request_protocol = "https://" if request.is_secure() else "http://"
+ request_protocol = "https://"
+ if not request.is_secure():
+ request_protocol = "http://"
return {
"site": models.SiteSettings.objects.get(),
"active_announcements": models.Announcement.active_announcements(),
- "static_url": STATIC_URL,
- "media_url": MEDIA_URL,
- "static_path": STATIC_PATH,
- "media_path": MEDIA_PATH,
+ "static_url": settings.STATIC_URL,
+ "media_url": settings.MEDIA_URL,
+ "static_path": settings.STATIC_PATH,
+ "media_path": settings.MEDIA_PATH,
+ "preview_images_enabled": settings.ENABLE_PREVIEW_IMAGES,
"request_protocol": request_protocol,
}
diff --git a/bookwyrm/preview_images.py b/bookwyrm/preview_images.py
index 1decb36a..60b69d0a 100644
--- a/bookwyrm/preview_images.py
+++ b/bookwyrm/preview_images.py
@@ -13,9 +13,9 @@ from django.core.files.uploadedfile import InMemoryUploadedFile
from django.db.models import Avg
from bookwyrm import models, settings
-from bookwyrm.settings import DOMAIN, STATIC_ROOT
from bookwyrm.tasks import app
+
IMG_WIDTH = settings.PREVIEW_IMG_WIDTH
IMG_HEIGHT = settings.PREVIEW_IMG_HEIGHT
BG_COLOR = settings.PREVIEW_BG_COLOR
@@ -27,7 +27,7 @@ margin = math.floor(IMG_HEIGHT / 10)
gutter = math.floor(margin / 2)
inner_img_height = math.floor(IMG_HEIGHT * 0.8)
inner_img_width = math.floor(inner_img_height * 0.7)
-font_dir = os.path.join(STATIC_ROOT, "fonts/public_sans")
+font_dir = os.path.join(settings.STATIC_ROOT, "fonts/public_sans")
def get_font(font_name, size=28):
@@ -103,7 +103,7 @@ def generate_instance_layer(content_width):
if site.logo_small:
logo_img = Image.open(site.logo_small)
else:
- static_path = os.path.join(STATIC_ROOT, "images/logo-small.png")
+ static_path = os.path.join(settings.STATIC_ROOT, "images/logo-small.png")
logo_img = Image.open(static_path)
instance_layer = Image.new("RGBA", (content_width, 62), color=TRANSPARENT_COLOR)
@@ -126,11 +126,11 @@ def generate_instance_layer(content_width):
def generate_rating_layer(rating, content_width):
- icon_star_full = Image.open(os.path.join(STATIC_ROOT, "images/icons/star-full.png"))
+ icon_star_full = Image.open(os.path.join(settings.STATIC_ROOT, "images/icons/star-full.png"))
icon_star_empty = Image.open(
- os.path.join(STATIC_ROOT, "images/icons/star-empty.png")
+ os.path.join(settings.STATIC_ROOT, "images/icons/star-empty.png")
)
- icon_star_half = Image.open(os.path.join(STATIC_ROOT, "images/icons/star-half.png"))
+ icon_star_half = Image.open(os.path.join(settings.STATIC_ROOT, "images/icons/star-half.png"))
icon_size = 64
icon_margin = 10
@@ -307,61 +307,64 @@ def save_and_cleanup(image, instance=None):
@app.task
def generate_site_preview_image_task():
"""generate preview_image for the website"""
- site = models.SiteSettings.objects.get()
+ if settings.ENABLE_PREVIEW_IMAGES == True:
+ site = models.SiteSettings.objects.get()
- if site.logo:
- logo = site.logo
- else:
- logo = os.path.join(STATIC_ROOT, "images/logo.png")
+ if site.logo:
+ logo = site.logo
+ else:
+ logo = os.path.join(settings.STATIC_ROOT, "images/logo.png")
- texts = {
- "text_zero": DOMAIN,
- "text_one": site.name,
- "text_three": site.instance_tagline,
- }
+ texts = {
+ "text_zero": settings.DOMAIN,
+ "text_one": site.name,
+ "text_three": site.instance_tagline,
+ }
- image = generate_preview_image(texts=texts, picture=logo, show_instance_layer=False)
+ image = generate_preview_image(texts=texts, picture=logo, show_instance_layer=False)
- save_and_cleanup(image, instance=site)
+ save_and_cleanup(image, instance=site)
@app.task
def generate_edition_preview_image_task(book_id):
"""generate preview_image for a book"""
- book = models.Book.objects.select_subclasses().get(id=book_id)
+ if settings.ENABLE_PREVIEW_IMAGES == True:
+ book = models.Book.objects.select_subclasses().get(id=book_id)
- rating = models.Review.objects.filter(
- privacy="public",
- deleted=False,
- book__in=[book_id],
- ).aggregate(Avg("rating"))["rating__avg"]
+ rating = models.Review.objects.filter(
+ privacy="public",
+ deleted=False,
+ book__in=[book_id],
+ ).aggregate(Avg("rating"))["rating__avg"]
- texts = {
- "text_one": book.title,
- "text_two": book.subtitle,
- "text_three": book.author_text,
- }
+ texts = {
+ "text_one": book.title,
+ "text_two": book.subtitle,
+ "text_three": book.author_text,
+ }
- image = generate_preview_image(texts=texts, picture=book.cover, rating=rating)
+ image = generate_preview_image(texts=texts, picture=book.cover, rating=rating)
- save_and_cleanup(image, instance=book)
+ save_and_cleanup(image, instance=book)
@app.task
def generate_user_preview_image_task(user_id):
"""generate preview_image for a book"""
- user = models.User.objects.get(id=user_id)
+ if settings.ENABLE_PREVIEW_IMAGES == True:
+ user = models.User.objects.get(id=user_id)
- texts = {
- "text_one": user.display_name,
- "text_three": "@{}@{}".format(user.localname, DOMAIN),
- }
+ texts = {
+ "text_one": user.display_name,
+ "text_three": "@{}@{}".format(user.localname, settings.DOMAIN),
+ }
- if user.avatar:
- avatar = user.avatar
- else:
- avatar = os.path.join(STATIC_ROOT, "images/default_avi.jpg")
+ if user.avatar:
+ avatar = user.avatar
+ else:
+ avatar = os.path.join(settings.STATIC_ROOT, "images/default_avi.jpg")
- image = generate_preview_image(texts=texts, picture=avatar)
+ image = generate_preview_image(texts=texts, picture=avatar)
- save_and_cleanup(image, instance=user)
+ save_and_cleanup(image, instance=user)
diff --git a/bookwyrm/settings.py b/bookwyrm/settings.py
index b0ff5a72..a4cf74f0 100644
--- a/bookwyrm/settings.py
+++ b/bookwyrm/settings.py
@@ -38,14 +38,12 @@ LOCALE_PATHS = [
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
# Preview image
-
-# Specify RGB tuple or RGB hex strings,
-# or "use_dominant_color_light" / "use_dominant_color_dark"
-PREVIEW_BG_COLOR = "use_dominant_color_light"
-PREVIEW_IMG_WIDTH = 1200
-PREVIEW_IMG_HEIGHT = 630
-PREVIEW_TEXT_COLOR = "#363636" # Change to "#FFF" if you use "use_dominant_color_dark"
-PREVIEW_DEFAULT_COVER_COLOR = "#002549"
+ENABLE_PREVIEW_IMAGES = env.bool("ENABLE_PREVIEW_IMAGES", False)
+PREVIEW_BG_COLOR = env.str("PREVIEW_BG_COLOR", "use_dominant_color_light")
+PREVIEW_TEXT_COLOR = env.str("PREVIEW_TEXT_COLOR", "#363636")
+PREVIEW_IMG_WIDTH = env.int("PREVIEW_IMG_WIDTH", 1200)
+PREVIEW_IMG_HEIGHT = env.int("PREVIEW_IMG_HEIGHT", 630)
+PREVIEW_DEFAULT_COVER_COLOR = env.str("PREVIEW_DEFAULT_COVER_COLOR", "#002549")
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
diff --git a/bookwyrm/templates/book/book.html b/bookwyrm/templates/book/book.html
index c4020996..150bda44 100644
--- a/bookwyrm/templates/book/book.html
+++ b/bookwyrm/templates/book/book.html
@@ -4,8 +4,7 @@
{% block title %}{{ book|book_title }}{% endblock %}
{% block opengraph_images %}
-
-
+ {% include 'snippets/opengraph_images.html' with image=book.preview_image %}
{% endblock %}
{% block content %}
diff --git a/bookwyrm/templates/layout.html b/bookwyrm/templates/layout.html
index c06294d9..6e0c4234 100644
--- a/bookwyrm/templates/layout.html
+++ b/bookwyrm/templates/layout.html
@@ -17,8 +17,7 @@
{% block opengraph_images %}
-
-
+ {% include 'snippets/opengraph_images.html' %}
{% endblock %}
diff --git a/bookwyrm/templates/snippets/opengraph_images.html b/bookwyrm/templates/snippets/opengraph_images.html
new file mode 100644
index 00000000..d2c379a8
--- /dev/null
+++ b/bookwyrm/templates/snippets/opengraph_images.html
@@ -0,0 +1,12 @@
+{% if preview_images_enabled is True %}
+ {% if image %}
+
+
+ {% else %}
+
+
+ {% endif %}
+{% else %}
+
+
+{% endif %}
diff --git a/bookwyrm/templates/user/layout.html b/bookwyrm/templates/user/layout.html
index a645b07b..69455806 100644
--- a/bookwyrm/templates/user/layout.html
+++ b/bookwyrm/templates/user/layout.html
@@ -8,8 +8,7 @@
{% block title %}{{ user.display_name }}{% endblock %}
{% block opengraph_images %}
-
-
+ {% include 'snippets/opengraph_images.html' with image=user.preview_image %}
{% endblock %}
{% block content %}