How MegaOptim Works: SSIM-Based Compression Explained
Most image compression tools take a blunt approach: pick a quality number, compress the image, and hope for the best. The problem is that every image is different. A quality setting of 75 might produce a perfectly sharp photo of a landscape but destroy the fine details in a product shot or architectural rendering. There is no universal “right” quality number.
MegaOptim takes a fundamentally different approach. Instead of guessing, it uses a perceptual quality metric called SSIM to search for the exact quality level where compression savings are maximized and visual fidelity is preserved. The result is smaller files that actually look good.
What Is SSIM?
SSIM stands for Structural Similarity Index Measure. It is a metric developed by researchers at the University of Texas at Austin to quantify how similar two images appear to the human eye. Unlike simple pixel-by-pixel comparison, SSIM evaluates three components that matter for human perception:
- Luminance — how brightness compares between the original and compressed image
- Contrast — how the range of light and dark values compares
- Structure — how patterns, edges, and textures are preserved
SSIM produces a score between 0 and 1, where 1 means the images are perceptually identical. A score above 0.95 is generally indistinguishable from the original to most viewers. A score below 0.85 starts to show noticeable degradation.
The key insight is that SSIM correlates far better with human perception than raw file metrics. Two images can differ significantly at the pixel level yet look identical to the eye, and SSIM captures that distinction.
The Binary Search Approach
Here is where MegaOptim’s approach becomes interesting. Rather than applying a fixed quality parameter, MegaOptim performs a binary search across the quality spectrum to find the sweet spot for each individual image.
The process works like this:
- Start with a quality range. Based on the selected compression level, MegaOptim defines a minimum and maximum quality boundary.
- Compress at the midpoint. The image is compressed at the middle of the current quality range.
- Measure SSIM. The compressed result is compared against the original using SSIM.
- Narrow the range. If the SSIM score is above the target threshold, the search moves toward lower quality (smaller files). If the score is below the threshold, it moves toward higher quality (better fidelity).
- Converge on the optimum. After several iterations, the algorithm arrives at the lowest quality setting that still meets the perceptual quality target.
This means every image gets a compression level tailored to its content. A simple graphic with flat colors can be compressed aggressively because there is little structural detail to lose. A complex photograph with fine textures will receive a gentler compression to preserve those details. The decision is automatic and based on measurable perceptual quality, not guesswork.
Three Compression Levels
MegaOptim offers three compression levels to match different use cases. Each level adjusts the SSIM target and quality boundaries that the binary search operates within.
Ultra
Ultra compression prioritizes the smallest possible file size. The quality boundaries are set aggressively low, and the SSIM threshold allows for more perceptual difference from the original. This is ideal for scenarios where bandwidth savings outweigh pixel-perfect quality — think thumbnails, social media previews, or content served to users on slow mobile connections.
Files compressed with Ultra can be 70-90% smaller than the original, depending on the source image.
Intelligent
Intelligent is the recommended default. It strikes the best balance between file size reduction and visual quality. The binary search targets a quality range where most viewers cannot distinguish the compressed image from the original, while still achieving meaningful size savings.
This is the right choice for most websites, e-commerce product images, and any context where images need to look sharp but load fast. Typical savings range from 40-70% without visible quality loss.
Lossless
Lossless compression preserves maximum quality. For formats that support true lossless encoding, every pixel is retained exactly. For lossy formats, the quality parameters are set high enough that the SSIM score remains extremely close to 1.0, meaning no human-perceptible difference from the original.
This mode is designed for photography portfolios, medical imaging, print preparation, and any use case where absolute fidelity is non-negotiable. File size savings are smaller — typically 10-30% — but the image is visually identical to the source.
Format-Specific Strategies
SSIM-based optimization works brilliantly for lossy formats where quality is a continuous spectrum. However, not every image format compresses the same way, and MegaOptim adapts its strategy accordingly.
Lossy Formats: JPEG, WebP, AVIF
For JPEG, WebP, and AVIF, MegaOptim uses the full SSIM binary search described above. These formats have a quality parameter that directly controls the trade-off between file size and visual fidelity, making them ideal candidates for perceptual optimization.
AVIF in particular benefits from this approach because it offers superior compression efficiency at equivalent SSIM scores compared to JPEG and WebP. If you are considering which format to adopt, see our guide to choosing the right image format for a detailed comparison.
PNG
PNG is a lossless format by nature, so the quality parameter does not apply in the same way. Instead, MegaOptim uses specialized tools like optipng and pngquant to reduce file size. Optipng optimizes the compression filters and encoding without any quality loss. Pngquant reduces the color palette (lossy) to achieve more dramatic size reductions when the Ultra or Intelligent levels are selected.
GIF
For GIF files, MegaOptim relies on gifsicle, a tool purpose-built for GIF optimization. It reduces color tables, optimizes frame data, and removes redundant information. Animated GIFs skip the SSIM binary search because the frame-by-frame nature of animation requires a different optimization path.
For a deeper understanding of when lossy and lossless approaches each make sense, see our article on lossy vs. lossless compression.
Two Modes of Operation
MegaOptim supports two distinct processing modes:
Optimize Mode
Optimize mode compresses an image while keeping it in the same format. A JPEG stays a JPEG. A WebP stays a WebP. This is the standard use case for reducing file sizes across an existing image library without changing your markup or delivery infrastructure.
Convert Mode
Convert mode changes the image format during compression — for example, converting a JPEG to WebP or an older PNG to AVIF. This unlocks the compression advantages of modern formats while still applying SSIM-based quality optimization to the output. Convert mode is particularly powerful when migrating a site to next-generation formats for better Core Web Vitals scores.
Why This Matters for Performance
The difference between blind compression and SSIM-guided compression compounds across a website. A typical page might load 20-40 images. If each image is 15-30% smaller than what a fixed-quality approach would produce — without any visible quality difference — the cumulative bandwidth savings translate directly into faster load times, lower hosting costs, and better user experience.
Search engines factor page speed into rankings. Users abandon slow pages. Every unnecessary kilobyte of image data is a cost with no benefit. SSIM-based compression eliminates that waste systematically.
Get Started
MegaOptim’s SSIM-based compression is available through our REST API for seamless integration into any workflow, and through our WordPress plugin for one-click optimization of your entire media library. Upload an image, choose your compression level, and let the binary search find the perfect balance for you.