Announcement

  •  » Engine
  •  » Piwigo Personal Plugin for CDN

#1 2022-12-06 06:45:19

AJ Quick
Member
2022-12-06
1

Piwigo Personal Plugin for CDN

I saw a few posts that were rather outdated about using Piwigo with a CDN. It seems the cdnplus plugin had not been updated for a while and was not up to date with Piwigo 11+ as well as no longer handling URLs correctly anymore.

I decided it wouldn't be very difficult to write a very quick and dirty plugin for my installation to re-write my URLs. This should be able to be dropped into your PersonalPlugin.

This is a very simple rewrite that changes your core domain to your new CDN domain on the image links. Since Piwigo creates images on the fly when they don't exist, there is a provision to not send those images to the CDN until after they are generated.

Code:

add_event_handler('get_src_image_url', 'rewrite_images', EVENT_HANDLER_PRIORITY_NEUTRAL+1000);
add_event_handler('get_derivative_url', 'rewrite_images', EVENT_HANDLER_PRIORITY_NEUTRAL+1000);

function rewrite_images($content_orig){
  
  $content = $content_orig;
  if(strpos($content,'upload/') !== FALSE){
    $content = str_replace("../", "", $content);
    $content = str_replace("/piwigo", "", $content);
    if(substr($content,0,1) == '/'){
      $content = substr($content, 1, strlen($content)-1);
    }
    if((substr($content,0,2) == 'i/') || (strpos($content, 'i.php') !== FALSE)){
      return $content_orig;
    }
    $content = '//cdn.domain.com/piwigo/' . $content;
  }
  return $content;
}

On your CDN, you would have it pointed to your domain. You can also have it point directly to the piwigo directory. In that case you would change the last $content line to this:

Code:

$content = '//cdn.domain.com/' . $content;

If your Piwigo installation is in a different folder, simply replace the references to "piwigo" with your installation directory.

Offline

 
  •  » Engine
  •  » Piwigo Personal Plugin for CDN

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact