Image block: uploads, captions, and resizing
Embed an image by URL, by upload, or by pasting a file.
Import
import { Image } from '@bloklabs/core/tools';Configuration
| Option | Type | Default | Description |
|---|---|---|---|
uploader | ImageUploader | undefined | Consumer-supplied uploader with optional |
types | string[] | ['image/*'] | Accepted MIME allowlist. Entries may be exact ( |
maxSize | MaxSizeConfig | 30 MiB | Max upload size. A number caps every type, in bytes. An object caps each MIME type, with |
onUploadError | UploadErrorHandler | undefined | Called when an upload fails. It receives
A rejection from your own uploader arrives as |
sources | 'upload' | 'url' | 'both' | 'both' | Restrict how an image may be added: file upload only, URL only, or both. |
convertGifToVideo | boolean | true | Auto-convert animated GIFs to a looping WebM video block on insert. It only applies when a video tool is registered. GIFs stay image blocks without one. Set false to always keep GIFs as image blocks. |
captionPlaceholder | string | "Write a caption…" | Placeholder text shown in the caption field. |
compress | boolean | ImageCompressionConfig | true | Re-encode uploaded images before they reach the uploader. It is on by default in a deliberately safe mode: the same format, quality 0.92, and the original dimensions. The result is only used when it saves at least 10%, otherwise the original bytes are uploaded untouched. Pass an object to opt into smaller output. The keys are
Set |
reloadAttempts | number | 5 | How many times a rendered image silently re-fetches its |
filters | Array<ImageFilterPreset | ImageFilterDefinition> | every built-in | The looks the image editor's Filters tab offers, in order. Original always comes first.
A built-in you leave out still renders on images that already use it. A saved name this editor does not know renders unfiltered and is kept on save. |
Save Data
interface ImageData {
url: string; // Image source URL — http(s) or blob:
caption?: string; // Plain-text caption
width?: number; // Width as percent of container, 10–100 (default 100)
alignment?: 'left' | 'center' | 'right';
size?: 'sm' | 'md' | 'lg' | 'full'; // Discrete size preset; overrides width when set
frame?: 'none' | 'border' | 'shadow'; // Decorative frame treatment (default 'none')
rounded?: boolean; // Rounded corners (default true)
captionVisible?: boolean; // Caption visible in the rendered state (default true)
crop?: ImageCrop; // Non-destructive crop rectangle, in the turned image's box
rotation?: 0 | 90 | 180 | 270; // Clockwise quarter turn (omitted for 0)
flipX?: boolean; // Mirrored left to right, before rotation
straighten?: number; // Fine turn in degrees, -45 to 45, after rotation
filter?: ImageFilterPreset | string; // A built-in look or a host filter name (omitted for 'none')
filterStrength?: number; // How strongly filter applies, 0 to 100 (omitted for 100)
adjust?: { brightness?: number; contrast?: number; saturation?: number }; // Each -100 to 100
markup?: ImageMarkup[]; // Drawings, shapes and text over the image, back to front
alt?: string; // Alt text for screen readers
fileName?: string; // Original filename, when known
naturalWidth?: number; // Intrinsic pixel width of the source (cached)
naturalHeight?: number; // Intrinsic pixel height of the source (cached)
variants?: { url: string; mimeType: string }[]; // Other formats, best first
}{
"id": "img001",
"type": "image",
"data": {
"url": "https://example.com/image.png",
"caption": "A cat",
"alt": "A cat",
"alignment": "center"
}
}Usage Example
import { Blok } from '@bloklabs/core';
import { Image } from '@bloklabs/core/tools';
const editor = new Blok({
holder: 'editor',
tools: {
image: {
class: Image,
config: {
uploader: {
async uploadByFile(file) {
const url = await myUpload(file);
return { url, fileName: file.name };
},
},
},
},
},
});