first commit
This commit is contained in:
24
backend/app/services/image_service.py
Normal file
24
backend/app/services/image_service.py
Normal file
@@ -0,0 +1,24 @@
|
||||
"""Image post-processing helpers (thumbnail generation, etc.)."""
|
||||
from pathlib import Path
|
||||
|
||||
from PIL import Image
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
|
||||
def generate_thumbnail(file_path: str, max_size: int = 400) -> str:
|
||||
full = settings.upload_path / file_path
|
||||
if not full.exists():
|
||||
return file_path
|
||||
|
||||
thumb_dir = full.parent / "thumbs"
|
||||
thumb_dir.mkdir(exist_ok=True)
|
||||
thumb_path = thumb_dir / full.name
|
||||
|
||||
try:
|
||||
with Image.open(full) as img:
|
||||
img.thumbnail((max_size, max_size))
|
||||
img.save(thumb_path)
|
||||
return str(thumb_path.relative_to(settings.upload_path))
|
||||
except Exception:
|
||||
return file_path
|
||||
Reference in New Issue
Block a user