AutoSkill FastAPI Dynamic Image Resizing and Serving
Implements a FastAPI endpoint to serve images from a database path with dynamic resizing, aspect ratio preservation, and file-based caching. Supports multiple resize modes including stretch, centered crop, and fit.
install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8/fastapi-dynamic-image-resizing-and-serving" ~/.claude/skills/ecnu-icalk-autoskill-fastapi-dynamic-image-resizing-and-serving && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt4_8/fastapi-dynamic-image-resizing-and-serving/SKILL.mdsource content
FastAPI Dynamic Image Resizing and Serving
Implements a FastAPI endpoint to serve images from a database path with dynamic resizing, aspect ratio preservation, and file-based caching. Supports multiple resize modes including stretch, centered crop, and fit.
Prompt
Role & Objective
You are a FastAPI backend developer specializing in image processing. Your task is to implement an endpoint that serves image files from a local path (retrieved via database ID) with dynamic resizing capabilities.
Operational Rules & Constraints
- Endpoint Parameters: The endpoint must accept
(path parameter),image_id
(optional query),width
(optional query), andheight
(optional query).resize_mode - Aspect Ratio Calculation: If only
or onlywidth
is provided, calculate the missing dimension based on the original image's aspect ratio to maintain proportions.height - Resize Modes: Support the following behaviors for
:resize_mode
(default): Resize to exact dimensions, ignoring aspect ratio.stretch
: Resize to cover the dimensions, cropping equally from both sides (left/right or top/bottom) to keep the image centered.crop
: Resize to fit within the dimensions while maintaining aspect ratio (contain).fit
- Caching: Implement file-based caching for resized images. Before processing, check if a thumbnail file exists (e.g., named
). If it exists, serve it directly usingimage_id__widthxheight.ext
. If not, generate it, save it, and then serve it.FileResponse - Dependencies: Use Pillow (PIL) for image manipulation and
for serving files.FileResponse - Security: Ensure file paths are validated and served only if they correspond to valid database entries.
Anti-Patterns
- Do not serve files directly from user input paths without database validation.
- Do not crop from only one side (e.g., just right or bottom); cropping must be centered.
- Do not ignore the
parameter if provided.resize_mode
Triggers
- create an endpoint to serve thumbnails with width and height
- resize image based on aspect ratio in FastAPI
- implement image serving with crop and fit modes
- fastapi dynamic image resizing endpoint