-
-
Save Volnus/35f83f592b44bc7120b0565dc526aa08 to your computer and use it in GitHub Desktop.
Use the WP.com Photon image CDN without installing JetPack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Photon CDN | |
* Version: 1.3 | |
* Description: Use the WP.com Photon image CDN without installing JetPack | |
* Author: Scott Hartley | |
* Credit: Original by Zachary Scott | |
*/ | |
namespace zacscott; | |
/** | |
* Driver class for the plugin. | |
* | |
* @author Zachary Scott <zac@zacscott.net> | |
*/ | |
class PhotonCDN { | |
/** | |
* The max number of image servers WP.com have (at time of writing it is 4) | |
* So the servers as i0.wp.com, i1.wp.com, i2.wp.com, i3.wp.com | |
* Edited to only load one, no reason to add extra dns lookups (Domain sharding is not recommended in an http2 world) | |
* Defult: 3 | |
*/ | |
const MAXSRV = 1; | |
function __construct() { | |
add_action( 'wp_head', array( $this, 'dns_prefetch' ) ); | |
add_action( 'template_redirect', array( $this, 'start_buffering' ), PHP_INT_MAX ); | |
} | |
// Adds the DNS prefetch meta fields for the WP.com servers | |
function dns_prefetch() { | |
for ( $srv = 0; $srv < self::MAXSRV; $srv++ ) : | |
$domain = "i{$srv}.wp.com"; | |
?> | |
<link rel='dns-prefetch' href='//<?php echo esc_attr( $domain ) ?>' /> | |
<?php | |
endfor; | |
} | |
// Start the output buffering | |
function start_buffering() { | |
ob_start( array( $this, 'process_output' ) ); | |
} | |
// Processes the output buffer, replacing all matching images with URLs | |
// Pointing to wp.com | |
function process_output( $buffer ) { | |
// Get the content directory URL minus the http:// | |
$photon_site_url = site_url(); | |
$content_url = content_url(); | |
$content_url = str_replace( 'http://', '', $content_url ); | |
$content_url = str_replace( 'https://', '', $content_url ); | |
$content_url = str_replace( $photon_site_url, '', $content_url ); | |
$content_url = str_replace( '', '', $content_url ); | |
// Replace references to images on our servers with the wp.com CDN | |
// Photon only supports the following image types. | |
return preg_replace_callback( | |
'{'. $content_url .'/.+\.(jpg|jpeg|png|gif)}i', | |
array( $this, 'replace' ), | |
$buffer | |
); | |
} | |
// Replaces a single image URL match | |
function replace( $matches ) { | |
// Grab the parsed image URL | |
$url = isset( $matches[0] ) ? $matches[0] : ''; | |
// Pick a random server | |
srand( crc32( $url ) ); // Best if we always use same server for this image | |
$server = rand( 0, self::MAXSRV-1 ); | |
// Build the wp.com URL, as return as the replacement | |
return "i{$server}.wp.com/{$url}"; | |
} | |
} | |
// Boot | |
new PhotonCDN(); |
this only work on content, but not work images themes inside csss
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's an example BTW: https://i0.wp.com/i264.photobucket.com/albums/ii162/I-So-Cunfooosed/Dancing%20Bananas/Other%20Video%20Games/kirby.gif (Works well on PhotoBucket images)