Search
Varnish Cache Plus

Image

Description

The image vmod provides a tool for image modification and optimization.

Example

Convert to WebP

import image;
import headerplus;

sub vcl_backend_response {
  headerplus.init(beresp);
  if (beresp.http.content-type == "image/jpeg" ||
    beresp.http.content-type == "image/png") {
    if (bereq.http.Accept ~ "image/webp") {
      image.webp();
    }
    headerplus.attr_set("Vary", "Accept");
  }
  headerplus.write();
}

API

webp

VOID webp([INT quality = 75], [INT resize_width = 0], [INT resize_height = 0], [BOOL lossless = 0], [INT lossless_level = 6])

Convert an image to the WebP format and store it in cache. Optionally scale, resize or change the quality of conversion. Scaling is done by using only one of resize_width or resize_height. quality and lossless are mutually exclusive options. The supported content types to convert are image/png and image/jpeg. When called this function disables streaming. It should not be re-enabled for proper functionality.

Note: quality is a number between 0 and 100, a lower number will result in a smaller size but lower quality. mode is a number between 0 and 9, a lower number is faster but produces a larger file size.

Arguments:

  • quality accepts type INT with a default value of 75 optional

  • resize_width accepts type INT with a default value of 0 optional

  • resize_height accepts type INT with a default value of 0 optional

  • lossless accepts type BOOL with a default value of 0 optional

  • lossless_level accepts type INT with a default value of 6 optional

Type: Function

Returns: None

Availability

The image VMOD is available in the varnish-plus-vmods-extras package.