Manipulating Humans 101

Policies with fallback

This page demonstrates a different behaviour of policies. In the absence of a policy, instead of just failing and returning the original image, we can apply a default set of parameters.


  table io_policies {
    "article-main": "crop=660:438&width=660",
    "article-large": "crop=318:212&width=318",
    "article-medium": "crop=250:166&width=250",
    "thumb": "crop=80:53&width=80",
    "avatar": "crop=1:1&width=150",
  }

  sub vcl_recv {
  #FASTLY recv

    // Return non-image
    if (req.url.path !~ "(?i)\.(?:jpg|png)$" 
      || req.url.path !~ "/images/") {
      return(lookup);
    }

    // Signal to send to IO
    set req.http.X-Fastly-Imageopto-Api = "fastly";

    // Don't alter the request if we're running at 
    // the shield, it has already been rewritten
    if (req.http.Fastly-FF) {
        return (lookup);
    }
    
    // Policies only, but enforce a default
    if (req.url.qs ~ "(?:^|&)policy=([^&]+)") {
      set var.pol = re.group.1;
    }
    set req.url = req.url.path "?" table.lookup(io_policies, 
                  var.pol, "width=1024&height=1024&fit=bounds");

  }
                

  //facepug.io/images/a6/cat-manipulating.jpg?policy=article-main

  //facepug.io/images/a6/cat-manipulating.jpg?policy=something-else

  //facepug.io/images/a6/cat-manipulating.jpg
                
Share this story