ImageTransformation
ImageTransformation
Warning Contentstack has deprecated the Dart Delivery SDK for new projects. Existing apps can keep using it. For new Dart or Flutter projects, call the Content Delivery API directly with an HTTP client.
The Image Delivery API resizes, crops and converts the images in a stack when a client requests an image URL with query parameters. Use this class to
- collect Image Delivery API parameters for one image, one setter call per parameter
- read the finished URL as a string, for an image widget or an HTTP client to load
Create an instance with the imageTransform method of Stack, which takes the argument below:
| Name | Type | Description |
|---|---|---|
| imageUrl | String | URL of the image, such as the url of an asset. |
Class-Level Notes
Setters return void.
- Every setter returns void, so chain setters with the Dart cascade operator .., for example stack.imageTransform('<IMAGE_URL>')..resize(width: 640)..quality(80).
- A setter makes no request. getUrl builds the URL from the settings the instance stores.
Only fetch makes a request.
- getUrl and every setter run in the app. The timeout, region and headers of the Stack play no part in them.
- The Image Delivery API applies the parameters when an image widget, a browser or an HTTP client requests the finished URL. The API reports a rejection to that client, not to the SDK.
- Known issue: fetch throws a FormatException when the API returns the image, because it decodes the image as JSON. Load the URL that getUrl returns with your own HTTP client or an image widget instead.
How the SDK writes values into the URL.
- Each Image Delivery API parameter appears once in the URL, with one value. A second call that writes the same parameter replaces the value and keeps its place in the URL. Two groups of methods share parameters:
- crop and cropBy both write crop.
- resize, fit and resizeFilter all write width and height.
- Parameters appear in the URL in the order you first set them.
- The SDK percent-encodes each value, so , becomes %2C, : becomes %3A, / becomes %2F and a space becomes +.
- The SDK removes the characters <, >, ", ;, (, ), { and } from every value.
- The SDK leaves out a parameter whose value is an empty string, so bgColor('') adds nothing to the URL.
- The SDK checks no value against the ranges the API accepts. A value outside a range reaches the URL unchanged.
- No method removes a setting. Create a new instance for each variant of an image.
Importing the image enums.
- package:contentstack/contentstack.dart does not export Fit, Format, Filter or Orientation. Import each one from its src/ file, for example import 'package:contentstack/src/image/format.dart';. The Dart analyzer reports these imports under the implementation_imports lint.
- In a Flutter app, Orientation also names the Flutter screen orientation enum. Import the SDK enum with a prefix, for example import 'package:contentstack/src/image/orientation.dart' as image;, and write image.Orientation.Vertically.
Environment.
- The Image Delivery API reference lists environment as a required query parameter of every image request, for example under Resize Images. getUrl does not add it and no setter writes it. Append &environment=<ENVIRONMENT> to the string that getUrl returns when your setup needs it.
Image Enums
The four enums below define the fixed values that fit, convert, resizeFilter and orientation accept. Each table lists every value of the enum.
Fit (package:contentstack/src/image/fit.dart), used by fit. The API reference describes each mode under Fit Mode.
| Value | Sends | Result |
|---|---|---|
| Fit.Bounds | fit=bounds | Keeps the image within the width and height. |
| Fit.Crop | fit=crop | Crops the image to the width and height. |
Format (package:contentstack/src/image/format.dart), used by convert. The API reference describes each format under Convert Formats.
| Value | Sends | Result |
|---|---|---|
| Format.Gif | format=gif | GIF |
| Format.Png | format=png | PNG |
| Format.Jpg | format=jpg | JPEG |
| Format.Pjpg | format=pjpg | Progressive JPEG |
| Format.Webp | format=webp | WebP |
| Format.Webplossy | format=webply | WebP, lossy |
| Format.Webplossless | format=webpll | WebP, lossless |
Filter (package:contentstack/src/image/filter.dart), used by resizeFilter. The API reference describes each filter under Resize-filter.
| Value | Sends | Result |
|---|---|---|
| Filter.Nearest | resize-filter=nearest | Uses neighboring pixels for fast resizing. |
| Filter.Bilinear | resize-filter=bilinear | Averages a 2x2 block of pixels. |
| Filter.Bicubic | resize-filter=bicubic | Averages a 4x4 block of pixels. |
| Filter.Lanczos | resize-filter=lanczos3 | Uses the Lanczos sinc approximation. |
Orientation (package:contentstack/src/image/orientation.dart), used by orientation. The API reference lists each value under Reorient Images.
| Value | Sends | Result |
|---|---|---|
| Orientation.ToDefault | orient=1 | Default orientation |
| Orientation.Horizontally | orient=2 | Flips the image horizontally. |
| Orientation.HorizontallyAndVertically | orient=3 | Flips the image horizontally and vertically. |
| Orientation.Vertically | orient=4 | Flips the image vertically. |
| Orientation.HorizontallyAndRotate90DegreeLeft | orient=5 | Flips horizontally, then rotates 90 degrees left. |
| Orientation.Degrees90TowardsRight | orient=6 | Rotates 90 degrees right. |
| Orientation.HorizontallyAndRotate90DegreesRight | orient=7 | Flips horizontally, then rotates 90 degrees right. |
| Orientation.Rotate90DegreesLeft | orient=8 | Rotates 90 degrees left. |
Capability Matrix
| Capability | Supported | Notes / Alternative |
|---|---|---|
| Download the resized or converted image | No | fetch decodes the image as JSON and throws. Load the getUrl string with an HTTP client or an image widget |
| Remove or reset a setting | No | Create a new instance for each variant |
| Decimal values for blur, brightness, contrast, saturation, dpr or sharpen | No | These setters take int. The API also accepts decimals |
| Percentage or p values for resize, cropBy or the overlay size | No | These parameters are int. Use crop with a string for a percentage crop |
| fit=cover, format=auto, format=avif or resize-filter=lanczos2 | No | The enums do not include these values. Use auto(auto: 'avif') for AVIF |
| Add the environment parameter | No | Append &environment=<ENVIRONMENT> to the getUrl string |
Method Index
Resize the image. Available on every instance that stack.imageTransform() returns.
| Method Name | Returns | Description |
|---|---|---|
| resize | Nothing. The image transformation keeps the setting for its next URL. | Sets the output width, height and upscaling. |
| fit | Nothing. The image transformation keeps the setting for its next URL. | Fits the image into a width and height. |
| resizeFilter | Nothing. The image transformation keeps the setting for its next URL. | Sets the resampling filter for a resize. |
| dpr | Nothing. The image transformation keeps the setting for its next URL. | Scales the output for a device pixel ratio. |
Crop, trim, pad and extend the image. Available on every instance that stack.imageTransform() returns.
| Method Name | Returns | Description |
|---|---|---|
| crop | Nothing. The image transformation keeps the setting for its next URL. | Crops the image with a crop string. |
| cropBy | Nothing. The image transformation keeps the setting for its next URL. | Crops the image by pixel width and height. |
| trim | Nothing. The image transformation keeps the setting for its next URL. | Trims pixels from the image edges. |
| padding | Nothing. The image transformation keeps the setting for its next URL. | Adds pixels around the image edges. |
| canvas | Nothing. The image transformation keeps the setting for its next URL. | Enlarges the canvas around the image. |
| bgColor | Nothing. The image transformation keeps the setting for its next URL. | Sets the color of padding and transparent pixels. |
Set the format and quality. Available on every instance that stack.imageTransform() returns.
| Method Name | Returns | Description |
|---|---|---|
| convert | Nothing. The image transformation keeps the setting for its next URL. | Converts the image to another format. |
| auto | Nothing. The image transformation keeps the setting for its next URL. | Serves WebP or AVIF to supporting clients. |
| quality | Nothing. The image transformation keeps the setting for its next URL. | Sets the compression level of lossy formats. |
| frame | Nothing. The image transformation keeps the setting for its next URL. | Returns the first frame of an animated GIF. |
Adjust the appearance. Available on every instance that stack.imageTransform() returns.
| Method Name | Returns | Description |
|---|---|---|
| blur | Nothing. The image transformation keeps the setting for its next URL. | Blurs the image. |
| brightness | Nothing. The image transformation keeps the setting for its next URL. | Raises or lowers the brightness. |
| contrast | Nothing. The image transformation keeps the setting for its next URL. | Raises or lowers the contrast. |
| saturation | Nothing. The image transformation keeps the setting for its next URL. | Raises or lowers the color intensity. |
| sharpen | Nothing. The image transformation keeps the setting for its next URL. | Sharpens the edges in the image. |
| orientation | Nothing. The image transformation keeps the setting for its next URL. | Flips or rotates the image. |
Place an overlay image. Available on every instance that stack.imageTransform() returns.
| Method Name | Returns | Description |
|---|---|---|
| overlay | Nothing. The image transformation keeps the setting for its next URL. | Places a second image on top. |
| overlayPadding | Nothing. The image transformation keeps the setting for its next URL. | Adds padding around the overlay image. |
Build the URL or request the image. Available on every instance that stack.imageTransform() returns.
| Method Name | Returns | Description |
|---|---|---|
| getUrl | The image URL with its transformation parameters. | Builds the image URL with its query parameters. |
| fetch | The decoded JSON body of the response. | Requests the image URL that getUrl builds. |
Class-Level Snippet
The snippet below fetches an asset, then builds a 640-pixel progressive JPEG URL with a white border from the asset's url.
import 'package:contentstack/contentstack.dart' as contentstack;
import 'package:contentstack/src/image/format.dart';
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
try {
final response = await stack.asset('<ASSET_UID>').fetch();
if (response is Map && response.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${response['error_code']} ${response['error_message']}');
} else {
final transform = stack.imageTransform(response['asset']['url'])
..resize(width: 640, disable: true)
..convert(Format.Pjpg)
..quality(80)
..padding('10')
..bgColor('FFFFFF');
// getUrl builds the string and makes no request
print(transform.getUrl());
// <asset url>?width=640&disable=upscale&format=pjpg&quality=80&pad=10&bg-color=FFFFFF
}
} catch (e) {
// Thrown for a missing UID, a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}Methods
Each method of the ImageTransformation class has its own section below, with its parameters, validation rules, behavior and examples.
auto
auto asks the Image Delivery API to serve WebP or AVIF to clients that support the format.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| auto | String? (named) | No | null (adds no auto parameter) | Format to serve, webp or avif. |
| format | String? (named) | No | null (adds nothing) | Fallback format for other clients. |
Validation
- There is no client-side validation. auto writes any string, including a value the API does not accept.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resources Refer to Enable auto optimization and Enable auto optimization and encode to progressive JPEG in the Image Delivery API reference for the values the API accepts.
Behavior
- Known issue: auto sends the format argument as formate, a parameter the Image Delivery API reference does not list. Clients without WebP or AVIF support do not get the fallback format. Set the fallback with the convert method instead.
- auto writes auto=<value> to the URL, and formate=<value> when you pass format.
- The API reference lists webp and avif as the values of auto. The API serves that format to clients that support it and applies the format parameter for the rest.
- The API reference states that auto does not apply to GIF files.
- With neither argument, auto adds nothing to the URL.
Example
Basic usage: serve WebP to supporting clients
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..auto(auto: 'webp');
print(transform.getUrl()); // <IMAGE_URL>?auto=webp
}All parameters: AVIF with a fallback format
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..auto(auto: 'avif', format: 'pjpg');
// The fallback reaches the URL as formate, which the API reference does not list
print(transform.getUrl()); // <IMAGE_URL>?auto=avif&formate=pjpg
}Workaround: set the fallback format with convert
import 'package:contentstack/contentstack.dart' as contentstack;
import 'package:contentstack/src/image/format.dart';
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')
..auto(auto: 'webp')
..convert(Format.Pjpg);
print(transform.getUrl()); // <IMAGE_URL>?auto=webp&format=pjpg
}bgColor
bgColor sets the color the Image Delivery API uses for padding and for transparent pixels.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| bgColor | String | Yes | Not applicable | Color as a hexadecimal, RGB or RGBA value. |
Validation
- There is no client-side validation. bgColor writes any string, including a value the API does not accept.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resources Refer to Change background color using hexadecimal value, Change background color by Red, Blue, Green value and Change background color by Red, Blue, Green, Alpha value in the Image Delivery API reference for the values the API accepts.
Behavior
- bgColor writes bg-color=<value> to the URL. The SDK encodes each comma as %2C, so 140,220,123 reaches the URL as bg-color=140%2C220%2C123.
- The API reference lists three forms of value:
- A 3 or 6 digit hexadecimal value without #, for example cccccc.
- A red, green and blue value from 0 to 255 each, for example 140,220,123.
- The same value with an alpha from 0.0 (transparent) to 1.0 (opaque), for example 140,220,123,0.5.
- Combine bgColor with the padding method to draw a colored border.
Example
Basic usage: a red border
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')
..padding('10')
..bgColor('FF0000');
print(transform.getUrl()); // <IMAGE_URL>?pad=10&bg-color=FF0000
}Semi-transparent color: an RGBA value
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..bgColor('140,220,123,0.5');
print(transform.getUrl()); // <IMAGE_URL>?bg-color=140%2C220%2C123%2C0.5
}blur
blur reduces the focus and clarity of the image.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| blur | int | Yes | Not applicable | Blur strength, 1 to 1000. |
Validation
- There is no client-side validation. blur writes any int, including a value outside the API range of 1 to 1000.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resource Refer to Blur in the Image Delivery API reference for the values the API accepts.
Behavior
- blur writes blur=<value> to the URL.
- The API reference accepts any number from 1 to 1000, decimals included.
- Limitations:
- Does not take a decimal value, because blur takes an int.
Example
Basic usage: blur an image by 40
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..blur(40);
print(transform.getUrl()); // <IMAGE_URL>?blur=40
}brightness
brightness raises or lowers the perceived light intensity of the image.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| brightness | int | Yes | Not applicable | Brightness change, -100 to 100. |
Validation
- There is no client-side validation. brightness writes any int, including a value outside the API range of -100 to 100.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resources Refer to Increase brightness and Decrease brightness in the Image Delivery API reference for the values the API accepts.
Behavior
- brightness writes brightness=<value> to the URL.
- A positive value brightens the image and a negative value darkens it. The API reference gives 0 as the API default, which leaves the image unchanged, 100 for an entirely white image and -100 for an entirely black one.
- The API also accepts decimals from -100.00 to 100.00.
- Limitations:
- Does not take a decimal value, because brightness takes an int.
Example
Basic usage: brighten an image
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..brightness(20);
print(transform.getUrl()); // <IMAGE_URL>?brightness=20
}Negative value: darken an image
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..brightness(-20);
print(transform.getUrl()); // <IMAGE_URL>?brightness=-20
}canvas
canvas enlarges the area around the image to a size, an aspect ratio or a position you give.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| canvasValue | String | Yes | Not applicable | Canvas size, ratio, position or offset. |
Validation
- There is no client-side validation. canvas writes any string, including a value the API does not accept.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resources Refer to Canvas by width and height, Canvas sub region and Canvas and offset in the Image Delivery API reference for the values the API accepts.
Behavior
- canvas writes canvas=<value> to the URL. The SDK encodes , as %2C and : as %3A.
- The API reference lists these forms of value:
- Width and height in pixels or percentage, for example 700,800, 0.70,0.80 or 700,0.80.
- An aspect ratio, for example 2:3.
- Width and height with the top-left corner, for example 700,800,x150,y75.
- Width and height with an offset in percentage, for example 700,800,offset-x0.65,offset-y0.80.
- The API reference requires the canvas to be at least as large as the image. The canvas is white, or transparent for a format that supports transparency.
- The API ignores the pad parameter of the padding method when canvas is present.
Example
Basic usage: a 700 by 800 canvas
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..canvas('700,800');
print(transform.getUrl()); // <IMAGE_URL>?canvas=700%2C800
}Aspect ratio: a 2:3 canvas
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..canvas('2:3');
print(transform.getUrl()); // <IMAGE_URL>?canvas=2%3A3
}Offset: place the image off center
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')
..canvas('700,800,offset-x0.65,offset-y0.80');
print(transform.getUrl()); // <IMAGE_URL>?canvas=700%2C800%2Coffset-x0.65%2Coffset-y0.80
}contrast
contrast raises or lowers the difference between the darkest and lightest tones of the image.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| contrast | int | Yes | Not applicable | Contrast change, -100 to 100. |
Validation
- There is no client-side validation. contrast writes any int, including a value outside the API range of -100 to 100.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resources Refer to Increase contrast and Decrease contrast in the Image Delivery API reference for the values the API accepts.
Behavior
- contrast writes contrast=<value> to the URL.
- A positive value raises the contrast and a negative value lowers it. The API reference gives 0 as the API default, which leaves the image unchanged, and -100 for a neutral gray image.
- The API also accepts decimals from -100.00 to 100.00.
- Limitations:
- Does not take a decimal value, because contrast takes an int.
Example
Basic usage: raise the contrast
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..contrast(20);
print(transform.getUrl()); // <IMAGE_URL>?contrast=20
}Negative value: lower the contrast
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..contrast(-30);
print(transform.getUrl()); // <IMAGE_URL>?contrast=-30
}convert
convert sets the format the Image Delivery API converts the image to.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| format | Format | Yes | Not applicable | Output format, a Format value. |
Validation
- There is no client-side validation. Every Format value maps to a format the API reference lists.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resources Refer to Convert to Progressive JPEG and Convert to WEBP Lossless in the Image Delivery API reference for the formats and the quality rules of the API.
Behavior
- convert writes format=<value> to the URL, with the value from the Format table on the ImageTransformation class page.
- Import Format from package:contentstack/src/image/format.dart. The main library does not export it.
- The API reference applies the quality method to JPEG, Progressive JPEG, AVIF and WebP lossy output only.
- The API reference requires an output format for a HEIC source image.
- Limitations:
- Does not offer format=auto or format=avif, because Format has no such values. Use the auto method with auto: 'avif' to serve AVIF to supporting clients.
Example
Basic usage: convert to progressive JPEG
import 'package:contentstack/contentstack.dart' as contentstack;
import 'package:contentstack/src/image/format.dart';
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..convert(Format.Pjpg);
print(transform.getUrl()); // <IMAGE_URL>?format=pjpg
}Lossless WebP: keep every detail
import 'package:contentstack/contentstack.dart' as contentstack;
import 'package:contentstack/src/image/format.dart';
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..convert(Format.Webplossless);
print(transform.getUrl()); // <IMAGE_URL>?format=webpll
}crop
crop removes pixels from the image, using the crop string you give.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| cropValue | String | Yes | Not applicable | Crop size, ratio, position, offset or mode. |
Validation
- There is no client-side validation. crop writes any string, including a value the API does not accept.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resources Refer to Crop by width and height, Crop by aspect ratio, Crop in fail-safe mode and Smart Crop in the Image Delivery API reference for the values the API accepts.
Behavior
- crop writes crop=<value> to the URL. The SDK encodes , as %2C and : as %3A.
- The API reference lists these forms of value:
- Width and height in pixels, percentage or p values, for example 300,400, 0.50,0.60 or 300,0.60. The API crops from the center.
- An aspect ratio, for example 1:3, together with a width or height parameter.
- Width and height with the top-left corner, for example 300,400,x150,y75.
- Width and height with an offset in percentage, for example 300,400,offset-x10.5,offset-y10.5.
- Any of the above followed by safe, which returns the part of the crop area inside the image instead of an error.
- An aspect ratio followed by smart, for example 2:5,smart, for a content-aware crop.
- crop and the cropBy method both write crop. The later call replaces the value.
- Set the width or height parameter for an aspect ratio crop with the resize method.
Example
Basic usage: crop to 300 by 400 pixels
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..crop('300,400');
print(transform.getUrl()); // <IMAGE_URL>?crop=300%2C400
}Aspect ratio: a 1:3 crop at 300 pixels wide
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')
..resize(width: 300)
..crop('1:3');
print(transform.getUrl()); // <IMAGE_URL>?width=300&crop=1%3A3
}Smart crop: a square crop around the main subject
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')
..resize(width: 400, height: 400)
..crop('1:1,smart');
print(transform.getUrl()); // <IMAGE_URL>?width=400&height=400&crop=1%3A1%2Csmart
}cropBy
cropBy removes pixels from the image, using a pixel width and height and an optional position.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| width | int | Yes | Not applicable | Crop width in pixels. |
| height | int | Yes | Not applicable | Crop height in pixels. |
| region | String? (named) | No | null (adds no position) | Top-left corner, for example x150,y75. |
| offset | String? (named) | No | null (adds no offset) | Offset, for example offset-x10.5,offset-y10.5. |
Validation
- There is no client-side validation. cropBy writes region and offset as given, including a value the API does not accept.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resources Refer to Crop by width and height, Crop sub region and Crop and offset in the Image Delivery API reference for the values the API accepts.
Behavior
- cropBy joins width, height, region and offset with a comma and a space, then writes the result to crop. cropBy(300, 400) produces crop=300%2C+400, which the API receives as 300, 400.
- To send the value without spaces, pass one string to the crop method, for example crop('300,400').
- The API reference takes x and y in pixels or percentage, and offset-x and offset-y in percentage only.
- Passing both region and offset writes both parts. The API reference describes x and y or offset-x and offset-y, not both together.
- cropBy and the crop method both write crop. The later call replaces the value.
- Limitations:
- Does not take a percentage or an aspect ratio, because width and height are int. Use the crop method for those.
Example
Basic usage: crop to 300 by 400 pixels
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..cropBy(300, 400);
print(transform.getUrl()); // <IMAGE_URL>?crop=300%2C+400
}Sub region: start the crop at a top-left corner
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')
..cropBy(300, 400, region: 'x150,y75');
print(transform.getUrl()); // <IMAGE_URL>?crop=300%2C+400%2C+x150%2Cy75
}Offset: move the center of the crop
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')
..cropBy(300, 400, offset: 'offset-x10.5,offset-y10.5');
print(transform.getUrl()); // <IMAGE_URL>?crop=300%2C+400%2C+offset-x10.5%2Coffset-y10.5
}dpr
dpr scales the output image for a screen with the given device pixel ratio.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| dpr | int | Yes | Not applicable | Device pixel ratio, 1 to 10000. |
Validation
- There is no client-side validation. dpr writes any int, including a value outside the API range of 1 to 10000.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resource Refer to Set device pixel ratio in the Image Delivery API reference for the values the API accepts.
Behavior
- dpr writes dpr=<value> to the URL.
- The API reference requires a width or height parameter with dpr. Set one with the resize method.
- The API also accepts decimals from 0.0 to 9999.9999.
- Limitations:
- Does not take a decimal ratio such as 1.5, because dpr takes an int.
Example
Basic usage: a 300-pixel image for a 2x screen
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')
..resize(width: 300)
..dpr(2);
print(transform.getUrl()); // <IMAGE_URL>?width=300&dpr=2
}fetch
fetch requests the URL that getUrl builds and decodes the response body as JSON.
Returns: Future<T>. The decoded JSON body of the response.
This method takes no parameters. The type arguments T and K have no effect on an image response.
Validation
- fetch throws Exception before any request when the image URL is not absolute, because it needs a scheme and a host to send the request to.
- fetch throws FormatException when the response body is not JSON. An image response is never JSON, so every successful request throws.
- When the API rejects the request with a JSON error body, fetch returns the decoded body. Check the result for an error_code key.
- Any network exception from the http package, for example SocketException, passes through unchanged.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Known issue: fetch cannot return the image, because it decodes the image as JSON and throws. Load the URL that the getUrl method returns with your own HTTP client or an image widget instead.
- fetch sends one GET request through the HTTP client of the stack, including a custom client you passed to the Stack constructor.
- It sends none of the stack headers, such as api_key or access_token.
- It sets no timeout. The 30-second timeout of the other SDK requests does not apply.
Example
Error handling: the FormatException on an image response
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..resize(width: 640);
try {
final response = await transform.fetch();
if (response is Map && response.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${response['error_code']} ${response['error_message']}');
} else {
print(response);
}
} on FormatException catch (e) {
// Thrown when the API returns the image, because fetch decodes the body as JSON
print('The response is an image, not JSON: $e');
} catch (e) {
// Thrown for a URL that is not absolute or a network failure
print('Request did not complete: $e');
}
}Workaround: download the image from the getUrl string
import 'package:contentstack/contentstack.dart' as contentstack;
import 'package:http/http.dart' as http;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..resize(width: 640);
// In a Flutter app, pass the same string to Image.network instead
final response = await http.get(Uri.parse(transform.getUrl()));
print('${response.statusCode} ${response.headers['content-type']} ${response.bodyBytes.length} bytes');
}fit
fit fits the image into a width and height, by keeping it within the area or by cropping it.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| width | double | Yes | Not applicable | Width in pixels, or 0.0 to 0.99 for a percentage. |
| height | double | Yes | Not applicable | Height in pixels, or 0.0 to 0.99 for a percentage. |
| fit | Fit | Yes | Not applicable | Fit mode, Fit.Bounds or Fit.Crop. |
Validation
- There is no client-side validation. fit writes any double, including a size outside the range the API accepts.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resources Refer to Fit to bounds and Fit by cropping in the Image Delivery API reference for the values the API accepts.
Behavior
- fit writes width, height and fit to the URL, in that order. The value of fit comes from the Fit table on the ImageTransformation class page.
- It converts each size with Dart's double.toString, so fit(300, 200, Fit.Bounds) produces width=300.0&height=200.0&fit=bounds, and fit(0.5, 0.5, Fit.Crop) produces width=0.5&height=0.5&fit=crop.
- To send whole-number pixel sizes, call the resize method with the same width and height after fit. resize replaces the two values and keeps their place in the URL.
- Fit.Bounds keeps the whole image within the width and height. Fit.Crop crops the image to the width and height.
- fit, resize and the resizeFilter method all write width and height. The latest call sets the values.
- Import Fit from package:contentstack/src/image/fit.dart. The main library does not export it.
- Limitations:
- Does not offer the cover mode that the API reference lists under Fit to cover, because Fit has no such value.
Example
Basic usage: fit within half the width and height
import 'package:contentstack/contentstack.dart' as contentstack;
import 'package:contentstack/src/image/fit.dart';
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..fit(0.5, 0.5, Fit.Bounds);
print(transform.getUrl()); // <IMAGE_URL>?width=0.5&height=0.5&fit=bounds
}Pixel sizes: crop to 300 by 200 with whole numbers
import 'package:contentstack/contentstack.dart' as contentstack;
import 'package:contentstack/src/image/fit.dart';
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')
..fit(300, 200, Fit.Crop)
// resize replaces width=300.0 and height=200.0 with whole numbers
..resize(width: 300, height: 200);
print(transform.getUrl()); // <IMAGE_URL>?width=300&height=200&fit=crop
}frame
frame returns one frame of an animated GIF as a still image.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| frame | int | Yes | Not applicable | Frame number. The API supports 1 only. |
Validation
- There is no client-side validation. frame writes any int, including a frame number other than 1.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resource Refer to Fetch first frame in the Image Delivery API reference for the values the API accepts.
Behavior
- frame writes frame=<value> to the URL.
- The API reference applies frame to animated GIF files only, and supports the first frame only.
Example
Basic usage: the first frame of an animated GIF
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..frame(1);
print(transform.getUrl()); // <IMAGE_URL>?frame=1
}getUrl
getUrl returns the image URL with every setting of the instance appended as a query parameter.
Returns: String. The image URL with its transformation parameters.
This method takes no parameters. It reads the image URL you passed to imageTransform and the settings of the instance.
Validation
- getUrl throws ArgumentError when the image URL is an empty string, because it has no URL to append the parameters to.
- getUrl checks no parameter value. The Image Delivery API judges the values when a client requests the URL and reports a rejection to that client, not to the SDK.
Additional Resource Refer to Resize Images for the request format of the Image Delivery API, including its environment parameter.
Behavior
- Client-side only. getUrl makes no request.
- It returns <image URL>?name=value&name=value, with the parameters in the order you first set them.
- It does not reset the instance. A second call returns the same URL plus any setting you add between the two calls.
- It always adds ? after the image URL as given:
- With no settings, the URL ends with ?.
- An image URL that already has a query string gets a second ?. Pass the image URL without its query string.
- It removes . and .. segments from the path of the image URL.
- Limitations:
- Does not add the environment parameter. Append &environment=<ENVIRONMENT> to the string when your setup needs it.
Example
Basic usage: build a resized WebP URL
import 'package:contentstack/contentstack.dart' as contentstack;
import 'package:contentstack/src/image/format.dart';
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')
..resize(width: 640, height: 480)
..convert(Format.Webp);
print(transform.getUrl()); // <IMAGE_URL>?width=640&height=480&format=webp
}Several sizes: one instance per URL
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
// No method removes a setting, so each size gets a new instance
for (final width in [320, 640, 1280]) {
final transform = stack.imageTransform('<IMAGE_URL>')..resize(width: width);
print(transform.getUrl());
}
}Edge case: add the environment parameter and catch an empty URL
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..quality(80);
// getUrl does not write environment, so append it to the string
print('${transform.getUrl()}&environment=<ENVIRONMENT>');
try {
stack.imageTransform('').getUrl();
} on ArgumentError catch (e) {
// Fires when the image URL is an empty string
print('Provide an image URL: $e');
}
}orientation
orientation flips or rotates the image.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| orient | Orientation | Yes | Not applicable | Flip or rotation, an Orientation value. |
Validation
- There is no client-side validation. Every Orientation value maps to a value from 1 to 8 that the API reference lists.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resource Refer to Orientation in the Image Delivery API reference for the values the API accepts.
Behavior
- orientation writes orient=<number> to the URL, with the number from the Orientation table on the ImageTransformation class page.
- The API reference states that the API corrects the orientation of an image whose EXIF data holds orientation details.
- Import Orientation from package:contentstack/src/image/orientation.dart. The examples below import it with the image prefix, which also avoids a clash with the Flutter Orientation enum.
Example
Basic usage: rotate the image 90 degrees right
import 'package:contentstack/contentstack.dart' as contentstack;
import 'package:contentstack/src/image/orientation.dart' as image;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')
..orientation(image.Orientation.Degrees90TowardsRight);
print(transform.getUrl()); // <IMAGE_URL>?orient=6
}Mirror image: flip the image horizontally
import 'package:contentstack/contentstack.dart' as contentstack;
import 'package:contentstack/src/image/orientation.dart' as image;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')
..orientation(image.Orientation.Horizontally);
print(transform.getUrl()); // <IMAGE_URL>?orient=2
}overlay
overlay places a second image on top of the image.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| overlayUrl | String | Yes | Not applicable | Relative URL of the image to place on top. |
| overlayAlign | String? (named) | No | API default (middle,center) | Position, for example left,bottom. |
| overlayRepeat | String? (named) | No | null (adds no overlay-repeat) | Repetition, x, y or both. |
| overlayWidth | int? (named) | No | null (adds no overlay-width) | Overlay width in pixels, 1 to 8192. |
| overlayHeight | int? (named) | No | null (adds no overlay-height) | Overlay height in pixels, 1 to 8192. |
Validation
- There is no client-side validation. overlay writes any string or int, including a value the API does not accept.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resources Refer to Overlay image, Align overlay, Repeat overlay horizontally, Set overlay width and Set overlay height in the Image Delivery API reference for the values the API accepts.
Behavior
- overlay writes overlay, then overlay-align, overlay-repeat, overlay-width and overlay-height for each named argument you pass.
- The SDK percent-encodes the path, so /v3/assets/... reaches the URL as %2Fv3%2Fassets%2F....
- The API reference lists these values:
- overlayAlign: top, bottom, left, right, middle or center, or two of them such as left,bottom.
- overlayRepeat: x for horizontal, y for vertical or both.
- The API crops an overlay that is larger than the image.
- Add padding around the overlay with the overlayPadding method.
- Limitations:
- Does not take a percentage such as 0.5 or a p value such as 300p for the overlay size, because overlayWidth and overlayHeight are int.
Example
Basic usage: place a logo asset on an image
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
try {
final response = await stack.asset('<ASSET_UID>').fetch();
if (response is Map && response.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${response['error_code']} ${response['error_message']}');
} else {
// overlay takes the path of the image URL, not the full URL
final logoPath = Uri.parse(response['asset']['url']).path;
final transform = stack.imageTransform('<IMAGE_URL>')..overlay(logoPath);
print(transform.getUrl()); // <IMAGE_URL>?overlay=%2Fv3%2Fassets%2F...
}
} catch (e) {
// Thrown for a missing UID, a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}All parameters: a repeated logo strip at the bottom right
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final logo = await stack.asset('<ASSET_UID>').fetch();
final logoPath = Uri.parse(logo['asset']['url']).path;
final transform = stack.imageTransform('<IMAGE_URL>')
..overlay(
logoPath,
overlayAlign: 'right,bottom',
overlayRepeat: 'x',
overlayWidth: 120,
overlayHeight: 40,
);
// <IMAGE_URL>?overlay=...&overlay-align=right%2Cbottom&overlay-repeat=x&overlay-width=120&overlay-height=40
print(transform.getUrl());
}overlayPadding
overlayPadding adds pixels to the edges of the overlay image.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| overlayPadding | String | Yes | Not applicable | Overlay padding for the top, right, bottom and left edges. |
Validation
- There is no client-side validation. overlayPadding writes any string, including a value the API does not accept.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resource Refer to Overlay pad in the Image Delivery API reference for the values the API accepts.
Behavior
- overlayPadding writes overlay-pad=<value> to the URL. The SDK encodes each comma as %2C.
- The padding applies to the image that the overlay method places, so set an overlay as well.
- The API reference takes pixels or percentage, in the order top, right, bottom, left, with CSS shorthand:
- 30,25,50 pads the left edge by the right value.
- 40,75 pads the top and bottom by 40, and the right and left by 75.
- 25 pads every edge by 25.
- The padding is white, or transparent when the overlay format supports transparency.
Example
Basic usage: pad a logo overlay
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
try {
final response = await stack.asset('<ASSET_UID>').fetch();
if (response is Map && response.containsKey('error_code')) {
// The API rejected the request
print('Request failed: ${response['error_code']} ${response['error_message']}');
} else {
// overlay takes the path of the image URL, not the full URL
final logoPath = Uri.parse(response['asset']['url']).path;
final transform = stack.imageTransform('<IMAGE_URL>')
..overlay(logoPath, overlayAlign: 'right,bottom')
..overlayPadding('30,25,50');
print(transform.getUrl()); // ...&overlay-pad=30%2C25%2C50
}
} catch (e) {
// Thrown for a missing UID, a non-JSON response, a network failure or a 30-second timeout
print('Request did not complete: $e');
}
}padding
padding adds pixels to the edges of the image, for whitespace or a border.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| padding | String | Yes | Not applicable | Padding for the top, right, bottom and left edges. |
Validation
- There is no client-side validation. padding writes any string, including a value the API does not accept.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resources Refer to Add padding and Add padding and background color in the Image Delivery API reference for the values the API accepts.
Behavior
- padding writes pad=<value> to the URL. The SDK encodes each comma as %2C.
- The API reference takes pixels or percentage, in the order top, right, bottom, left, with CSS shorthand:
- 25,50,75,100 pads each edge by its own value.
- 25,50,75 pads the left edge by the right value.
- 50,100 pads the top and bottom by 50, and the right and left by 100.
- 50 pads every edge by 50.
- The padding is white, or transparent when the image and the output format have transparency. Set a color with the bgColor method.
- The API ignores pad when the canvas method sets canvas.
Example
Basic usage: pad each edge by its own value
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..padding('25,50,75,100');
print(transform.getUrl()); // <IMAGE_URL>?pad=25%2C50%2C75%2C100
}Colored border: 10 pixels of blue on every edge
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')
..padding('10')
..bgColor('0000FF');
print(transform.getUrl()); // <IMAGE_URL>?pad=10&bg-color=0000FF
}quality
quality sets the compression level of an image in a lossy format.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| quality | int | Yes | Not applicable | Compression level, 1 to 100. |
Validation
- There is no client-side validation. quality writes any int, including a value outside the API range of 1 to 100.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resource Refer to Quality in the Image Delivery API reference for the values the API accepts.
Behavior
- quality writes quality=<value> to the URL.
- A lower value gives a smaller file and a lower quality.
- The API reference applies quality to JPEG, Progressive JPEG, AVIF and WebP lossy output only. The API ignores it for GIF and PNG.
- The API reference states that with quality alone, the API returns the original image when the output would be larger than it.
- Set the output format with the convert method.
Example
Basic usage: a progressive JPEG at quality 75
import 'package:contentstack/contentstack.dart' as contentstack;
import 'package:contentstack/src/image/format.dart';
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')
..convert(Format.Pjpg)
..quality(75);
print(transform.getUrl()); // <IMAGE_URL>?format=pjpg&quality=75
}resize
resize sets the width or height of the output image and can turn off upscaling.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| width | int? (named) | No | null (adds no width) | Output width in pixels, 1 to 8192. |
| height | int? (named) | No | null (adds no height) | Output height in pixels, 1 to 8192. |
| disable | bool? (named) | No | null (the API upscales) | true turns off upscaling. |
Validation
- There is no client-side validation. resize writes any int, including a size outside the API range of 1 to 8192.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resources Refer to Width, Height and Disable in the Image Delivery API reference for the values the API accepts.
Behavior
- resize writes width and height when you pass them, and disable=upscale when disable is true. A false value adds nothing.
- With one of the two sizes, the API keeps the aspect ratio of the image. With neither, it keeps the size of the source image.
- The API upscales an image when the output is larger than the source, unless the URL carries disable=upscale.
- The API reference states that width and height do not apply to GIF files.
- resize, the fit method and the resizeFilter method all write width and height. The latest call sets the values.
- Limitations:
- Does not take a percentage such as 0.5 or a p value such as 250p, because width and height are int. The fit method takes a double for a percentage.
Example
Basic usage: resize to 640 pixels wide
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..resize(width: 640);
print(transform.getUrl()); // <IMAGE_URL>?width=640
}All parameters: an exact size without upscaling
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')
..resize(width: 1200, height: 800, disable: true);
print(transform.getUrl()); // <IMAGE_URL>?width=1200&height=800&disable=upscale
}resizeFilter
resizeFilter sets the resampling filter the Image Delivery API uses when it resizes the image.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| width | int? (named) | No | null (adds no width) | Output width in pixels. |
| height | int? (named) | No | null (adds no height) | Output height in pixels. |
| filter | Filter? (named) | No | API default (lanczos3) | Resampling filter, a Filter value. |
Validation
- There is no client-side validation. resizeFilter writes any int size, including a value the API does not accept.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resources Refer to Resize image by nearest and Resize image by lanczos in the Image Delivery API reference for the values the API accepts.
Behavior
- resizeFilter writes width, height and resize-filter for each argument you pass. The filter value comes from the Filter table on the ImageTransformation class page.
- The API reference returns the original image when the URL has resize-filter without a width or height.
- resizeFilter, the resize method and the fit method all write width and height. The latest call sets the values.
- Import Filter from package:contentstack/src/image/filter.dart. The main library does not export it.
- Limitations:
- Does not offer the lanczos2 filter, because Filter has no such value. Filter.Lanczos sends lanczos3.
Example
All parameters: resize to 800 by 600 with the bicubic filter
import 'package:contentstack/contentstack.dart' as contentstack;
import 'package:contentstack/src/image/filter.dart';
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')
..resizeFilter(width: 800, height: 600, filter: Filter.Bicubic);
print(transform.getUrl()); // <IMAGE_URL>?width=800&height=600&resize-filter=bicubic
}Filter only: add a filter to an earlier resize
import 'package:contentstack/contentstack.dart' as contentstack;
import 'package:contentstack/src/image/filter.dart';
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')
..resize(width: 300)
..resizeFilter(filter: Filter.Nearest);
print(transform.getUrl()); // <IMAGE_URL>?width=300&resize-filter=nearest
}saturation
saturation raises or lowers the intensity of the colors in the image.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| saturation | int | Yes | Not applicable | Saturation change, -100 to 100. |
Validation
- There is no client-side validation. saturation writes any int, including a value outside the API range of -100 to 100.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resources Refer to Increase saturation and Decrease saturation in the Image Delivery API reference for the values the API accepts.
Behavior
- saturation writes saturation=<value> to the URL.
- A positive value intensifies the colors and a negative value mutes them. The API reference gives 0 as the API default, which leaves the image unchanged, and -100 for a grayscale image.
- The API also accepts decimals from -100.00 to 100.00.
- Limitations:
- Does not take a decimal value, because saturation takes an int.
Example
Basic usage: intensify the colors
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..saturation(20);
print(transform.getUrl()); // <IMAGE_URL>?saturation=20
}Grayscale: remove all color
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..saturation(-100);
print(transform.getUrl()); // <IMAGE_URL>?saturation=-100
}sharpen
sharpen increases the definition of the edges in the image.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| amount | int | Yes | Not applicable | Contrast added to edges, 0 to 10. |
| radius | int | Yes | Not applicable | Width of the sharpened band, 1 to 1000. |
| threshold | int | Yes | Not applicable | Edges to leave unchanged, 0 to 255. |
Validation
- There is no client-side validation. sharpen writes any int, including a value outside the ranges the API accepts.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resource Refer to Sharpen in the Image Delivery API reference for the values the API accepts.
Behavior
- sharpen writes sharpen=a<amount>,r<radius>,t<threshold> to the URL. The SDK encodes each comma as %2C.
- The API reference describes the three values:
- Amount: the contrast that the API adds to the edges, a decimal from 0 to 10.
- Radius: the width of the band of pixels that the API sharpens around each edge, a decimal from 1 to 1000 or a percentage such as 50p.
- Threshold: the minimum edge strength that the API sharpens, a whole number from 0 to 255.
- Limitations:
- Does not take a decimal amount or radius, or a p radius, because all three parameters are int.
Example
Basic usage: sharpen with amount 2, radius 1000 and threshold 2
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..sharpen(2, 1000, 2);
print(transform.getUrl()); // <IMAGE_URL>?sharpen=a2%2Cr1000%2Ct2
}trim
trim removes pixels from the edges of the image, such as a border or whitespace.
Returns: void. Nothing. The image transformation keeps the setting for its next URL.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| top | int? | No | null (adds no value for the edge) | Pixels to trim from the top edge. |
| right | int? | No | null (adds no value for the edge) | Pixels to trim from the right edge. |
| bottom | int? | No | null (adds no value for the edge) | Pixels to trim from the bottom edge. |
| left | int? | No | null (adds no value for the edge) | Pixels to trim from the left edge. |
Validation
- There is no client-side validation. trim writes any int, including a value larger than the image.
- With every argument null, trim adds nothing to the URL.
- When you pass right without top, trim writes the text null for the right edge, because it reads top in place of right.
- The Image Delivery API applies the value when a client requests the URL that the getUrl method returns. The API reports a rejection to that client, not to the SDK.
Additional Resource Refer to Trim in the Image Delivery API reference for the values the API accepts.
Behavior
- Known issue: trim writes the value of top where right belongs, so trim(25, 50, 75, 100) trims 25 pixels from the right edge, not 50. Append the parameter to the string that the getUrl method returns instead, for example '${transform.getUrl()}&trim=25,50,75,100'.
- The four parameters are optional positional parameters, in the order top, right, bottom, left.
- trim joins the values with a comma and a space, so trim(25, 50) produces trim=25%2C+25.
- trim leaves out each null value, so the values after it move up one place. trim(10, null, 20) sends 10, 20, which the API reads as top and bottom 10, right and left 20.
- The API reference takes CSS shorthand, in the order top, right, bottom, left. One value trims every edge by that value.
- Limitations:
- Does not take a percentage, because the parameters are int.
Example
Basic usage: trim every edge by 20 pixels
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..trim(20);
print(transform.getUrl()); // <IMAGE_URL>?trim=20
}All parameters: four values and the known issue
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..trim(25, 50, 75, 100);
// The right edge receives the top value, 25
print(transform.getUrl()); // <IMAGE_URL>?trim=25%2C+25%2C+75%2C+100
}Workaround: append the trim parameter to the URL
import 'package:contentstack/contentstack.dart' as contentstack;
Future<void> main() async {
final stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>');
final transform = stack.imageTransform('<IMAGE_URL>')..resize(width: 640);
print('${transform.getUrl()}&trim=25,50,75,100'); // <IMAGE_URL>?width=640&trim=25,50,75,100
}