Image transformations
Resize, crop, and convert uploaded images to WebP or AVIF on the fly with Appwrite Storage. Upload once, request any size and format, and pay per origin image.
5 min read
Appwrite Storage resizes, crops, and converts images on request. Upload the original once, then ask the preview endpoint for the width, height, crop, quality, and format you need, including WebP and AVIF. Appwrite caches each result, and you pay per origin image, not per variant.
Appwrite Storage's preview endpoint let you manipulate resolution, add borders and the border-radius, add background-color, set the opacity for the image, and get the image in the appropriate output format.
You can manipulate images resolution to display appropriately on responsive websites. You can also adjust the image border, background color, and border-radius to match the theming of your application. The Appwrite Storage also allows you to change the format and compression of your images for network transfer optimization and to help you speed your application. You can do all that without caring about how the image was originally uploaded.
When manipulating images in Appwrite, the resulting images are cached by Appwrite and your browser. When you repeatedly use the same transformed images, the performance impact will be minimal.
Plans and pricing
Image transformations are available on paid plans. Pro includes 100 origin images a month, then $5 per 1,000. The Start plan, where it's offered, includes 35. The Free plan doesn't include image transformations.
An origin image counts once however many variants you serve from it: one upload delivered in ten sizes and formats is one origin image. See image transformation billing.
Options
Below you can find all the different parameters offered by the preview endpoint to manipulate the image.
| Parameter | Description |
|---|---|
| width | Set the width of the output image in pixels, the image will be resized keeping the aspect ratio intact. Accepts integer between 0-4000 |
| height | Set the height of the output image in pixels, the image will be resized keeping the aspect ratio intact. Accepts integer between 0-4000 |
| gravity | The gravity while cropping the image providing either width, height, or both. Accepts any of: center, top-left, top, top-right, left, right, bottom-left, bottom, bottom-right, or auto. When set to auto, Appwrite detects the main subject of the image and keeps it in view while cropping. |
| quality | Set the quality of the output image. Accepts integer between 0-100, where 100 is the highest quality. |
| borderWidth | Set a border with the given width in pixels to the output image. Accepts integer between 0-100. |
| borderColor | Set a border-color for the output image. Accepts any valid hex color value without the leading #. |
| borderRadius | Set a border-radius in pixels. Accepts an integer between 0-4000. |
| opacity | Set opacity for the output image. Accepts a float value between 0-1, where 0 makes it transparent. Only works with output formats supporting alpha channels like png. |
| rotation | Rotate the output image by a degree. Accepts an integer between -360 to 360. |
| background | Set a background-color. Accepts any valid hex color value without the leading #. Only works with output formats supporting alpha channels like png. |
| output | Set the output image format. If not provided, will use the original image's format. Supported formats are: jpg, jpeg, png, gif, webp, avif, and heic. Pass auto to get AVIF or WebP when the request's Accept header lists them. See automatic output format. |
Examples
Here are some examples using Client SDKs.
import { Client, Storage } from "appwrite";
const client = new Client();
const storage = new Storage(client);
client .setEndpoint('https://<REGION>.cloud.appwrite.io/v1') // Your API Endpoint .setProject('<PROJECT_ID>') // Your project ID;
const result = storage.getFilePreview({ bucketId: 'photos', fileId: 'sunset.png', width: 1800, gravity: 'center', quality: '90', borderWidth: 5, borderColor: 'CDCA30', borderRadius: 15, background: 'FFFFFF'});
console.log(result.href);Automatic output format
Set output to auto to serve each client the smallest format it can display, from a single preview URL. Appwrite reads the request's Accept header and picks the output format in this order:
avif, if the header listsimage/avif.webp, if the header listsimage/webp.- Otherwise, the format you get without
output: the original image's format, orjpgwhen Appwrite can't output the original format.
Only types the header names explicitly count. Wildcards such as image/* and */* never select AVIF or WebP, and a type with q=0 is excluded. The order above is Appwrite's preference, so a client that lists both types gets AVIF.
import { Client, Storage, ImageFormat } from "appwrite";
const client = new Client() .setEndpoint('https://<REGION>.cloud.appwrite.io/v1') // Your API Endpoint .setProject('<PROJECT_ID>'); // Your project ID
const storage = new Storage(client);
// Returns a preview URL. The browser's Accept header picks the format.const url = storage.getFilePreview({ bucketId: 'photos', fileId: 'sunset', width: 800, output: ImageFormat.Auto});
document.querySelector('#sunset').src = url;You can also write the parameter into a preview URL directly, for example in server-rendered HTML:
<img src="https://<REGION>.cloud.appwrite.io/v1/storage/buckets/photos/files/sunset/preview?project=<PROJECT_ID>&width=800&output=auto" alt="Sunset over the bay"/>Which clients get AVIF or WebP
Negotiation depends on the Accept header the client sends, so auto is built for URLs that a browser loads as an image, such as <img> elements, srcset candidates, and CSS backgrounds. The Web SDK's getFilePreview() returns a URL for exactly this. According to MDN's list of default Accept values, Chrome, Edge, and Firefox list image/avif for images, while Safari lists image/webp but not image/avif, so Safari gets WebP.
Requests that don't name an image type get the default format:
- The Flutter, Apple, Android, and React Native SDKs, and the server SDKs, download preview bytes with
Accept: image/*. Pass an explicit format, such aswebp, when you need a specific one. - A browser
fetch()call sendsAccept: */*unless you set the header yourself.
ImageFormat.Auto is available from these SDK versions:
| SDK | First version |
|---|---|
| Web | 29.3.0 |
| React Native | 3.3.0 |
| Flutter | 28.4.0 |
| Apple | 21.3.0 |
| Android | 31.3.0 |
| Unity | 0.13.0 |
| Node.js | 31.3.0 |
| Python | 27.3.0 |
| PHP | 32.4.0 |
| Ruby | 30.3.0 |
| Dart | 31.3.0 |
| .NET | 11.3.0 |
| Kotlin | 24.3.0 |
| Swift | 23.4.0 |
| Rust | 0.24.0 |
The Go SDK takes output as a string, so pass "auto" to WithGetFilePreviewOutput.
Caching
Responses to auto requests include Vary: Accept and Cache-Control: private, so browsers cache them, but shared caches and CDNs don't. Appwrite's own preview cache stores one entry per negotiated format, and an auto request that resolves to AVIF reuses the entry for output=avif. Requests with an explicit output ignore the Accept header and cache exactly as before.
Each format is a transformation of the same file, so it counts as one origin image however many formats you serve.
Was this page helpful?
Share what worked or what we should fix. Once approved, our agents automatically apply suggested updates to the docs.