Jump to content

Developer filter to generate thumbnails via CloudFlare Image CDN


Alejandro

Recommended Posts

Hi

This came out of recent collaboration with a client. Keep in mind that right now this only works on Business and Enterprise plans.

https://gist.github.com/jreviews/b03d06c8058f1043abf0bae4feafec3d

You can find other dev filters and code snippets here https://gist.github.com/jreviews

Enjoy!

<?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);

  • Like 1
Link to comment
×
×
  • Create New...