A Cookieless, Cache-Friendly Image Proxy in Laravel

A Cookieless, Cache-Friendly Image Proxy in Laravel

Apr 17, 2025 |

9 minutes read

A Cookieless, Cache-Friendly Image Proxy in Laravel

Optimizing Images for Speed with Cloudflare

When it comes to building fast, modern websites, every detail matters, especially how you handle images. Images can make or break your site’s performance, impacting load times, user experience, and even SEO rankings. Services like Cloudflare’s image resizing are fantastic: they dynamically transform and optimize images using URL parameters, offload processing to edge servers, and deliver perfectly sized assets to any device. If you can use Cloudflare’s solution, it’s a no-brainer; go for it!

But what if you’re working on a project where Cloudflare’s image resizing doesn’t quite fit? Maybe you’re managing a multi-tenant platform, storing images in your repository for quick deployment, or prioritizing a smooth local development workflow. That’s the challenge we faced while building a high-performance platform to host multiple sites. Our setup spanning Postgres, SQLite, Rails, and Laravel needed a custom solution. So, we crafted a cookieless, cache-friendly image proxy in Laravel, inspired by Cloudflare’s brilliance, with minimal effort. Here’s how it works and why it’s a game-changer.

The Goal: Fast, Flexible, and Cacheable Images

Our aim was simple yet ambitious: create an image proxy that rivals Cloudflare’s functionality within Laravel. We wanted a system that:

  • Accepts image transformation instructions via URL (think resize, format, quality).
  • Processes and optimizes images on the fly.
  • Delivers cacheable, lightweight images with long-lived headers.
  • Strip cookies to maximize compatibility with Cloudflare’s caching.
  • Integrates seamlessly with Laravel’s routing and controllers.

The result? A proxy that resizes images once, caches them via Cloudflare for up to 30 days, and keeps our server load low. So far, we’ve focused on resizing, format changes, and quality adjustments, but it’s easy to expand as needed.

Setting Up the Proxy Route

In our project, images live in a public directory (e.g., /public/images), making them directly accessible like  highperformancesqlite.com/images/aaron.jpg.

Typically, a web server like Nginx checks this directory first and serves the file if it exists, bypassing Laravel entirely. This is perfect for untransformed, static images, we didn’t want to mess with that.

For transformed images, we introduced a new route:   /images/{options}/{path}.

This structure ensures static requests stay with Nginx while transformation requests hit Laravel. For example, /images/width=80,quality=75/aaron.jpg  tells our proxy to resize aaron.jpg to 80 pixels wide with 75% quality. The {options} part captures transformation instructions, and {path} grabs the file location. It’s clean, intuitive, and mirrors Cloudflare’s URL-based approach.

Transforming Images On-the-Fly

To handle transformations, we leaned on a powerful PHP library that simplifies resizing, format conversion, and quality tweaks. Think of it as a Swiss Army knife for image, it’s fast, flexible, and perfect for Laravel. We pass options like width, height, quality, and format through the URL, and the proxy processes them instantly. For instance:

  • /images/width=80/aaron.jpg scales the image down to 80 pixels wide.
  • /images/format=webp,quality=75/aaron.jpg converts it to WebP at 75% quality.

The beauty? It happens dynamically, and the output is optimized for web delivery, smaller file sizes, faster load times, and happier users.

Boosting Cacheability with Smart Headers

Here’s where the magic happens: caching. We didn’t want our server resizing the same image repeatedly, so we optimized for browser and CDN caching, specifically Cloudflare. By adding the right headers, we ensure images are cached for 30 days:

  • Public: Allows any cache (like Cloudflare) to store the response.
  • Max-age: Sets a 30-day lifespan for browser caching.
  • S-maxage: Fine-tunes caching for CDNs like Cloudflare. 
  • Immutable: Tells browsers the image won’t change, skipping unnecessary checks.

With these headers, the first request triggers a transformation, and subsequent requests pull from Cloudflare’s cache. Your server stays idle, and users get lightning-fast image delivery.

Ditching Cookies for Cloudflare Bliss

One catch with Cloudflare: it won’t cache responses with cookies. Even with perfect cache headers, a stray Set-Cookie header means a cache miss every time. Our fix? A lightweight middleware that strips cookies but only for image proxy requests. This keeps the rest of our app untouched while letting Cloudflare cache images efficiently. The result: a cf-cache-status: HIT on repeat visits, meaning Cloudflare serves the image without bothering our server.

Key Advantages of Laravel for Building an Image Proxy

Key Advantages of Laravel for Building an Image Proxy

Why It’s a Win

This image proxy is a lightweight powerhouse. It trims payload sizes, speeds up page loads, and leverages browser and CDN caching all within Laravel’s ecosystem. It’s perfect for developers who need control over image delivery without relying on external services. Plus, it’s fun to build, combining URL-based controls, real-time transformations, and caching into one tidy package.

Whether you’re optimizing a blog, an e-commerce site, or a multi-tenant platform, this approach delivers. It’s scalable, SEO-friendly (thanks to faster load times), and lets you focus on what matters: creating great content and experiences.

Boost Site Speed with Laravel Image Proxy

The Way Forward

Implementing a cookieless, cache-friendly image proxy in Laravel offers a highly efficient and scalable solution for optimizing images on your website. By dynamically transforming images and leveraging Cloudflare’s caching capabilities, you can drastically improve load times, reduce server strain, and enhance the user experience. This approach not only streamlines the delivery of optimized images but also ensures your website remains fast, SEO-friendly, and reliable, whether you’re managing a simple blog or a complex multi-tenant platform. With minimal effort and maximum performance, it’s a win for developers looking for a seamless, cost-effective image optimization strategy.

Free Consultation

    Gaurang Jadav

    Dynamic and results-driven eCommerce leader with 17 years of experience in developing, managing, and scaling successful online businesses. Proven expertise in driving digital transformation, optimizing operations, and delivering exceptional customer experiences to enhance revenue growth and brand presence. A visionary strategist with a strong track record in leveraging cutting-edge technologies and omnichannel solutions to achieve competitive advantage in global markets.



    MAP_New

    Global Footprints

    Served clients across the globe from38+ countries