Skip to main content

VIUCraft Documentation

Home Getting Started API Reference Integration Guides Pricing & Plans SDKs Playground URL Builder

JavaScript SDK - Image Transformations

On This Page

The ImageBuilder class provides a chainable API for building image transformation URLs. Access it via client.image(imageId).

Typescript
const url = client.image('img-uuid')
  .resize(800, 600)
  .grayscale()
  .quality(80)
  .setFormat('webp')
  .toURL();

Every method returns the builder instance, so you can chain as many transformations as you need.

resize(width, height, scale?)

Resize the image to the given dimensions.

ParameterTypeRequiredDescription
widthnumberYesTarget width in pixels (1–16384)
heightnumberYesTarget height in pixels (1–16384)
scalenumberNoOptional scale factor (positive number)
Typescript
client.image('img').resize(400, 300).toURL();
client.image('img').resize(400, 300, 2).toURL(); // with scale

crop(left, top, width, height)

Crop a rectangular region from the image.

ParameterTypeRequiredDescription
leftnumberYesX offset in pixels (>= 0)
topnumberYesY offset in pixels (>= 0)
widthnumberYesCrop width in pixels (1–16384)
heightnumberYesCrop height in pixels (1–16384)
Typescript
client.image('img').crop(10, 20, 400, 300).toURL();

rotate(angle, background?)

Rotate the image by the given angle.

ParameterTypeRequiredDescription
anglenumberYesRotation angle in degrees (any finite number)
backgroundstringNoBackground fill color as hex code (e.g. "#ff0000")
Typescript
client.image('img').rotate(90).toURL();
client.image('img').rotate(45, '#ffffff').toURL();

brightness(factor)

Adjust image brightness.

ParameterTypeRequiredDescription
factornumberYesBrightness factor (0–10)
Typescript
client.image('img').brightness(1.5).toURL();

contrast(factor)

Adjust image contrast.

ParameterTypeRequiredDescription
factornumberYesContrast factor (0–10)
Typescript
client.image('img').contrast(1.5).toURL();

grayscale()

Convert the image to grayscale. Takes no parameters.

Typescript
client.image('img').grayscale().toURL();

invert()

Invert the image colors. Takes no parameters.

Typescript
client.image('img').invert().toURL();

blur(sigma?)

Apply a Gaussian blur.

ParameterTypeRequiredDescription
sigmanumberNoBlur radius (0.01–100, default 1.0)
Typescript
client.image('img').blur().toURL();       // sigma = 1.0
client.image('img').blur(3.5).toURL();    // sigma = 3.5

sharpen(sigma?)

Sharpen the image.

ParameterTypeRequiredDescription
sigmanumberNoSharpen radius (0.01–100, default 1.0)
Typescript
client.image('img').sharpen().toURL();
client.image('img').sharpen(2.0).toURL();

emboss()

Apply an emboss effect. Takes no parameters.

Typescript
client.image('img').emboss().toURL();

median(size?)

Apply a median filter (noise reduction).

ParameterTypeRequiredDescription
sizenumberNoFilter size — must be a positive odd integer (1–99, default 3)
Typescript
client.image('img').median().toURL();     // size = 3
client.image('img').median(5).toURL();    // size = 5

thumbnail(width, height, crop?)

Create a thumbnail, optionally using a smart crop strategy.

ParameterTypeRequiredDescription
widthnumberYesThumbnail width (1–16384)
heightnumberYesThumbnail height (1–16384)
cropstringNoCrop strategy: 'centre', 'entropy', or 'attention'
Typescript
client.image('img').thumbnail(150, 150).toURL();
client.image('img').thumbnail(150, 150, 'attention').toURL();

smartCrop(width, height)

Automatically crop the image to the most interesting region.

ParameterTypeRequiredDescription
widthnumberYesTarget width (1–16384)
heightnumberYesTarget height (1–16384)
Typescript
client.image('img').smartCrop(400, 400).toURL();

flip(direction)

Flip the image horizontally or vertically.

ParameterTypeRequiredDescription
directionstringYes'horizontal', 'vertical', 'h', or 'v'
Typescript
client.image('img').flip('horizontal').toURL();
client.image('img').flip('v').toURL();

quality(value)

Set the output compression quality.

ParameterTypeRequiredDescription
valuenumberYesQuality from 1 (lowest) to 100 (highest)
Typescript
client.image('img').quality(85).toURL();

setFormat(format)

Set the output image format.

ParameterTypeRequiredDescription
formatstringYes'jpg', 'jpeg', 'png', or 'webp'
Typescript
client.image('img').setFormat('webp').quality(80).toURL();

useShort(enabled?)

Switch to the short URL format. Pass false to switch back.

ParameterTypeRequiredDescription
enabledbooleanNoEnable short format (default true)
Typescript
client.image('img').resize(300, 200).useShort().toURL();
// => .../resize-300-200/img.jpg

Short Format Mapping

When useShort() is enabled, operation names are abbreviated:

Standard SegmentShort Segment
resize_width_W_height_Hresize-W-H
crop_left_L_top_T_width_W_height_Hcrop-L-T-W-H
rotate_angle_Arotate-A
brightness_factor_Fbright-F
contrast_factor_Fcon-F
grayscalegray
invertinv
blur_sigma_Sblur-S
sharpen_sigma_Ssharp-S
embossemb
median_size_Smed-S
thumbnail_width_W_height_Hthumb-W-H
smartcrop_width_W_height_Hscrop-W-H
flip_direction_Dflip-h / flip-v
quality_value_Vq-V

Validation

All parameters are validated before the URL is built. Invalid values throw a ViucraftValidationError immediately — no network request is made.

Typescript
client.image('img').resize(-1, 100); // throws ViucraftValidationError
client.image('img').quality(200);    // throws ViucraftValidationError
client.image('img').median(4);       // throws ViucraftValidationError (must be odd)

Combining Transformations

Chain as many operations as you need:

Typescript
const url = client.image('photo-uuid')
  .resize(1200, 800)
  .brightness(1.2)
  .contrast(1.1)
  .sharpen(1.5)
  .quality(90)
  .setFormat('webp')
  .toURL();

Inspecting Instructions

Use getInstructions() to see the current state of the builder:

Typescript
const builder = client.image('img').resize(800, 600).grayscale();
console.log(builder.getInstructions());
// { resize: { width: 800, height: 600 }, grayscale: true }

Was this helpful?

On This Page