Skip to content
Skillv1.0.0

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

by David-Li0406(0) 0 installs
Free
Sign in to install

Free account. Installing gives you the manifest plus copy-paste snippets.

See reviews

About

Imported from David-Li0406/meta-skill-evloving (AutoSkill/SkillBank/ConvSkill/english_gpt4_8/fastapi-dynamic-image-resizing-and-serving/SKILL.md). Install upstream with npx skills add David-Li0406/meta-skill-evloving --skill fastapi-dynamic-image-resizing-and-serving. Copyright stays with the author.

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

  1. Endpoint Parameters: The endpoint must accept image_id (path parameter), width (optional query), height (optional query), and resize_mode (optional query).
  2. Aspect Ratio Calculation: If only width or only height is provided, calculate the missing dimension based on the original image's aspect ratio to maintain proportions.
  3. Resize Modes: Support the following behaviors for resize_mode:
    • stretch (default): Resize to exact dimensions, ignoring aspect ratio.
    • crop: Resize to cover the dimensions, cropping equally from both sides (left/right or top/bottom) to keep the image centered.
    • fit: Resize to fit within the dimensions while maintaining aspect ratio (contain).
  4. Caching: Implement file-based caching for resized images. Before processing, check if a thumbnail file exists (e.g., named image_id__widthxheight.ext). If it exists, serve it directly using FileResponse. If not, generate it, save it, and then serve it.
  5. Dependencies: Use Pillow (PIL) for image manipulation and FileResponse for serving files.
  6. 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 resize_mode parameter if provided.

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

Use it

Copy one of these into your project. Installing also returns the manifest and these snippets.

yaml
targets:
  - https://api.opensmartroute.ai/api/v1/registry/david-li0406-meta-skill-evloving-fastapi-dynamic-image-r-f9ea9c/manifest   # or paste the manifest below

Manifest

An Open Capability Manifest: the router reads it to know what this does, what it costs and when to pick it.

david-li0406-meta-skill-evloving-fastapi-dynamic-image-r-f9ea9c.ocm.jsonjson
{
  "ocm": "1",
  "id": "david-li0406-meta-skill-evloving-fastapi-dynamic-image-r-f9ea9c",
  "kind": "skill",
  "name": "FastAPI Dynamic Image Resizing and Serving",
  "description": "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.",
  "publisher": "David-Li0406",
  "version": "1.0.0",
  "capabilities": {
    "domains": [
      "general"
    ],
    "tags": [
      "skill-md",
      "fastapi",
      "image-processing",
      "thumbnails",
      "pillow",
      "backend",
      "github"
    ],
    "languages": [
      "en"
    ]
  },
  "quality_prior": 0.6,
  "examples": [
    "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."
  ],
  "primary": false,
  "metadata": {
    "source": {
      "provider": "github",
      "repository": "https://github.com/David-Li0406/meta-skill-evloving",
      "path": "AutoSkill/SkillBank/ConvSkill/english_gpt4_8/fastapi-dynamic-image-resizing-and-serving/SKILL.md",
      "ref": "ca3a335628981df10c36e00cb9850df2c247ab9a",
      "url": "https://github.com/David-Li0406/meta-skill-evloving/blob/ca3a335628981df10c36e00cb9850df2c247ab9a/AutoSkill/SkillBank/ConvSkill/english_gpt4_8/fastapi-dynamic-image-resizing-and-serving/SKILL.md",
      "key": "David-Li0406/meta-skill-evloving/AutoSkill/SkillBank/ConvSkill/english_gpt4_8/fastapi-dynamic-image-resizing-and-serving/SKILL.md"
    }
  },
  "instructions": "# FastAPI Dynamic Image Resizing and Serving\n\nImplements 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.\n\n## Prompt\n\n# Role & Objective\nYou 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.\n\n# Operational Rules & Constraints\n1. **Endpoint Parameters**: The endpoint must accept `image",
  "cost": {
    "context_tokens": 549
  }
}

Fetch it by URL: GET /api/v1/registry/david-li0406-meta-skill-evloving-fastapi-dynamic-image-r-f9ea9c/manifest?version=1.0.0

Reviews

Star ratings from people who tried it. One review per account; edit yours any time.

No reviews yet. Install it, try it, and be the first to rate it.