> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gcore.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Change image quality

> Reduce CDN image quality with Gcore Image Stack presets or URL query parameters for JPG and PNG delivery.

## Customer Portal

The Quality feature reduces the quality of images delivered through the CDN. It works in combination with at least one other Image Stack operation — such as Compression (WebP or AVIF), Resize, or Crop — and cannot run independently. The origin retains the original image; only the version delivered to end users is affected.

These four pictures show the same image at different quality settings. The visual difference is subtle for detailed or non-contrasted images, and more noticeable for images with large solid areas against contrasting backgrounds.

> [tab] High (95%)
>
> ![Example of high image quality](https://mintcdn.com/gcore/-ze7QZWmeaPWuT_U/images/docs/cdn/cdn-resource-options/image-optimization-paid/image-stack-tools/change-image-quality/11030503003793.png?fit=max\&auto=format\&n=-ze7QZWmeaPWuT_U\&q=85\&s=afd7464442697f179eb297965852210d)

> [tab] Medium (80%)
>
> ![Example of medium image quality](https://mintcdn.com/gcore/-ze7QZWmeaPWuT_U/images/docs/cdn/cdn-resource-options/image-optimization-paid/image-stack-tools/change-image-quality/11030503010449.png?fit=max\&auto=format\&n=-ze7QZWmeaPWuT_U\&q=85\&s=9b88899b3c23e223d516ab41441495ab)

> [tab] Low (65%)
>
> ![Example of low image quality](https://mintcdn.com/gcore/-ze7QZWmeaPWuT_U/images/docs/cdn/cdn-resource-options/image-optimization-paid/image-stack-tools/change-image-quality/11030503150481.png?fit=max\&auto=format\&n=-ze7QZWmeaPWuT_U\&q=85\&s=17acfe0708a5d998730068d5b4c8e1c0)

> [tab] Custom value (50%)
>
> ![Example of custom value image quality](https://mintcdn.com/gcore/-ze7QZWmeaPWuT_U/images/docs/cdn/cdn-resource-options/image-optimization-paid/image-stack-tools/change-image-quality/11030503151121.png?fit=max\&auto=format\&n=-ze7QZWmeaPWuT_U\&q=85\&s=5f7179bc6e7cc487de3928ad95190759)

## Enable quality

Quality is included in the paid Image Stack option. To use it, enable Image Stack for the CDN resource with the [Enable Image stack](https://docs.gcore.com/cdn/cdn-resource-options/image-optimization-paid/enable-image-stack.md) guide.

## Use quality

Quality can be configured in two ways: via the Gcore Customer Portal to apply a default level to all images matching the rule, or via query parameters to set quality per individual image URL.

### Use quality in the customer portal

**1. Open the Image optimization rule**

Navigate to the settings of the required CDN resource. Click the **Rules** tab, then open the **Image optimization** rule.

![CDN resource settings showing OPTIONS and RULES tabs with Create rule button and Image optimization template in the dropdown](https://mintcdn.com/gcore/sDdV0UvuxgNPxJ34/images/docs/cdn/cdn-resource-options/image-optimization-paid/image-stack-tools/change-image-quality/11774830851473.png?fit=max\&auto=format\&n=sDdV0UvuxgNPxJ34\&q=85\&s=b5b4a56b4b6a3d61b61fab463e67f1fc)

**2. Select the quality level**

In the **Image Stack** section, select one of the built-in presets: **High (95%)**, **Medium (80%)**, **Low (65%)**, or **Custom value**. The default is **High (95%)**.

To prevent quality reduction for PNG images, enable **Enable lossless for PNG**. When enabled, the quality setting applies only to JPG images.

> [!NOTE]
>
> The quality value cannot exceed 100%.

![Image Stack section showing Image quality radio buttons with High selected and Enable lossless for PNG toggle](https://mintcdn.com/gcore/sDdV0UvuxgNPxJ34/images/docs/cdn/cdn-resource-options/image-optimization-paid/image-stack-tools/change-image-quality/11030503278097.png?fit=max\&auto=format\&n=sDdV0UvuxgNPxJ34\&q=85\&s=338ed451e5c5a90df02a84a61d0fcf48)

**3. Save the rule**

Click **Save changes** at the top right corner of the page. Saving the Image Stack rule clears the resource cache, and CDN servers request the images from the origin. Enable [Origin Shielding](https://docs.gcore.com/cdn/cdn-resource-options/general/enable-and-configure-origin-shielding.md) first, or save the change during low-traffic hours.

### Use quality via query parameters

Query parameters set quality for individual images by modifying the URL in the website source.

1\. Open the website source code.

2\. Find the URLs of the images whose quality needs to change.

3\. Add the `quality` query parameter to each URL alongside at least one other Image Stack parameter:

```
image.jpg?quality=value&tool2=value2…
```

where:

* `value` is a number between 1 and 100
* `tool2=value2` represents query parameters for any other Image Stack function such as [Compression](https://docs.gcore.com/cdn/cdn-resource-options/image-optimization-paid/image-stack-tools/configure-image-compression-to-webp-and-avif.md), [Resize](https://docs.gcore.com/cdn/cdn-resource-options/image-optimization-paid/image-stack-tools/reduce-image-size.md), or [Crop](https://docs.gcore.com/cdn/cdn-resource-options/image-optimization-paid/image-stack-tools/crop-images.md)

To reduce quality to 76%, convert to AVIF, and crop a 200×200 px area from the center:

```
image.jpg?quality=76&fmt=avif&crop=200,200
```

The same image processed with different `quality` and `fmt` combinations:

> [tab] Before
>
> * Link: img.jpg
> * Size: 813 KB
> * Quality: 100%
>
> ![Original image before processing](https://mintcdn.com/gcore/jGvvbKmzzlO7ynHV/images/docs/cdn/cdn-resource-options/image-optimization-paid/image-stack-tools/change-image-quality/11030480618769.png?fit=max\&auto=format\&n=jGvvbKmzzlO7ynHV\&q=85\&s=f5be938dba9afc1b224fa20e00c89aee)

> [tab] After WebP compression
>
> * Link: img.jpg?quality=95\&fmt=webp
> * Size: 97.4 KB
> * Quality: 95%
>
> ![Image after WebP compression at 95% quality](https://mintcdn.com/gcore/jGvvbKmzzlO7ynHV/images/docs/cdn/cdn-resource-options/image-optimization-paid/image-stack-tools/change-image-quality/11030480610193.png?fit=max\&auto=format\&n=jGvvbKmzzlO7ynHV\&q=85\&s=2dcc97c1e60c85414a18b8633635f222)

> [tab] After AVIF compression 50%
>
> * Link: img.jpg?quality=50\&fmt=avif
> * Size: 97.4 KB
> * Quality: 48% (actual measured quality; AVIF encoding maps the requested value non-linearly)
>
> ![Image after AVIF compression at 50% quality](https://mintcdn.com/gcore/-ze7QZWmeaPWuT_U/images/docs/cdn/cdn-resource-options/image-optimization-paid/image-stack-tools/change-image-quality/11030480735249.png?fit=max\&auto=format\&n=-ze7QZWmeaPWuT_U\&q=85\&s=9e984f9620bbb51b023d4c981f1de722)

> [tab] After AVIF compression 10%
>
> * Link: img.jpg?quality=10\&fmt=avif\&crop=300,600
> * Size: 18.5 KB
> * Quality: 10%
>
> ![Image after AVIF compression at 10% quality](https://mintcdn.com/gcore/-ze7QZWmeaPWuT_U/images/docs/cdn/cdn-resource-options/image-optimization-paid/image-stack-tools/change-image-quality/11030480735505.png?fit=max\&auto=format\&n=-ze7QZWmeaPWuT_U\&q=85\&s=e8ac5eaf6c578d21b17ead7174736cf2)

4\. Save the changes in the website source code.

## Check HTTP headers

After processing, the image URL and file extension remain unchanged. Use the HTTP response headers to verify that quality conversion was applied.

The `X-Img-Operations` header lists all conversions performed. If the value includes `convert`, the image quality was changed.

![Browser DevTools Response Headers panel showing X-Img-Operations header with value convert highlighted](https://mintcdn.com/gcore/jGvvbKmzzlO7ynHV/images/docs/cdn/cdn-resource-options/image-optimization-paid/image-stack-tools/change-image-quality/11030503623825.png?fit=max\&auto=format\&n=jGvvbKmzzlO7ynHV\&q=85\&s=337ab8a217423659c3b75a0746be2c4a)

If `X-Img-Operations` does not contain `convert` and the CDN returns the original-quality image, check the `Img-Skip-Reason` header. It explains why the operation was skipped. The value `converted image bigger than origin` appears when the configured quality value is higher than the original image quality. In this case, combine Quality with another Image Stack feature.

![Browser DevTools Response Headers panel showing Img-Skip-Reason header](https://mintcdn.com/gcore/jGvvbKmzzlO7ynHV/images/docs/cdn/cdn-resource-options/image-optimization-paid/image-stack-tools/change-image-quality/11030503627153.png?fit=max\&auto=format\&n=jGvvbKmzzlO7ynHV\&q=85\&s=16b9786e548440dc59d0fc23945e5a6e)

## REST API

Image quality sets how much detail Gcore CDN keeps in a delivered picture. A lower value makes the file smaller. The level is the `quality` field on Image Stack for images that match a rule, or a `quality` query parameter on one image URL. Saving the rule clears the resource cache, including a later change to `quality`, and CDN servers request the images from the origin. Enable [Origin Shielding](https://docs.gcore.com/cdn/cdn-resource-options/general/enable-and-configure-origin-shielding.md) first, or make the change during low-traffic hours.

> [!NOTE]
>
> An [API token](https://docs.gcore.com/account-settings/api-tokens.md) is required. The CDN resource ID is the `id` from the [resources list](https://docs.gcore.com/api-reference/cdn/cdn-resources/list-cdn-resources) call.

```bash theme={null}
export GCORE_API_KEY="{YOUR_API_KEY}"
export CDN_RESOURCE_ID="{YOUR_CDN_RESOURCE_ID}"
```

## Set quality for image paths

A rule stores the quality level for paths that match `/.*\.(jpg|jpeg|png)`. `ruleType` 0 is a regular expression and must start with `^/` or `/`.

`enabled` is required. This call sets `quality` to 80 and turns WebP on. AVIF and `png_lossless` stay off.

A field left out of the request is stored as its default, so send every field that should stay set. The JSON field is `image_stack`; `imageStack` is ignored.

`quality` accepts integers from 1 to 100. Other values return HTTP 400. The API can store `quality` without WebP or AVIF, but quality changes the delivered image only when WebP, AVIF, resize, or crop is also enabled.

> [tab] Python SDK
>
> ```python theme={null}
> import os
>
> from gcore import Gcore
>
> client = Gcore()
> resource_id = int(os.environ["CDN_RESOURCE_ID"])
>
> rule = client.cdn.cdn_resources.rules.create(
>     resource_id,
>     name="image-quality",
>     rule="/.*\\.(jpg|jpeg|png)",
>     rule_type=0,
>     options={
>         "image_stack": {
>             "enabled": True,
>             "quality": 80,
>             "webp_enabled": True,
>             "avif_enabled": False,
>             "png_lossless": False,
>         }
>     },
> )
> saved = rule.options.image_stack
> print(rule.id, saved.enabled, saved.quality, saved.webp_enabled, saved.avif_enabled, saved.png_lossless)
> ```

> [tab] Go SDK
>
> ```go theme={null}
> package main
>
> import (
>     "context"
>     "fmt"
>     "os"
>     "strconv"
>
>     gcore "github.com/G-Core/gcore-go"
>     "github.com/G-Core/gcore-go/cdn"
> )
>
> func main() {
>     client := gcore.NewClient()
>     ctx := context.Background()
>     resourceID, err := strconv.ParseInt(os.Getenv("CDN_RESOURCE_ID"), 10, 64)
>     if err != nil {
>         panic(err)
>     }
>
>     rule, err := client.CDN.CDNResources.Rules.New(ctx, resourceID, cdn.CDNResourceRuleNewParams{
>         Name:     "image-quality",
>         Rule:     "/.*\\.(jpg|jpeg|png)",
>         RuleType: 0,
>         Options: cdn.CDNResourceRuleNewParamsOptions{
>             ImageStack: cdn.CDNResourceRuleNewParamsOptionsImageStack{
>                 Enabled:     true,
>                 Quality:     gcore.Int(80),
>                 WebpEnabled: gcore.Bool(true),
>                 AvifEnabled: gcore.Bool(false),
>                 PngLossless: gcore.Bool(false),
>             },
>         },
>     })
>     if err != nil {
>         panic(err)
>     }
>     saved := rule.Options.ImageStack
>     fmt.Println(rule.ID, saved.Enabled, saved.Quality, saved.WebpEnabled, saved.AvifEnabled, saved.PngLossless)
> }
> ```

> [tab] curl
>
> ```bash theme={null}
> curl -X POST "https://api.gcore.com/cdn/resources/$CDN_RESOURCE_ID/rules" \
>   -H "Authorization: APIKey $GCORE_API_KEY" \
>   -H "Content-Type: application/json" \
>   -d '{
>     "name": "image-quality",
>     "rule": "/.*\\.(jpg|jpeg|png)",
>     "ruleType": 0,
>     "options": {
>       "image_stack": {
>         "enabled": true,
>         "quality": 80,
>         "webp_enabled": true,
>         "avif_enabled": false,
>         "png_lossless": false
>       }
>     }
>   }'
> ```
>
> The API returns HTTP 201. The saved option is:
>
> ```json theme={null}
> {
>   "enabled": true,
>   "avif_enabled": false,
>   "webp_enabled": true,
>   "quality": 80,
>   "png_lossless": false
> }
> ```

High is 95, medium is 80, and low is 65. Any integer from 1 to 100 is stored, including 50. Set `png_lossless` to true in the same object to keep PNG lossless. Quality then applies to JPG.

## Change the quality level

The rule ID is the `id` returned when the rule is created. Send the full `image_stack` object again. A field left out of this call is stored as its default, so an omitted `quality` becomes 95 and omitted flags become false. This call changes `quality` from 80 to 65 and keeps WebP on.

```bash theme={null}
export CDN_RULE_ID="{YOUR_CDN_RULE_ID}"
```

> [tab] Python SDK
>
> ```python theme={null}
> import os
>
> from gcore import Gcore
>
> client = Gcore()
> resource_id = int(os.environ["CDN_RESOURCE_ID"])
> rule_id = int(os.environ["CDN_RULE_ID"])
>
> updated = client.cdn.cdn_resources.rules.update(
>     rule_id,
>     resource_id=resource_id,
>     options={
>         "image_stack": {
>             "enabled": True,
>             "quality": 65,
>             "webp_enabled": True,
>             "avif_enabled": False,
>             "png_lossless": False,
>         }
>     },
> )
> saved = updated.options.image_stack
> print(saved.enabled, saved.quality, saved.webp_enabled, saved.avif_enabled, saved.png_lossless)
> ```

> [tab] Go SDK
>
> ```go theme={null}
> package main
>
> import (
>     "context"
>     "fmt"
>     "os"
>     "strconv"
>
>     gcore "github.com/G-Core/gcore-go"
>     "github.com/G-Core/gcore-go/cdn"
> )
>
> func main() {
>     client := gcore.NewClient()
>     ctx := context.Background()
>     resourceID, err := strconv.ParseInt(os.Getenv("CDN_RESOURCE_ID"), 10, 64)
>     if err != nil {
>         panic(err)
>     }
>     ruleID, err := strconv.ParseInt(os.Getenv("CDN_RULE_ID"), 10, 64)
>     if err != nil {
>         panic(err)
>     }
>
>     updated, err := client.CDN.CDNResources.Rules.Update(ctx, ruleID, cdn.CDNResourceRuleUpdateParams{
>         ResourceID: resourceID,
>         Options: cdn.CDNResourceRuleUpdateParamsOptions{
>             ImageStack: cdn.CDNResourceRuleUpdateParamsOptionsImageStack{
>                 Enabled:     true,
>                 Quality:     gcore.Int(65),
>                 WebpEnabled: gcore.Bool(true),
>                 AvifEnabled: gcore.Bool(false),
>                 PngLossless: gcore.Bool(false),
>             },
>         },
>     })
>     if err != nil {
>         panic(err)
>     }
>     saved := updated.Options.ImageStack
>     fmt.Println(saved.Enabled, saved.Quality, saved.WebpEnabled, saved.AvifEnabled, saved.PngLossless)
> }
> ```

> [tab] curl
>
> ```bash theme={null}
> curl -X PATCH "https://api.gcore.com/cdn/resources/$CDN_RESOURCE_ID/rules/$CDN_RULE_ID" \
>   -H "Authorization: APIKey $GCORE_API_KEY" \
>   -H "Content-Type: application/json" \
>   -d '{
>     "options": {
>       "image_stack": {
>         "enabled": true,
>         "quality": 65,
>         "webp_enabled": true,
>         "avif_enabled": false,
>         "png_lossless": false
>       }
>     }
>   }'
> ```
>
> The API returns HTTP 200. The saved option is:
>
> ```json theme={null}
> {
>   "enabled": true,
>   "avif_enabled": false,
>   "webp_enabled": true,
>   "quality": 65,
>   "png_lossless": false
> }
> ```

## Set quality on one image URL

`quality` on the image URL is not an API field. Add it next to another Image Stack parameter. The value is an integer from 1 to 100.

```
image.jpg?quality=76&fmt=avif&crop=200,200
image.jpg?quality=95&fmt=webp
image.jpg?quality=50&fmt=avif
image.jpg?quality=10&fmt=avif&crop=300,600
```

Resize, crop, and `fmt` are query parameters on the image URL. See [Crop images](https://docs.gcore.com/cdn/cdn-resource-options/image-optimization-paid/image-stack-tools/crop-images.md), [image size](https://docs.gcore.com/cdn/cdn-resource-options/image-optimization-paid/image-stack-tools/reduce-image-size.md), and [WebP and AVIF](https://docs.gcore.com/cdn/cdn-resource-options/image-optimization-paid/image-stack-tools/configure-image-compression-to-webp-and-avif.md).

## Check the delivered image

After processing, the image URL and file extension stay the same. The `X-Img-Operations` response header lists the conversions. If the value includes `convert`, the image quality was changed.

If `X-Img-Operations` does not contain `convert` and the CDN returns the original-quality image, the `Img-Skip-Reason` header explains the skip. The value `converted image bigger than origin` appears when the configured quality is higher than the original image quality. Combine Quality with another Image Stack feature.

## Terraform

Image quality sets how much detail the delivered picture keeps. A lower value makes the file smaller. The level is `quality` inside `image_stack` on a CDN resource rule, or a `quality` query parameter on one image URL. Saving the rule clears the resource cache, including a later change to `quality`, and CDN servers request the images from the origin. Enable [Origin Shielding](https://docs.gcore.com/cdn/cdn-resource-options/general/enable-and-configure-origin-shielding.md) first, or apply the change during low-traffic hours. Declare the rule with the [Terraform provider](https://docs.gcore.com/developer-tools/terraform/overview.md) v2.

`quality` accepts integers from 1 to 100. Other values are rejected before `terraform apply`. A field left out of `image_stack` is stored as its default, so an omitted `quality` becomes 95 and omitted flags become false. Quality changes the delivered image only when WebP, AVIF, resize, or crop is also enabled. `quality` is still stored when WebP and AVIF are off.

## Set quality for image paths

A rule stores the quality level for paths that match `/.*\.(jpg|jpeg|png)`. `rule_type` 0 is a regular expression and must start with `^/` or `/`. Set `enabled`, `quality` to 80, WebP on, and AVIF and `png_lossless` off, then run `terraform apply`. High is 95, medium is 80, and low is 65. Any integer from 1 to 100 is stored, including 50. Set `png_lossless` to true in the same object to keep PNG lossless. Quality then applies to JPG.

```hcl theme={null}
resource "gcore_cdn_origin_group" "example" {
  name = "my-origin-group"

  sources = [
    {
      source  = "example.com"
      enabled = true
    }
  ]
}

resource "gcore_cdn_resource" "example" {
  cname           = "cdn.example.com"
  origin_group    = gcore_cdn_origin_group.example.id
  origin_protocol = "HTTPS"
}

resource "gcore_cdn_resource_rule" "images" {
  resource_id = gcore_cdn_resource.example.id
  name        = "image-quality"
  rule        = "/.*\\.(jpg|jpeg|png)"
  rule_type   = 0

  options = {
    image_stack = {
      enabled      = true
      quality      = 80
      webp_enabled = true
      avif_enabled = false
      png_lossless = false
    }
  }
}
```

## Change the quality level

Edit `quality` in the same object and run `terraform apply`. This configuration changes `quality` from 80 to 65 and keeps WebP on. Keep every field that should stay set, because a field left out is stored as its default.

```hcl theme={null}
resource "gcore_cdn_origin_group" "example" {
  name = "my-origin-group"

  sources = [
    {
      source  = "example.com"
      enabled = true
    }
  ]
}

resource "gcore_cdn_resource" "example" {
  cname           = "cdn.example.com"
  origin_group    = gcore_cdn_origin_group.example.id
  origin_protocol = "HTTPS"
}

resource "gcore_cdn_resource_rule" "images" {
  resource_id = gcore_cdn_resource.example.id
  name        = "image-quality"
  rule        = "/.*\\.(jpg|jpeg|png)"
  rule_type   = 0

  options = {
    image_stack = {
      enabled      = true
      quality      = 65
      webp_enabled = true
      avif_enabled = false
      png_lossless = false
    }
  }
}
```

## Set quality on one image URL

The rule sets one quality level for every matching path. To set quality on one image, add `quality` to that image URL next to another Image Stack parameter. The value is an integer from 1 to 100.

```
image.jpg?quality=76&fmt=avif&crop=200,200
image.jpg?quality=95&fmt=webp
image.jpg?quality=50&fmt=avif
image.jpg?quality=10&fmt=avif&crop=300,600
```

Resize, crop, and `fmt` are query parameters on the image URL.

## Check the delivered image

After processing, the image URL and file extension stay the same. The `X-Img-Operations` response header lists the conversions. If the value includes `convert`, the image quality was changed.

If `X-Img-Operations` does not contain `convert` and the CDN returns the original-quality image, the `Img-Skip-Reason` header explains the skip. The value `converted image bigger than origin` appears when the configured quality is higher than the original image quality. Combine Quality with another Image Stack feature.
