Skip to content
Back to skills

Fastapi Dynamic Image Resizing And Thumbnailing

ASecurity

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.

  • 611 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added November 11, 2026
educationfastapiapidatabasefrontendbackendsecurity

Works with

  • api

Security analysis

A100/100

Scanned November 11, 2026

npx -y skills add ECNU-ICALK/AutoSkill --skill fastapi-dynamic-image-resizing-and-thumbnailing --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Fastapi Dynamic Image Resizing And Thumbnailing?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Fastapi Dynamic Image Resizing And Thumbnailing
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ecnu-icalk-fastapi-dynamic-image-resizing-and-thumbnailing/badge)](https://www.skillsdirectory.com/skills/ecnu-icalk-fastapi-dynamic-image-resizing-and-thumbnailing)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
id: "d7371d41-6f03-49e5-91f0-5333812bb48f"
name: "FastAPI Dynamic Image Resizing and Thumbnailing"
description: "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."
version: "0.1.0"
tags:
  - "fastapi"
  - "image-processing"
  - "thumbnails"
  - "pillow"
  - "caching"
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"
---

# 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
1. **Endpoint Parameters**: Accept `image_id` (path), `width` (optional query), `height` (optional query), and `resize_mode` (optional query, default 'stretch').
2. **Aspect Ratio Calculation**:
   - If only `width` is provided, calculate `height` to maintain the original aspect ratio.
   - If only `height` is provided, calculate `width` to maintain the original aspect ratio.
3. **Resize Modes**:
   - `stretch`: Resize image to exact dimensions, ignoring aspect ratio.
   - `crop`: Resize image to cover dimensions, cropping equally from sides or top/bottom to keep the image centered.
   - `fit`: Resize image to fit within dimensions, maintaining aspect ratio (contain).
4. **Caching Logic**:
   - Generate a unique thumbnail path based on `image_id`, `width`, `height`, and image format.
   - **Crucial**: Check if the thumbnail file exists *inside* the `Image.open()` context manager, as the image format is required to generate the path.
   - If the thumbnail exists, return it immediately using `FileResponse`.
   - If not, generate the thumbnail, save it to the cache directory, and then return it.
5. **Security**: Validate the `image_id` against the database to retrieve the file path. Do not accept arbitrary file paths from the user.
6. **Dependencies**: Use `PIL` (Pillow) for image manipulation and `fastapi.responses.FileResponse` for serving files.

# 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 `response.ok` when using Axios; check `response.data` directly.

## 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

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in education →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.