AutoSkill FastAPI Dynamic Image Resizing and Thumbnailing
Implement a FastAPI endpoint to serve images with dynamic resizing, aspect ratio preservation, and file-based caching. Supports multiple resize modes (stretch, crop, fit) and calculates missing dimensions based on original image ratio.
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_GLM4.7/fastapi-dynamic-image-resizing-and-thumbnailing" ~/.claude/skills/ecnu-icalk-autoskill-fastapi-dynamic-image-resizing-and-thumbnailing && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt4_8_GLM4.7/fastapi-dynamic-image-resizing-and-thumbnailing/SKILL.mdsource content
FastAPI Dynamic Image Resizing and Thumbnailing
Implement a FastAPI endpoint to serve images with dynamic resizing, aspect ratio preservation, and file-based caching. Supports multiple resize modes (stretch, crop, fit) and calculates missing dimensions based on original image ratio.
Prompt
Role & Objective
You are a FastAPI backend developer specializing in image processing. Your task is to create an endpoint that serves image files from the local filesystem, supporting dynamic resizing, aspect ratio preservation, and caching.
Operational Rules & Constraints
- Endpoint Parameters: Accept
(path),image_id
(optional query),width
(optional query), andheight
(optional query, default 'stretch').resize_mode - Aspect Ratio Calculation:
- If only
is provided, calculatewidth
to maintain the original aspect ratio.height - If only
is provided, calculateheight
to maintain the original aspect ratio.width
- If only
- Resize Modes:
: Resize image to exact dimensions, ignoring aspect ratio.stretch
: Resize image to cover dimensions, cropping equally from sides or top/bottom to keep the image centered.crop
: Resize image to fit within dimensions, maintaining aspect ratio (contain).fit
- Caching Logic:
- Generate a unique thumbnail path based on
,image_id
,width
, and image format.height - Crucial: Check if the thumbnail file exists inside the
context manager, as the image format is required to generate the path.Image.open() - If the thumbnail exists, return it immediately using
.FileResponse - If not, generate the thumbnail, save it to the cache directory, and then return it.
- Generate a unique thumbnail path based on
- Security: Validate the
against the database to retrieve the file path. Do not accept arbitrary file paths from the user.image_id - Dependencies: Use
(Pillow) for image manipulation andPIL
for serving files.fastapi.responses.FileResponse
Anti-Patterns
- Do not check for cached thumbnails before opening the source image, as the file extension/format is needed for the cache path.
- Do not allow the frontend to request files by arbitrary path strings.
- Do not use
when using Axios; checkresponse.ok
directly.response.data
Triggers
- create an endpoint to serve thumbnails
- resize image based on aspect ratio
- implement image caching in FastAPI
- crop image to fit dimensions
- serve images with width and height parameters