Image Editor Recipes

Status: experimental. This API may change in any release.

To mount the canvas, start from the Quick Start.

Add Controls

Read the limits from the controller, so your controls stop where the editor does.

const { state, setZoom, snapRotate90, toggleFlip, reset } = controller;

return (
    <>
        <button
            disabled={ state.zoom >= controller.maxZoom }
            onClick={ () =>
                setZoom( Math.min( controller.maxZoom, state.zoom + 0.5 ) )
            }
        >
            Zoom in
        </button>
        <button onClick={ () => snapRotate90( 1 ) }>Rotate right</button>
        <button onClick={ () => toggleFlip( 'horizontal' ) }>
            Flip horizontal
        </button>
        <button onClick={ () => reset() } disabled={ ! controller.isDirty }>
            Reset
        </button>
        <ImageEditorCanvas src={ src } controller={ controller } />
    </>
);

Offer Aspect-Ratio Presets

The controller takes a width / height ratio, or undefined for a free crop. Keep your own list of presets, and keep the selected one in your own state. To offer the image’s own ratio, work it out from controller.state.image.

const PRESETS = [
    { label: 'Free', value: 'free' },
    { label: 'Original', value: 'original' },
    { label: 'Square', value: '1' },
    { label: 'Landscape (16:9)', value: String( 16 / 9 ) },
];

function PresetPicker( { controller }: { controller: ImageEditorController } ) {
    const [ preset, setPreset ] = useState( 'free' );
    const { image } = controller.state;

    return (
        <select
            value={ preset }
            onChange={ ( event ) => {
                const { value } = event.target;
                setPreset( value );
                if ( value === 'free' ) {
                    controller.setAspectRatio( undefined );
                } else if ( value === 'original' ) {
                    controller.setAspectRatio(
                        image ? image.naturalWidth / image.naturalHeight : undefined
                    );
                } else {
                    controller.setAspectRatio( parseFloat( value ) );
                }
            } }
        >
            { PRESETS.map( ( { label, value } ) => (
                <option key={ value } value={ value }>
                    { label }
                </option>
            ) ) }
        </select>
    );
}

Lock the Crop to a Ratio

For a fixed shape, such as a square site icon, start from a state with a locked ratio and pass freeformCrop={ false }. The crop stays the largest square that fits, centred, while the person pans and zooms the image under it.

Leave freeformCrop at its default to let them move and resize the crop instead. The locked ratio holds either way.

Pass the image too if you have it, so the crop starts the right shape rather than waiting for the canvas to fit it.

const [ state, setState ] = useState( () =>
    createImageEditorState( { aspectRatio: 1 } )
);
const controller = useImageEditor( { state, onChange: setState } );

return (
    <ImageEditorCanvas
        src={ src }
        controller={ controller }
        freeformCrop={ false }
    />
);

Start From a Saved Edit

Store the whole state object, and start from it next time. It is kept when the image loads.

const [ state, setState ] = useState< ImageEditorState >( () => savedState );

Keep an Undo History

Because you keep the state, you can keep earlier states too. A minimal history, with a drag recorded as one entry:

import { useRef, useState } from '@wordpress/element';
import {
    ImageEditorCanvas,
    areImageEditorStatesEqual,
    createImageEditorState,
    useImageEditor,
    type ImageEditorState,
} from '@wordpress/image-editor';

function ImageEditorWithHistory( { src }: { src: string } ) {
    const [ state, setState ] = useState( () => createImageEditorState() );
    const [ past, setPast ] = useState< ImageEditorState[] >( [] );
    const gestureStart = useRef< ImageEditorState | null >( null );

    const controller = useImageEditor( {
        state,
        onChange: ( next, { isEdit } ) => {
            if ( isEdit && ! gestureStart.current ) {
                setPast( ( entries ) => [ ...entries, state ] );
            }
            setState( next );
        },
    } );

    return (
        <>
            <button
                disabled={ ! past.length }
                onClick={ () => {
                    setState( past[ past.length - 1 ] );
                    setPast( past.slice( 0, -1 ) );
                } }
            >
                Undo
            </button>
            <ImageEditorCanvas
                src={ src }
                controller={ controller }
                onGestureStart={ () => {
                    gestureStart.current = state;
                } }
                onGestureEnd={ () => {
                    const start = gestureStart.current;
                    gestureStart.current = null;
                    if ( start && ! areImageEditorStatesEqual( start, state ) ) {
                        setPast( ( entries ) => [ ...entries, start ] );
                    }
                } }
            />
        </>
    );
}

isEdit is false for an image loading or the crop being refitted after a window resize, so those do not get an undo entry. The media editor’s session (packages/media-editor/src/state) is a fuller version of this, with redo and other edits in the same history.

Save the Edit

getImageEdit( state ) describes the edit against the full-size image loaded in state.image: an optional flip, rotation and crop, to apply in that order. The README says what each part measures.

For example, the WordPress media REST endpoint (/wp/v2/media/<id>/edit) applies modifiers in the order they arrive, with the crop in percentages. mapImageEdit( edit, handlers ) calls one handler for each part the edit has, in the order the parts apply, so the list comes out in the right order:

import apiFetch from '@wordpress/api-fetch';
import { getImageEdit, mapImageEdit } from '@wordpress/image-editor';
import type { Flip } from '@wordpress/image-editor';

type Modifier =
    | { type: 'flip'; args: { flip: Flip } }
    | { type: 'rotate'; args: { angle: number } }
    | {
            type: 'crop';
            args: { left: number; top: number; width: number; height: number };
      };

const edit = getImageEdit( controller.state );
const modifiers = edit
    ? mapImageEdit< Modifier >( edit, {
            flip: ( flip ) => ( { type: 'flip', args: { flip } } ),
            rotation: ( angle ) => ( { type: 'rotate', args: { angle } } ),
            crop: ( { x, y, width, height } ) => ( {
                type: 'crop',
                args: {
                    left: x * 100,
                    top: y * 100,
                    width: width * 100,
                    height: height * 100,
                },
            } ),
      } )
    : [];

if ( modifiers.length ) {
    await apiFetch( {
        path: `/wp/v2/media/${ attachmentId }/edit`,
        method: 'POST',
        data: { src: imageUrl, modifiers },
    } );
}

An editor that uploads the result as a new file, rather than asking the server to edit the original, exports it in the browser instead. exportImage( state, options? ) resolves with a Blob of the framed region at the size getOutputSize( state ) reports:

import { exportImage } from '@wordpress/image-editor';

const file = await exportImage( controller.state, {
    type: 'image/jpeg',
    quality: 0.9,
} );

Apply an Edit From Code

applyImageEdit( state, edit ) turns an edit back into a state. With a canvas, store result.state like any other change. Without one, export it:

import {
    applyImageEdit,
    createImageEditorState,
    exportImage,
} from '@wordpress/image-editor';

// A quarter turn clockwise, then the top half of the turned photo.
const result = applyImageEdit(
    createImageEditorState( {
        image: { src: photoUrl, naturalWidth: 1600, naturalHeight: 900 },
    } ),
    { rotation: 90, crop: { x: 0, y: 0, width: 1, height: 0.5 } }
);

// Without strict, only `no-image` or `invalid` can reject.
if ( 'rejected' in result ) {
    throw new Error( `The edit was rejected: ${ result.rejected }` );
}

const file = await exportImage( result.state, { type: 'image/jpeg' } );

Accessibility Notes

  • The crop area is focusable and keyboard-operable, and changes are announced through an ARIA live region.
  • Arrow keys pan the image. Hold Shift for bigger steps.
  • + or = zooms in, and - or _ zooms out.
  • R rotates 90 degrees clockwise, and Shift+R rotates counter-clockwise.
  • H flips horizontally, and V flips vertically.
  • With resize handles (the default), each handle is a button. Arrow keys resize from the focused handle; hold Shift for bigger steps.