Image Transformations
Image transformations let you serve optimized versions of your images automatically. Instead of manually creating multiple sizes, Pixelflare generates them on-demand and caches them at the edge.
Why transformations matter
Serving the right image size saves bandwidth, speeds up your site, and improves user experience. A mobile device doesn't need a 4K desktop image. Transformations handle this automatically:
- Bandwidth savings: A 512px image is 80-90% smaller than a 2048px original
- Faster page loads: Smaller images mean quicker downloads
- Modern formats: Automatic conversion to AVIF or WebP based on browser support
- No manual work: Request any size via URL, generated on first use
- Edge caching: Transformed images cached globally for instant delivery
Pixelflare includes 9 built-in presets: w128, w256, w512, w1024, w1536, w2048 (width-constrained), thumb (128x128 square), og-image (1200x630 social cards), and original (unmodified). All variants use quality 85, auto-negotiate format (AVIF/WebP), and strip EXIF metadata by default.
Setup
Transformations need one environment variable, a public custom domain for your R2 bucket:
R2_CUSTOM_DOMAIN='r2.yourdomain.com'The presets themselves are set in code, see Adding Custom Presets.
R2 Custom Domain
Transformations require a public custom domain on your R2 bucket:
- Go to R2 bucket settings in Cloudflare dashboard
- Add custom domain (e.g.,
r2.yourdomain.com) - Configure DNS as instructed
- Set
R2_CUSTOM_DOMAINin your config
Without this, Pixelflare can't resize or convert images, so it serves the originals instead.
Adding Custom Presets
To add new variants, edit packages/config/src/image-variants.ts and add your preset to VARIANT_PRESETS and VARIANT_CONFIG, then rebuild and deploy.
How it Works
When a user requests an image variant, Pixelflare checks if it's already been generated. If so, it's served straight from R2. If not, Cloudflare Image Resizing transforms it on demand, and Pixelflare serves it immediately, then saves it to R2 for next time. The variant queue then records it against the image, so it shows up in the web app.
sequenceDiagram
participant Client
participant Gateway as Gateway Worker
participant API as API Worker
participant D1 as D1 Database
participant R2 as R2 Storage
participant CF as Cloudflare Image Resizing
participant Queue as Queue (Background)
Client->>Gateway: GET /alice/vacation/beach.jpg/w512
Gateway->>API: Route to CDN handler
API->>D1: Check image exists, privacy, deleted
alt Image not found or deleted
D1-->>API: Not found
API-->>Client: 404 or 410
end
API->>R2: Try fetch pre-generated variant
alt Variant exists in R2
R2-->>API: Return variant
API-->>Client: 200 with transformed image<br/>(Cache-Control: immutable, 1 year)
else Variant not generated yet
R2-->>API: Not found
API->>R2: Fetch original image
R2-->>API: Original image data
API->>CF: Transform on-demand<br/>(via cf.image fetch option)
CF-->>API: Transformed image
API-->>Client: 200 with transformed image
API->>R2: Store variant for next time
API->>Queue: Record the new variant
endCaching strategy
Images use multi-layer caching for performance:
- Edge cache: Cloudflare caches all images globally with 1-year TTL (immutable URLs)
- R2 storage: Pre-generated variants stored permanently until image deletion
- Rate limiting: KV-based limits prevent abuse of on-demand generation (1,000 new variants per minute per IP)
Cache invalidation happens automatically when you delete an image via the Cloudflare API (requires CLOUDFLARE_ZONE_ID and CLOUDFLARE_API_TOKEN).
Pricing
Transformations use Cloudflare Images, which is billed per unique transformation each month. The same transformation requested again in the same month only counts once, and format and quality changes don't count as new ones.
- Free plan: 5,000 unique transformations a month. After that, new transformations fail until the next month, but you're never charged
- Paid plans: 5,000 included, then $0.50 per 1,000
Since each variant is saved to R2 after its first request, most traffic is served from storage rather than transformed again. See Cloudflare's Images pricing for the latest.
Customization
Custom presets can configure width, height, fit mode (scale-down, cover, contain), format (AVIF, WebP, JPEG), quality (1-100), gravity for smart cropping, and various effects. Edit packages/config/src/image-variants.ts to add your own presets with specific dimensions and options. Cloudflare can transform images up to 12,000px on the longest side, and 70 MB.
Security & Performance
Built-in protections
- Rate limiting: On-demand generation is limited to 1,000 new variants per minute per IP, and custom transforms to 10 per minute (already generated variants are unlimited)
- Privacy: Private images require authentication (API key, JWT, or owner verification)
- Immutable caching: 1-year TTL on images with automatic edge caching across 300+ Cloudflare locations
- Format negotiation: Automatic AVIF/WebP serving based on browser support
- Lazy generation: Variants only created when requested, not on upload
- CORS enabled: Images embeddable anywhere with optimized preflight caching
Monitoring
Analytics Engine tracks variant popularity, bandwidth savings, cache hit rates, and geographic distribution. Use this data to optimise your preset configuration.