# Styles

> Save a set of media processing parameters as a style and call it with a short name. Variable parameters are supported.

A Style saves a set of [CoreIX](https://docs.bitiful.com/en/coreix/basic.md) processing parameters under a name, so at access time you write only the name. Use it to standardize thumbnail specs across your site and to hide processing parameters. It is also a prerequisite for [original file protection](https://docs.bitiful.com/en/coreix/handbook/origin-protect.md).

To get there, go to **Object Storage (对象存储) › [Bucket list (桶列表)](https://console.bitiful.com/buckets)** › click the bucket name › **Media processing (媒体处理) › Style rules (Style规则)**.

## Create a style

### Create a rule

Click **New rule (新建规则)**.

### Enter a name and code

Enter `thumb` for **Rule name (规则名称)** and `w=500&h=500&fmt=webp` for **Code (代码)**.

### Preview

Click **Refresh preview (刷新预览)** to see the effect on a sample image.

### Save

Click **Save (保存)**, and it takes effect immediately.

- `Rule name` (text, required): 1 \~ 36 characters, digits and upper- and lowercase letters only, case-sensitive. Names must be unique within a bucket. A duplicate shows "您已添加该条规则，请更换规则名称" (you have already added this rule; please change the rule name).

- `Code` (text, required): The processing parameters, written the same way as URL parameters, joined with `&`, without a leading `?`, up to 1000 characters. Special characters must be URL-encoded, for example `%` as `%25` and `#` as `%23`. For the available parameters, see the [CoreIX overview](https://docs.bitiful.com/en/coreix/basic.md).

Parameters aren't validated one by one when you save, so confirm the effect with the preview. You can't create or edit styles while the account has an overdue balance.

## Use a style

Append `!style:style-name` or `!style=style-name` to the file address. The two forms are equivalent:

```text
https://img.example.com/photos/girl.jpeg!style:thumb
https://img.example.com/photos/girl.jpeg!style=thumb
```

The style goes in the path and is not a URL parameter. Put other parameters after `?` as usual.

When you access with a signed link, sign `!style:thumb` as part of the object key, for example a key of `photos/girl.jpeg!style:thumb`.

If the style name doesn't exist, the whole address is treated as a file name and returns 404.

## Variable parameters

A `*` before a parameter name means the value is only a **default**, which URL parameters can override at access time. Parameters without `*` are fixed.

Take the style `card` with the code `w=1200&*txt=@Bitiful&*txt-size=0.04` as an example:

| Address                          | Actual effect                                          |
| :------------------------------- | ------------------------------------------------------ |
| `girl.jpeg!style:card`           | `w=1200`, text is `@Bitiful`                           |
| `girl.jpeg!style:card?txt=Hello` | `w=1200`, text is `Hello`                              |
| `girl.jpeg!style:card?txt=`      | Empty value; the default text `@Bitiful` is still used |
| `girl.jpeg!style:card?w=3000`    | `w` is fixed and stays `1200`                          |
| `girl.jpeg!style:card?h=100`     | `h` isn't in the style and is ignored                  |

Summary of the rules:

- When you use a style, all processing parameters outside the style are ignored.
- If the same parameter is written both with and without `*`, the one without `*` wins.
- Non-processing parameters, such as the download file name, are unaffected.

> **Warning**
>
> Don't make parameters that affect file size (format, width, height, quality, and so on) variable. Otherwise, visitors can generate large numbers of images in different sizes, which increases processing and data transfer costs.

## Common styles

| Name      | Code                                    | Purpose                                                         |
| :-------- | --------------------------------------- | --------------------------------------------------------------- |
| `thumb`   | `w=300&h=300&fmt=auto`                  | Square thumbnail; the format is chosen automatically by browser |
| `preview` | `w=1600&q=80&fmt=auto`                  | Large image for detail pages                                    |
| `avatar`  | `w=200&h=200&pos=smart`                 | Avatar; automatically keeps faces                               |
| `cover`   | `frame=auto&w=1280`                     | Video cover                                                     |
| `wm`      | `w=2000&txt=@MyBrand&txt-pos=0.95,0.93` | Add a text watermark                                            |

For parameter details, see [Resize images](https://docs.bitiful.com/en/coreix/features/resize.md), [Format conversion](https://docs.bitiful.com/en/coreix/features/fmt.md), [Text watermarks](https://docs.bitiful.com/en/coreix/features/watermark.md), and [Frame capture](https://docs.bitiful.com/en/coreix/features/frame.md).

## Edit and delete

- **Edit (编辑)**: Changes to the code take effect immediately. Old images accessed through a CDN are still cached, so you need to [purge the cache](https://docs.bitiful.com/en/cdn/handbook/cache.md). If you change the rule name while editing, it is the same as saving a new rule, and the old rule is kept.
- **Delete (删除)**: Links that use the style become 404. You can't delete a style that is set as the **Default Style (默认Style)** of original file protection. Change it in original file protection first.

## Relationship to original file protection

Creating a style alone does **not** stop visitors from using parameters such as `?w=3000` directly or accessing the original image. To require visitors to access only through styles, also turn on [original file protection](https://docs.bitiful.com/en/coreix/handbook/origin-protect.md).

## FAQ

<details>
<summary>It returns 404 NoSuchKey</summary>

1. The style name's case doesn't match, or the style has been deleted.
2. `!style:` was written as a URL parameter.
3. The original file doesn't exist.

</details>

<details>
<summary>Do styles work on a CDN?</summary>

Yes, with the same syntax.

</details>

<details>
<summary>Can I use a style across buckets?</summary>

No. Each bucket has its own settings.

</details>

<details>
<summary>It returns TooLargeForCoreIX</summary>

The original image exceeds 200 MiB. See [Supported formats](https://docs.bitiful.com/en/formats.md).

</details>
