[main] Image webp
All checks were successful
Build, Push, and Deploy to Nomad / docker-nomad (push) Successful in 47s

This commit is contained in:
2024-12-30 20:47:04 +01:00
parent bf1401c32b
commit 13bc417d45
11 changed files with 12 additions and 2 deletions

View File

@@ -1,4 +1,6 @@
import os import os
from pathlib import Path
from fastapi import HTTPException from fastapi import HTTPException
from fastapi.responses import FileResponse from fastapi.responses import FileResponse
from fastapi import APIRouter, Request, FastAPI from fastapi import APIRouter, Request, FastAPI
@@ -27,6 +29,13 @@ class FileHandler:
def dest_filename(self) -> str: def dest_filename(self) -> str:
base_url = "static/images/{category}/{image_type}/{filename}" base_url = "static/images/{category}/{image_type}/{filename}"
return base_url.format( category = self.category, image_type = self.image_type, filename = self.filename ) return base_url.format( category = self.category, image_type = self.image_type, filename = self.filename )
@property
def dest_filename_webp(self) -> str:
base_url = "static/images/{category}/{image_type}/{filename}"
path = Path( base_url.format( category = self.category, image_type = self.image_type, filename = self.filename ) )
if path.suffix != ".webp":
path = path.with_suffix( ".webp" )
return str(path)
@property @property
def dest_path(self) -> str: def dest_path(self) -> str:
@@ -139,7 +148,8 @@ class ImageService:
resized_img = img.resize( (width, height), Image.Resampling.LANCZOS ) resized_img = img.resize( (width, height), Image.Resampling.LANCZOS )
os.makedirs(file_path.dest_path,exist_ok = True) os.makedirs(file_path.dest_path,exist_ok = True)
resized_img.save( file_path.dest_filename ) resized_img.save( file_path.dest_filename )
resized_img.save( file_path.dest_filename_webp, format = "WEBP", quality = 90 ) # Adjust quality as needed
print(file_path.dest_filename_webp)
async def get_image(self, category: str, type: str, filename: str): async def get_image(self, category: str, type: str, filename: str):
file_path = self._resolve_path( category, type, filename ) file_path = self._resolve_path( category, type, filename )
@@ -157,7 +167,7 @@ class ImageService:
file_path = FileHandler(category = category,image_type = image_type,filename = filename) file_path = FileHandler(category = category,image_type = image_type,filename = filename)
self.validate_image( file_path, width = width,height=height, overwrite = True ) self.validate_image( file_path, width = width,height=height, overwrite = True )
tag = f'<img src="/{file_path.dest_filename}" alt="{alt}"' tag = f'<img src="/{file_path.dest_filename_webp}" alt="{alt}"'
if width: if width:
tag += f' width="{width}"' tag += f' width="{width}"'
if height: if height:

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 135 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.5 KiB

After

Width:  |  Height:  |  Size: 5.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 846 B