Adding query parameters for Image Optimization in a VCL snippet


#1

Hello,

I am trying to add two query parameters, as well as modify current two separate params, to enable the image optimizer in certain conditions, and it doesn’t seem like the parameters are being recognized.

Here is the VCL snippet that I am adding to vcl_recv:

if (req.url.ext ~ “(?i)^(gif|png|jpg|jpeg|webp)$”) {
set req.url = regsub(req.url, “w=”, “width=”);
set req.url = regsub(req.url, “h=”, “height=”);
set req.url = querystring.add(req.url, “fit”, “crop”);
set req.url = querystring.add(req.url, “auto”, “webp”);
}

All of my images are already being served with the parameters “w” and “h” (needed for a different image optimizer). As we migrate to Fastly, I would just need to replace these current parameters with the ones recognized by Fastly (“width” and “height”).

Any idea why these conditions are not being picked up by the image optimizer?

Thanks,
stanlee


#2

Hi Stanlee,

Here you go:

if (req.url.ext ~ "(?i)^(gif|png|jpe?g|webp)$") {
  set req.url = regsuball(req.url, "([\?|&])w=", "\1width=");
  set req.url = regsuball(req.url, "([\?|&])h=", "\1height="); 
  set req.url = querystring.set(req.url, "fit", "crop");
  set req.url = querystring.set(req.url, "auto", "webp")
}

If that doesnt work, have you followed the setup instructions?

Thanks
Dom


#3

Thanks Dom! That regex did the trick!