Skip to main content

What is Auto Compression?

Auto compression automatically reduces image file sizes during upload while maintaining visual quality. This saves storage space and makes images load faster for your users.
Compression is optional and can be configured in your Dashboard Settings.

How It Works

When you upload an image with auto-compression enabled:
1

Upload Detection

Imperial receives your image and detects its format (JPEG, PNG, WebP)
2

Quality Analysis

The image is analyzed to determine the optimal compression settings
3

Smart Compression

Using Sharp (high-performance image processing), the image is recompressed with your quality setting
4

Size Verification

If compression makes the file larger (rare), the original is kept instead
5

Storage & Delivery

The optimized image is stored and delivered via CDN

Supported Formats

Best for: Photos, complex images with gradients
  • Default quality: 80%
  • Typical savings: 40-60%
  • Maintains color accuracy
  • Uses optimized encoding (Progressive JPEG)
Original: cat.jpg (2.4 MB)
Compressed: cat.jpg (960 KB) - 60% smaller ✨
Videos are never compressed to preserve quality and avoid long processing times. Video compression is complex and best done before upload with tools like FFmpeg.

Quality Settings

You can customize the compression quality in your dashboard:
QualityFile SizeVisual QualityBest For
60%SmallestNoticeable lossThumbnails, previews
70%Very SmallSlight lossWeb images, social media
80%BalancedMinimal lossDefault, recommended
90%LargerNearly perfectProfessional photography
100%LargestPerfectOriginal archival
80% quality is the sweet spot - most users can’t tell the difference from 100%, but files are 40-60% smaller.

Technical Implementation

Imperial uses Sharp, the industry-standard high-performance image processing library:

Why Sharp?

Fast Processing

4-5x faster than ImageMagickUses libvips for efficient memory usage

High Quality

Professional-grade algorithmsMaintains color profiles and metadata

Memory Efficient

Streams data instead of loading full imagesCan process images larger than RAM

Modern Formats

Supports WebP, AVIF, JPEG XLFuture-proof for new formats

Compression Algorithm

// Simplified version of Imperial's compression logic
async function compressImage(buffer, mimeType, quality = 80) {
  const sharpInstance = sharp(buffer);
  
  switch (mimeType) {
    case 'image/jpeg':
      return await sharpInstance
        .jpeg({
          quality,
          progressive: true,        // Better for web loading
          optimizeScans: true,      // Optimal scan compression
          chromaSubsampling: '4:2:0' // Standard subsampling
        })
        .toBuffer();
    
    case 'image/png':
      return await sharpInstance
        .png({
          quality,
          compressionLevel: 9,      // Maximum compression
          adaptiveFiltering: true,  // Better compression
          palette: true             // Use indexed color if possible
        })
        .toBuffer();
    
    case 'image/webp':
      return await sharpInstance
        .webp({
          quality,
          effort: 4,                // Balance speed vs compression
          smartSubsample: true      // Better quality at edges
        })
        .toBuffer();
  }
}

Real-World Examples

Example 1: Product Photos

Original JPEG (iPhone 14 Pro Max):
├─ Resolution: 4032 × 3024
├─ File size: 3.8 MB
└─ Quality: 100%

After 80% Compression:
├─ Resolution: 4032 × 3024 (unchanged)
├─ File size: 1.4 MB (63% smaller)
└─ Quality: Visually identical

Example 2: Screenshots

Original PNG (4K monitor):
├─ Resolution: 3840 × 2160
├─ File size: 2.2 MB
└─ Colors: 16.7 million

After Compression:
├─ Resolution: 3840 × 2160 (unchanged)
├─ File size: 1.6 MB (27% smaller)
└─ Colors: Optimized palette

Example 3: Web Graphics

Original WebP (design export):
├─ Resolution: 1920 × 1080
├─ File size: 890 KB
└─ Transparency: Yes

After 80% Compression:
├─ Resolution: 1920 × 1080 (unchanged)
├─ File size: 310 KB (65% smaller)
└─ Transparency: Preserved

When to Use Compression

  • Web Images: Photos, banners, backgrounds
  • Social Media: Posts, stories, profile pictures
  • App Assets: Icons, splash screens, UI elements
  • User Uploads: Profile pictures, content submissions
  • Large Galleries: Photo albums, portfolios
  • Print Materials: Need maximum quality
  • Professional Photography: Client deliverables
  • Medical/Scientific: Requires perfect accuracy
  • Already Optimized: Files that are already small
  • Videos: Use dedicated video encoders instead

Storage Savings Calculator

Per-Image Savings

1 image at 80% compression:
├─ Original: 2.5 MB
├─ Compressed: 1.0 MB
└─ Saved: 1.5 MB (60%)

100 images:
└─ Saved: 150 MB

1,000 images:
└─ Saved: 1.5 GB

10,000 images:
└─ Saved: 15 GB ✨

Cost Savings

With Imperial’s pricing:
Starter Plan (5 GB):
├─ Without compression: ~2,000 images
└─ With compression: ~5,000 images (2.5x more!)

Pro Plan (10 GB):
├─ Without compression: ~4,000 images
└─ With compression: ~10,000 images
Compression effectively 2-3x your storage capacity without any extra cost!

Performance Impact

Upload Processing Time

Compression adds minimal overhead:
Image SizeWithout CompressionWith CompressionOverhead
1 MB200ms250ms+50ms
5 MB800ms950ms+150ms
10 MB1,500ms1,800ms+300ms
The slight processing delay is offset by faster uploads to R2 (smaller files) and faster CDN delivery to users.

Configuration

Enable compression in your dashboard:
1

Navigate to Settings

Go to Dashboard Settings → Uploads
2

Enable Auto-Compress

Toggle “Auto-compress images” on
3

Set Quality

Choose your quality level (60-100%)Recommended: 80%
4

Save Changes

Your preference applies to all future uploads

API Usage

Compression settings are account-level and apply automatically to API uploads:
# Uploads automatically compressed based on your settings
curl -X POST https://api.imperial.gay/images/upload \
  -H "Authorization: Bearer imperial_live_xxxxxxxxxxxxx" \
  -F "[email protected]"

# Response includes compression info
{
  "compressed": true,
  "originalSize": 2500000,
  "compressedSize": 1000000
}

Quality Comparison

Visual Examples

  • File Size: 2.4 MB
  • Quality: Perfect, original
  • Use Case: Archival, print

Advanced Features

Progressive JPEG

All compressed JPEGs use progressive encoding:
Baseline JPEG:
└─ Loads top-to-bottom
└─ Slow on poor connections

Progressive JPEG:
├─ Loads in increasing quality
├─ Shows full image immediately (low quality)
└─ Refines to full quality
└─ Better perceived performance

Metadata Preservation

Imperial preserves important metadata:
  • EXIF: Camera info, date taken (optional)
  • Color Profile: sRGB, Display P3
  • Orientation: Proper rotation
But strips:
  • GPS Data: For privacy
  • Thumbnail: Redundant, increases size

Disable Compression

Need original quality? Simply disable compression in settings:
Disabling compression means:
  • Files are stored at original size
  • Uses more of your storage quota
  • May load slower for end users
  • Costs remain the same per GB

Get Started

Try compression and see the savings:

Upload and Compare

Upload an image to see compression in action