Last active
July 29, 2020 16:08
-
-
Save jreviews/b03d06c8058f1043abf0bae4feafec3d to your computer and use it in GitHub Desktop.
Developer filter to generate thumbnails with the CloudFlare Image CDN
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 | |
defined('MVC_FRAMEWORK') or die; | |
/** | |
* Re-writes image URLs to the CloudFlare Image CDN | |
* https://developers.cloudflare.com/images/about/ | |
*/ | |
function jreviews_cloudflare_cdn_thumbnails ($attr, $params = []) | |
{ | |
// Skip if thumbnails are not yet generated | |
if ( | |
false !== strpos($attr['src'], 'admin-ajax.php') | |
|| | |
false !== strpos($attr['src'], 'com_jreviews') | |
) { | |
return $attr; | |
} | |
if ( false === strpos($attr['src'], 'thumbnail') ) | |
{ | |
return $attr; | |
} | |
$width = !empty($attr['width']) ? $attr['width'] : 640; | |
$attr['src'] = str_replace(WWW_ROOT, WWW_ROOT."cdn-cgi/image/width={$width},quality=100,metadata=copyright,f=auto,fit=scale-down/", $attr['src']); | |
// Replace '/thumbnail/' with '/original/' and remove thumbnail size | |
$attr['src'] = preg_replace('/\/thumbnail\/[\s\S]+?\//', '/original/', $attr['src']); | |
return $attr; | |
} | |
Clickfwd\Hook\Filter::add('image_attributes_before_render', 'jreviews_cloudflare_cdn_thumbnails', $priority = 5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The filter allows modifying the local stored image URLs to load them from a different domain when using a CDN (e.g. CloudFlare)
To use this filter: