Search code examples
phpcdntilesbandwidth

Offload image loading to CDN on the fly


We are running a tile serve which generates images on the fly but caches some to a CDN. When we get a request for a given tile e.g. http://mydomain.com/x/y/z/tile.png, can we choose whether to serve up the local image or to serve up the image on the CDN (if we know there is one cached for the given tile)? And if so, will it be served via our server and thus not actually reduce bandwith?

Note, the client will make a request for an image file and needs to receive an image back. We cannot send the client a url for the CDN and then have the client make a second request.


Solution

  • If you can't trigger a 30x header redirect

    header("Location: http://cdn.example.com/.....");
    

    there is no way to do this. You would have to fetch the image from the CDN on server side and pass it through, rendering the point of the CDN moot.