Proxying images to a remote host on Laravel Valet

In an earlier post, I talked about loading images by proxy using NGINX directives which can be a great technique to avoid having to download media libraries every time you sync a live site to your local machine which saves you both time and disk space.

In another article where I talked about using Laravel Valet for WordPress development, I touched on the ability to proxy images using PHP-based local drivers and thought I should share a bit of code around just that.

Laravel Valet’s Local Driver

While getting familiar with Valet, I learned of its built-in driver system for supporting various frameworks and also learned of the WordPressValetDriver built right into the Valet package. These drivers are basically responsible for providing support for the file structure of various frameworks.

Especially interesting to me was the notion of local drivers. Simply put, by adding a LocalValetDriver class with the same filename to the root of your project, you can take control of how Valet serves your project.

Using Local Drivers to Proxy Images From Production

After some experimentation, I found a way to load any images that were missing from the local uploads directory from a remote host – in my case, this is the live site.

Drop the following class into your project’s root directory, change out the REMOTE_HOST constant, and make sure you name the file LocalValetDriver.php. This should just work.

<?php
/**
* Class LocalValetDriver
*
* This class demonstrates how we might go about proxying any missing local images to a remote host. i.e; the production
* site. This has been created with WordPress in mind but could be adjusted to work with any other system.
*/
class LocalValetDriver extends WordPressValetDriver {
/** @var string The remote host to proxy requests to */
const REMOTE_HOST = 'https://remotehost.com/';
/** @var string If the request URI starts with this, we want to proxy the request to the remote host */
const URI_PREFIX = '/wp-content/uploads/';
/** @var bool Whether or not to load the current request remotely */
private static $tryRemoteFallback = false;
/**
* This method checks if we have the file on disk. If not, changes the domain of any requests for files within the
* uploads directory to the remote domain. It also sets a flag that this request is now a remote request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
*
* @return bool|false|string
*/
public function isStaticFile( $sitePath, $siteName, $uri ) {
$localFileFound = parent::isStaticFile( $sitePath, $siteName, $uri );
if ( $localFileFound ) {
return $localFileFound;
}
if ( self::stringStartsWith( $uri, self::URI_PREFIX ) ) {
self::$tryRemoteFallback = true;
return rtrim( self::REMOTE_HOST, '/' ) . $uri;
}
return false;
}
/**
* This method checks if the remote flag is set and, if so, redirects the request by setting the Location header.
*
* @param string $staticFilePath
* @param string $sitePath
* @param string $siteName
* @param string $uri
*/
public function serveStaticFile( $staticFilePath, $sitePath, $siteName, $uri ) {
if ( self::$tryRemoteFallback ) {
header( "Location: $staticFilePath" );
} else {
parent::serveStaticFile( $staticFilePath, $sitePath, $siteName, $uri );
}
}
/**
* @param string $string
* @param string $startsWith
*
* @return bool
*/
private static function stringStartsWith( $string, $startsWith ) {
return strpos( $string, $startsWith ) === 0;
}
}

Need a website? Let's talk.

From website design & SEO through to custom WordPress plugin development. I transform ideas into dynamic, engaging, and high-performing solutions.
Subscribe to get the latest insights & updates in the world of web and how it impacts your business and website.
© 2024 Phil Kurth  |  All rights reserved.