How to use

The parameter $servers_for_file is available to you (which is an array of servers), you should return one of them (or return an empty array if you do not want any of them to be used).

Internally, $servers_for_file looks like this:

$servers_for_file[0] = array('url' => 'http://cdn1.com/image.jpg', 'server' => 'cdn1.com');
$servers_for_file[1] = array('url' => 'http://cdn2.net/image.jpg', 'server' => 'cdn2.net');

Eventually, you should always end your code with something like:

return $servers_for_file[$some_index];

Example

  1. Default behavior (pick the first server found — note that multiple servers can only be found if you're using File Conveyor), one would write:

    return $servers_for_file[0];

  2. If you want to balance the number of files served by each CDN (i.e. on average, each CDN serves the same amount of files on a page) instead of picking the CDN based purely on filetype, one could write:
    $filename = basename($servers_for_file[0]['url']);
    $unique_file_id = hexdec(substr(md5($filename), 0, 5));
    return $servers_for_file[$unique_file_id % count($servers_for_file)];