This page helps you to get started with Cloud CDN image optimization,
explaining how to configure image optimization and list of supported operations
with examples.

## Prerequisites

For Cloud CDN image optimization to work, make sure that the
following prerequisites are met:

- Cloud CDN cache policy must be enabled on the URL route or the
  associated backend service or bucket, and the image optimization feature must
  be active on that URL map route.

- Image optimization is supported for Global External Application Load Balancers
  only. classic Application Load Balancer is not supported.

- Supported HTTP methods:

  - `GET`: performs image transformations and edge caching for `GET` requests.

  - `HEAD`: if the `imop` query parameter is present, returns cached headers
    (including `Content-Length`) on cache hits; on cache misses, validates
    headers, sets `Content-Type` to the target output format, and strips
    origin `Content-Length` without invoking edge transformation or billing.

  - `OPTIONS`: if the `imop` query parameter is present, returns an
    `HTTP 204 No Content` response with `Allow: OPTIONS, GET, HEAD`.

  - Unsupported methods (`POST`, `PUT`, `DELETE`, `PATCH`):
    If the `imop` query parameter is present, Cloud CDN rejects
    requests at the edge and returns an `HTTP 405 Method Not Allowed` error.

- Origin request behavior on cache misses: Cloud CDN sets
  `Accept-Encoding: identity` and removes client `Range` headers when fetching
  pristine images from the origin. This helps ensure complete payloads without
  HTTP compression, such as `gzip` or `brotli`.

- The client request should include an `Accept` header indicating it can receive
  image content, such as `image/webp`, `image/avif`, `image/*`, or `*/*`. If
  the header is absent, `*/*` is assumed by default.

- The origin server must respond with an `HTTP 200` status code
  and a supported image `Content-Type` header.

- The `imop` query parameter must be included in the URL and contain one or more
  valid transformation operations.

### Operation

If the `Content-Type` header is a supported image format, the image is
optimized, cached, and served to the user. Requests without `?imop=` bypass
image optimization and pass through unmodified.

However, if `?imop=` is present but the content type is unsupported, an
`HTTP 415 Unsupported Media Type` error is returned instead of serving the raw
file unmodified (`GET` requests are still billed as transformations).

For unsupported content-encoding, if the origin ignores
`Accept-Encoding: identity` and sends any Content-Encoding header (for example,
`gzip`, `br`, `deflate`, or `identity`), Cloud CDN
aborts the request and returns `HTTP 415 Unsupported Media Type`.

## Configure image optimization

Image optimization requires an active caching enabled. Ensure
that you configure at least one of the following:

- Backend-based caching: make sure the backend has **Enable Cloud CDN** checked.

- URL map-level caching: ensure that a cache policy is configured at the
  [URL map level](https://docs.cloud.google.com/cdn/docs/caching.md.txt#cache-policies-url-maps)
  for the specific path where you plan to enable image optimization.

### gcloud

You can declare your URL map configuration in a YAML file and import it,
as image optimization policies are defined inside the `routeAction` object.

1. Export your existing URL map configuration to a local YAML file:

   ```
   gcloud compute url-maps export YOUR_URL_MAP_NAME \
      --global \
      --destination=my-url-map.yaml
   ```
2. Edit the `my-url-map.yaml` file in a text editor and add the `imageOptimizationPolicy`
   field to the selected `routeAction` or `defaultRouteAction` section:

   ```yaml
   name: image-matcher
   routeRules:
     - description: ImageOptimizationService
       matchRules:
         - prefixMatch: /images/
       priority: 1
       routeAction:
         imageOptimizationPolicy:
           queryParameterInterpretation: ENABLED
   ```
3. Import the updated URL map:

   ```
   gcloud compute url-maps import YOUR_URL_MAP_NAME \
      --global \
      --source=my-url-map.yaml
   ```

   Alternatively, to edit the file interactively in your default text editor,
   use the [`gcloud compute url-maps edit`](https://docs.cloud.google.com/sdk/gcloud/reference/compute/url-maps/edit) command:

   ```
   gcloud compute url-maps edit YOUR_URL_MAP_NAME --global
   ```

### Terraform

Configure image optimization within your `google_compute_url_map` resource:

```terraform
resource "google_compute_url_map" "urlmap" {
  name            = "image-url-map"
  default_service = google_compute_backend_bucket.static.id

  host_rule {
    hosts        = ["images.example.com"]
    path_matcher = "imagematcher"
  }

  path_matcher {
    name            = "imagematcher"
    default_service = google_compute_backend_bucket.static.id

    path_rule {
      paths   = ["/images/*"]
      service = google_compute_backend_bucket.static.id

      route_action {
        image_optimization_policy {
          query_parameter_interpretation = "ENABLED"
        }
      }
    }
  }
}
```

### GKE Gateway API

Configure image optimization on GKE Gateway using a `GCPHTTPFilter`:

1. Create a `GCPHTTPFilter` manifest (`gcp-img-opt-filter.yaml`):

   ```yaml
   apiVersion: networking.gke.io/v1
   kind: GCPHTTPFilter
   metadata:
     name: img-opt-filter
   spec:
     imageOptimizationPolicy:
       queryParameterInterpretation: ENABLED
   ```
2. Attach the filter to your `HTTPRoute` using `extensionRef`:

   ```yaml
   apiVersion: gateway.networking.k8s.io/v1
   kind: HTTPRoute
   metadata:
     name: images-route
   spec:
     parentRefs:
     - kind: Gateway
       name: external-http
     hostnames:
     - "images.example.com"
     rules:
     - matches:
       - path:
           value: /images/
       filters:
       - type: ExtensionRef
         extensionRef:
           group: networking.gke.io
           kind: GCPHTTPFilter
           name: img-opt-filter
       backendRefs:
       - name: images-backend
         port: 8080
   ```

### Console

1. In the Google Cloud console, go to the **Load Balancing** page.

   [Go to the Load balancing page](https://console.cloud.google.com/networking/loadbalancing/list)
2. Click the name of your global external Application Load Balancer.
3. Click **Edit** .
4. Navigate to the **Backend configuration** tab.
5. **Configure caching:** Image optimization requires active caching to save successfully. Choose and configure one of the following, for image optimization to work.
   - **Backend-based:** Ensure **Enable Cloud CDN** is selected in your **Backend Service** configurations.
   - **URL map level:**
     1. Navigate to **Routing rules** and select **Advanced host and path
        rules**.
     2. Define a cache policy at URL map level on the route where you are enabling image optimization.

Depending on your routing architecture, select one of the routing rules:

**To use the default routing rule, do the following:**

1. In the **Host and path rules** section, under **Action** , select **Route traffic to a single backend mode**, and then select a backend from the list.
2. Expand the **Image optimization section** (collapsed by default).
3. Select the **Enable Image optimization** checkbox.

**To use the custom routing rule, do the following:**

1. In the **Host and path rules** section, select **Add host and path rule**.
2. In the **Hosts field**, enter \* to match all hostnames.
3. In the **Path matcher** section, use the YAML editor to define your configuration.
4. Click **Code guidance** below the YAML editor for guidance on the code.
5. Copy the snippet and paste it into the YAML editor.
6. Update the placeholder values.

**Review configurations**

1. Review your configurations.
2. Click **Done** to close the sub-editor.
3. Click **Update** to save your configurations.

## Supported image formats

Cloud CDN image optimization supports a wide range of input formats from your
origin server. While most formats can be used for processing and output,
some formats are Input only and cannot be used as target output formats.

The following image formats are supported from your origin server:

| Format | Content-Type Header | Input/output | Usecase scenario |
|---|---|---|---|
| **gif** | `image/gif` | Input and output | Standard animated/still format. Animated frames can be extracted using the `Freeze` operation. |
| **png** | `image/png` | Input and output | Lossless format, ideal for transparency, logos, and digital art. |
| **webp** | `image/webp` | Input and output | Modern, web-optimized format with both lossy and lossless compression. |
| **jpeg** | `image/jpeg` | Input and output | Standard photographic format. |
| **brunsli** | `image/x-j` | Input and output (only when the origin image is already in this format and `Convert` isn't specified) | High-efficiency lossless JPEG repacking format. |
| **tiff** | `image/tiff` | Input and output (only when the origin image is already in this format and `Convert` isn't specified) | High-quality tag-based raster format. |
| **avif** | `image/avif` | Input and output | High-efficiency next-generation image format. |
| **bmp** | `image/bmp` | **Input only** | Cannot be specified as an output format. Convert to WEBP or JPEG using `?imop=Convert,format=webp`. |
| **ico** | `text/ico`, `image/ico`, `image/icon`, `application/ico`, `image/x-icon`, `image/vnd.microsoft.icon` | **Input only** | Icon container format. Cannot be exported; convert to a supported output format for delivery. |
| **jp2k** | `image/jp2` | **Input only** | JPEG 2000 format. Must be explicitly converted using a `Convert` operation. |
| **heif** | `image/heif` | **Input only** | High Efficiency Image Format. Must be converted before edge delivery. |

> [!NOTE]
> **Note:** If a requested output format is not natively supported, or if the source format is *Input Only* , a `Convert` operation must be explicitly defined in your `imop` query parameter to specify a supported output format. For example: `?imop=Convert,format=webp`.

## Image transformation URL

To perform dynamic image transformations, add the `imop` query
parameter to your image URL. This parameter holds a transformation
string, which consists of one or more operations applied in sequence.

The transformation string uses the following format rules:

- Use a semicolon (`;`) to separate each individual operation.
- Use a comma (`,`) to separate the internal parameters within each individual operation.
- Only one operation of a given type can be included in a single query string.

The baseline syntax uses the following structure:

    ?imop=[OPERATION_1];[OPERATION_2];...[OPERATION_N]

To perform multiple actions on an image simultaneously, append a chained parameter
string to the image URL. For example, to resize an image to *800 pixels* wide,
set the compression quality to 85, and convert the format to `WebP`, append the
parameter string.

For example,

    https://www.example.com/image.jpg?imop=Resize,width=800;Quality,level=85;Convert,format=webp

## Policy structure

The image optimization policy acts as the core configuration wrapper containing
every property relevant to the functionality of the image optimization feature.

| **Name** | **Description** | **Configuration** |
|---|---|---|
| `queryParameterInterpretation` | Enum value that determines whether the query parameter for image optimization will be interpreted in the data plane. | **Required** . If set to **DISABLED** , query parameters for transformation will be ignored by the policy, effectively disabling this feature. If set to **ENABLED**, this feature will be enabled. |

### Policy dependencies and validation

The image optimization policy has a dependency on Cloud CDN caching.
For this feature to work, a **Cache Policy** must be explicitly set
on one of two locations:

- The same URL map route where the Image optimization policy is being
  configured.

- A backend service or backend bucket related to that exact route.

The image optimization policy is applied strictly in conjunction with the
active, underlying Cache Policy.

> [!NOTE]
> **Note:** Configuration of an image optimization policy automatically returns a configuration validation error if a Cache Policy is not actively enabled on either the same route or on its associated backend service or bucket.

## Roles and permissions

To configure and manage the image optimization feature, you must have an
Identity and Access Management (IAM) role that grants permission to modify
load balancer URL maps.

### Predefined roles

The following predefined roles provide the required permissions:

- **Compute Load Balancer Admin** (`roles/compute.loadBalancerAdmin`):
  provides full control over load balancing resources, including attaching
  image optimization policies to URL maps.

- **Compute Network Admin** (`roles/compute.networkAdmin`):
  provides broader networking permissions, including full control over
  load balancing and URL maps.

### Key permissions

If you use custom IAM roles, ensure that they include the
following permissions:

- `compute.urlMaps.update`: required to attach or modify an image optimization policy on a URL map.
- `compute.urlMaps.get` and `compute.urlMaps.list`: required to view URL map configurations.
- `compute.backendServices.update`: required if configuring backend service-level caching.

## Image optimization ordering rules

Operations listed in the `imop` query parameter are processed in a structured
execution pipeline:

1. **`Freeze` and `Scale`**:

   - If `Scale` is specified, it must be the first operation in the query string.

   - If `Freeze` is specified on an animated GIF, animated WebP, or animated
     AVIF/HEIF image, place `Freeze` at the start of the query string (or
     immediately after `Scale` if `Scale` is used) so frame extraction occurs
     before any subsequent geometric or visual filter transformations.
     Placing `Freeze` after `Blur`, `Tone`, or `Sharpen` returns an `HTTP 500`
     error.

2. **Geometric and visual filter operations** (`Crop`, `Rotate`, `Mirror`, `Resize`, `Blur`, `Tone`, and `Sharpen`):

   - These operations are executed sequentially from left to right in the exact order they appear in the query parameter string.
   - Each operation acts on the visual output of the preceding operation.
3. **Output encoding operations** (`Convert`, `Compress`, `Quality`, `Metadata`):

   - Format conversion, compression or numeric quality adjustment, and metadata preservation (including C2PA Content Credentials manifests) are applied during final output encoding.
4. **`Canvas`**:

   - If `Canvas` is specified, it must be the last operation in the query string and is always applied last.

All input assets undergo automatic normalization before Cloud CDN
executes these transformations. Cloud CDN applies EXIF orientation
markers and ICC color profiles directly to the image pixels before it strips
any metadata. This process ensures that coordinate integrity remains accurate
for subsequent structural actions.

> [!NOTE]
> **Note:** Only one operation of a given type is allowed per request.

## What's next

- [Supported operations](https://docs.cloud.google.com/cdn/docs/image-opt-supportedops.md.txt)
- [Limitations](https://docs.cloud.google.com/cdn/docs/image-opt-limitations.md.txt)
- [Image optimization use cases](https://docs.cloud.google.com/cdn/docs/image-opt-use-cases.md.txt)
- [Securing image transformation requests](https://docs.cloud.google.com/cdn/docs/image-opt-security.md.txt)
- [Monitor and troubleshoot](https://docs.cloud.google.com/cdn/docs/image-opt-monitor-troubleshoot.md.txt)