generated from hjess/PythonTemplateProject
Lets see what lighthouse says
Some checks failed
Build, Push, and Deploy to Nomad / docker-nomad (push) Has been cancelled
Some checks failed
Build, Push, and Deploy to Nomad / docker-nomad (push) Has been cancelled
This commit is contained in:
72
app/services/image_controller.py
Normal file
72
app/services/image_controller.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import os
|
||||
from PIL import Image
|
||||
|
||||
class ImageHandler:
|
||||
def __init__(self, base_dir: str):
|
||||
"""
|
||||
Initialize the ImageHandler.
|
||||
|
||||
:param base_dir: Base directory for storing and retrieving images.
|
||||
"""
|
||||
self.base_dir = base_dir
|
||||
|
||||
def get_image_path(self, filename: str) -> str:
|
||||
"""
|
||||
Construct the full path for a given image file.
|
||||
|
||||
:param filename: Relative filename of the image.
|
||||
:return: Full path to the image.
|
||||
"""
|
||||
return os.path.join(self.base_dir, filename)
|
||||
|
||||
def get_resized_image_path(self, filename: str, width: int, height: int) -> str:
|
||||
"""
|
||||
Construct the path for a resized image.
|
||||
|
||||
:param filename: Original image filename.
|
||||
:param width: Desired width.
|
||||
:param height: Desired height.
|
||||
:return: Path to the resized image.
|
||||
"""
|
||||
return os.path.join(self.base_dir, f"resized_{width}x{height}_{filename}")
|
||||
|
||||
def resize_and_save(self, original_path: str, resized_path: str, width: int, height: int):
|
||||
"""
|
||||
Resize and save the image if it doesn't already exist.
|
||||
|
||||
:param original_path: Path to the original image file.
|
||||
:param resized_path: Path to save the resized image.
|
||||
:param width: Desired width.
|
||||
:param height: Desired height.
|
||||
"""
|
||||
if not os.path.exists(resized_path):
|
||||
with Image.open(original_path) as img:
|
||||
img_resized = img.resize((width, height))
|
||||
img_resized.save(resized_path, format="JPEG")
|
||||
|
||||
def generate_image_tag(self, src: str, width: int, height: int, css_class: str = "", alt: str = "") -> str:
|
||||
"""
|
||||
Generate an HTML <img> tag and ensure the image exists with the specified dimensions.
|
||||
|
||||
:param src: Relative path to the original image.
|
||||
:param width: Desired width of the image.
|
||||
:param height: Desired height of the image.
|
||||
:param css_class: Optional CSS class to add to the <img> tag.
|
||||
:param alt: Alternative text for the image.
|
||||
:return: HTML <img> tag.
|
||||
"""
|
||||
original_path = self.get_image_path(src)
|
||||
if not os.path.isfile(original_path):
|
||||
raise FileNotFoundError(f"Image not found: {src}")
|
||||
|
||||
# Construct resized image path
|
||||
resized_filename = f"resized_{width}x{height}_{os.path.basename(src)}"
|
||||
resized_path = self.get_resized_image_path(src, width, height)
|
||||
|
||||
# Resize and save the image if necessary
|
||||
self.resize_and_save(original_path, resized_path, width, height)
|
||||
|
||||
# Return the <img> tag
|
||||
class_attr = f' class="{css_class}"' if css_class else ""
|
||||
alt_attr = f' alt="{alt}"' if alt else ""
|
||||
return f'<img src="/{resized_path}" width="{width}" height="{height}"{alt_attr}{class_attr}>'
|
||||
@@ -1,6 +1,6 @@
|
||||
import markdown
|
||||
from jinja2 import Environment, DictLoader
|
||||
|
||||
from .image_controller import ImageHandler
|
||||
# Define Jinja2 custom functions
|
||||
def img_left_overlay(src):
|
||||
"""Render an image with overlay."""
|
||||
@@ -82,6 +82,8 @@ def slider(options, images):
|
||||
def create_jinja_environment():
|
||||
"""Create and configure the Jinja2 environment."""
|
||||
env = Environment(loader=DictLoader({"base_template": "{{ content | safe }}"}))
|
||||
image_handler = ImageHandler(base_dir="static/images")
|
||||
|
||||
env.globals.update({
|
||||
"img_left_overlay": img_left_overlay,
|
||||
"box": box,
|
||||
@@ -89,6 +91,8 @@ def create_jinja_environment():
|
||||
"warning": warning,
|
||||
"link_to": link_to,
|
||||
"slider": slider,
|
||||
"image": image_handler.generate_image_tag, # Add image handler function
|
||||
|
||||
})
|
||||
return env
|
||||
|
||||
@@ -113,11 +117,7 @@ def render_markdown_with_jinja(markdown_content: str):
|
||||
template = env.get_template("base_template")
|
||||
final_html = template.render(content=intermediate_html)
|
||||
|
||||
#Step 3: Re-render final_html in Jinja2 for embedded tags like {{ box(...) }}
|
||||
|
||||
try:
|
||||
final_output = env.from_string(final_html).render()
|
||||
except:
|
||||
print(final_html)
|
||||
# Step 3: Re-render final_html in Jinja2 for embedded tags like {{ image(...) }}
|
||||
final_output = env.from_string(final_html).render()
|
||||
|
||||
return final_output, metadata
|
||||
|
||||
Reference in New Issue
Block a user