0.0
The project is in a healthy, maintained state
Image resizing on a fly
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

> 2.0, < 4.0
 Project Readme

Gem Version

Designed to be local simulation of CDN's resize feature

How to use with Rails

group :development do
  gem "rack_resize", require: "rack_resize/rails_autoload"
end

How to use with Rack

use RackResize::RackApp, processor: :mini_magick, assets_folders: ["samples"]
use Rack::Static, urls: [""], root: "samples", index: "index.html" # optional

URL Formats:

Cloudflare format: (can be used with helpers from carrierwave-cloudflare gem)

/cdn-cgi/image/width=426,format=auto/assets/pets/dog.jpg

Fastly / bunny.net format (query string params):

/assets/pets/dog.jpg?width=300&height=200&fit=cover&format=webp&quality=85

Imgproxy format:

/insecure/rs:fill:400:300/q:85/f:webp/plain/local:///assets/pets/dog.jpg
/insecure/rs:fit:200:150/plain/local:///assets/banner.jpg@webp

Supported Parameters:

Parameter Shortcut Imgproxy Option Type Description
width w w:<val> or in rs:<mode>:<w>:<h> integer Target width in pixels. Scales proportionally if height is omitted.
height h h:<val> or in rs:<mode>:<w>:<h> integer Target height in pixels. Scales proportionally if width is omitted.
fit — rs:fill (cover), rs:fit (contain) string Resize mode: contain (default), cover, crop. See below.
format f f:<fmt> or @<fmt> suffix string Output format: jpeg, png, webp, avif, gif. Use auto to keep original.
quality q q:<val> integer (1–100) Compression quality. Defaults to default_quality config value (95).
dpr — dpr:<val> float (0.1–10) Device pixel ratio. Multiplies width and height before processing.
bg-color bg bg:<color> color Background color for flattening transparency. Also aliased as background. See formats below.

Background color formats:

  • CSS named color: white, red, cornflowerblue (all 148 CSS colors supported)
  • 3-digit hex: #a84
  • 6-digit hex: #aa8844
  • 8-digit hex: #aa884480 (last two digits = alpha 0–255)
  • Decimal RGB: 0,255,0
  • Decimal RGBA: 0,255,0,0.5 (alpha 0.0–1.0)

Supported by: vips, mini_magick. Accepted but ignored by sips and imlib2.

Fit modes:

  • contain (rs:fit) — resizes to fit within the given box, preserving aspect ratio (default)
  • cover / crop (rs:fill) — resizes and center-crops to fill the exact box

Example URLs (query string format):

# Resize to width only
/samples/image_1.jpeg?w=400

# Resize to fit within 400×300 box
/samples/image_1.jpeg?width=400&height=300

# Cover-crop to exact 400×300
/samples/image_1.jpeg?width=400&height=300&fit=cover

# Convert to WebP at 80% quality
/samples/image_1.jpeg?w=400&f=webp&q=80

# Retina (2×) resize
/samples/image_1.jpeg?w=200&dpr=2

# Flatten transparency with a background color
/samples/image.png?w=400&f=jpeg&bg-color=white
/samples/image.png?w=400&f=jpeg&bg=%23ff0000
/samples/image.png?w=400&f=jpeg&background=0,255,0,0.5

Example URLs (Cloudflare format):

# Resize to width
/cdn-cgi/image/width=400/samples/image_1.jpeg

# Cover-crop with format conversion
/cdn-cgi/image/width=400,height=300,fit=cover,format=webp/samples/image_1.jpeg

# Quality + format
/cdn-cgi/image/width=400,format=avif,quality=80/samples/image_1.jpeg

Example URLs (Imgproxy format):

# Resize to width and height with cover crop (rs:fill)
/insecure/rs:fill:400:300/plain/local:///samples/image_1.jpeg

# Resize to fit within 400×300 box (rs:fit)
/insecure/rs:fit:400:300/plain/local:///samples/image_1.jpeg

# Convert format and adjust quality
/insecure/rs:fill:400:300/q:85/f:webp/plain/local:///samples/image_1.jpeg

# Specify format via extension suffix (@webp)
/insecure/rs:fit:200:150/plain/samples/banner.jpg@webp

# Using separate width, height, and DPR
/insecure/w:400/h:300/dpr:2/plain/samples/image_1.jpeg

# Flatten transparency with background color
/insecure/rs:fill:200:200/bg:white/plain/samples/logo.png

# Rails fingerprint stripping (fingerprinted URL automatically resolves to original file)
/insecure/w:200/plain/assets/photo-1a2b3c4d.jpg

Configuration:

RackResize.configure do |config|
  config.assets_folders     = { assets: Rails.root.join('app', 'assets', 'images') }
  config.processor          = :sips / :vips / :mini_magick / :imlib2 / :bun_image
  config.default_quality    = 95
  config.save_resized       = false
  config.cache_folder       = Rails.root.join('tmp', 'rack_resize_cache') # used if save_resized enabled
  config.http_cache_max_age = 86400
end

Development vs. Production Architecture:

rack_resize lets you write standard CDN / imgproxy URLs in your application code without needing external services running in development:

[ Local Development ]
Browser Request  -->  Rails / Rack App  -->  RackResize Middleware  -->  Ruby Processor (:vips, :mini_magick, etc.)

[ Production ]
Browser Request  -->  Nginx / CDN Cache  -->  imgproxy (Go service)  -->  Original Image Storage (S3 / disk)
  • In Development: Include rack_resize in your :development Gemfile group. It intercepts /insecure/..., /cdn-cgi/image/..., or query string parameters, processes images on the fly in Ruby, and serves them immediately.
  • In Production: Route image paths directly at your reverse proxy (e.g. Nginx with proxy_cache) to your production imgproxy container or CDN edge, keeping your web application servers focused on dynamic application traffic.

Supported Processing Backends:

processor config system library gems
sips (macOS only) processor: :sips built-in on macOS none
mini_magick processor: :mini_magick

ImageMagick — brew install imagemagick

gem "image_processing"
gem "mini_magick"
vips processor: :vips

libvips — brew install vips

gem "image_processing"
gem "ruby-vips"
imlib2 processor: :imlib2

Imlib2 — brew install imlib2

gem "rszr"
bun_image processor: :bun_image

Bun ≥ 1.3.14 — brew install oven-sh/bun/bun

none (shells out to bun)

Note: fit=cover/fit=crop falls back to stretch (fill) with bun_image — Bun.Image has no crop yet.

Supported Image Formats:

Format sips mini_magick vips imlib2 bun_image
JPEG ✅ ✅ ✅ ✅ ✅
PNG ✅ ✅ ✅ ✅ ✅
GIF ✅ ✅ ✅ ✅ ❌
WebP ❌ ✅ ¹ ✅ ¹ ❌ ✅
AVIF ✅ ² ✅ ³ ✅ ⁴ ❌ ✅ ⁷
HEIC ✅ ✅ ³ ✅ ⁴ ❌ ✅ ⁷
SVG ❌ ✅ ⁵ ✅ ⁶ ❌ ❌

¹ Requires ImageMagick / libvips built with libwebp support (brew install webp) ² macOS 13 (Ventura) or later ³ Requires ImageMagick built with libheif support (brew install libheif) ⁴ Requires libvips built with libheif support (brew install libheif) ⁵ Requires Inkscape or librsvg (brew install librsvg) ⁶ Requires libvips built with librsvg support (brew install librsvg) ⁷ AVIF/HEIC encode is macOS/Windows only in Bun.Image

Performance Benchmarks:

Measured on an Apple M1 Pro with Ruby 4.0.5. Output size: 300×200 px. Run with ruby benchmark/processor_benchmark.rb

JPEG — image_1.jpeg (51 KB)

Processor i/s ms/i vs fastest
imlib2 613.0 1.63 —
vips 216.9 4.61 2.83×
mini_magick 36.4 27.49 16.85×
sips 20.1 49.68 30.45×

PNG — sample.png (2 KB)

Processor i/s ms/i vs fastest
vips 431.1 2.32 —
imlib2 221.7 4.51 1.94×
mini_magick 36.9 27.12 11.69×
sips 19.4 51.62 22.25×