@wordpress/image-cropper


目录

An implementation of react-easy-crop.

Current features

Future features

Installation

Install the module

npm install @wordpress/image-cropper --save

API

Components

interface ImageCropperProps {
    src: string; // Image source URL
    onLoad?: ( mediaSize: MediaSize ) => void; // Callback when image loads with media dimensions
    minZoom?: number; // Minimum zoom level (default: 1)
    maxZoom?: number; // Maximum zoom level (default: 5)
}

Hooks

import { useImageCropper } from '@wordpress/image-cropper';

const {
    // Unified state object
    cropperState, // Complete cropper state { crop, croppedArea, croppedAreaPixels, zoom, rotation, aspectRatio, flip, mediaSize }
    setCropperState, // Set multiple state properties at once

    // Actions
    reset, // Reset all changes (uses resetState if set)
    setResetState, // Set custom reset state
    getCroppedImage, // Get cropped image as data URL

    // Reset state
    resetState, // Current reset state configuration
    isDirty, // Whether the state is dirty based on resetState or default settings
} = useImageCropper();

Types

Utilities

import { normalizeRotation } from '@wordpress/image-cropper';

const normalized = normalizeRotation( -90 ); // Returns 270
const normalized2 = normalizeRotation( 450 ); // Returns 90

Usage

Basic Implementation

The image cropper provides the core cropping functionality without any built-in UI controls. You must implement your own tools using the useImageCropper hook.

import {
    ImageCropper,
    ImageCropperProvider,
    useImageCropper,
} from '@wordpress/image-cropper';

function ImageEditor() {
    return (
        <ImageCropperProvider>
            <div className="image-editor">
                <ImageCropper
                    src="https://example.com/image.jpg"
                    className="image-cropper"
                    onLoad={ ( mediaSize ) =>
                        console.log( 'Image loaded', mediaSize )
                    }
                />
                <ImageEditorTools />
            </div>
        </ImageCropperProvider>
    );
}

function ImageEditorTools() {
    const { cropperState, setCropperState, reset, setResetState } =
        useImageCropper();

    const { zoom, rotation, aspectRatio, flip, mediaSize, croppedArea } =
        cropperState;

    const handleSave = () => {
        console.log( 'Cropper state:', {
            crop: croppedArea,
            zoom,
            rotation,
            aspectRatio,
            flip,
        } );
        // Apply the crop state to your image
    };

    return (
        <div className="image-editor-tools">
            <button
                onClick={ () => setCropperState( { rotation: rotation + 90 } ) }
            >
                Rotate Right
            </button>
            <button
                onClick={ () => setCropperState( { rotation: rotation - 90 } ) }
            >
                Rotate Left
            </button>
            <button
                onClick={ () =>
                    setCropperState( {
                        flip: { ...flip, horizontal: ! flip.horizontal },
                    } )
                }
            >
                Flip Horizontal
            </button>
            <button
                onClick={ () =>
                    setCropperState( {
                        flip: { ...flip, vertical: ! flip.vertical },
                    } )
                }
            >
                Flip Vertical
            </button>
            <button onClick={ handleSave }>Apply Changes</button>
            <button onClick={ reset }>Reset</button>
        </div>
    );
}

Advanced Usage with Custom Reset State

You can configure a custom initial state that will be used when the reset function is called:

function ImageEditorWithCustomReset() {
    const { setResetState, reset } = useImageCropper();

    // Set custom reset state
    useEffect( () => {
        setResetState( {
            rotation: 90,
            zoom: 1.5,
            aspectRatio: 16 / 9,
            flip: { horizontal: false, vertical: false },
        } );
    }, [ setResetState ] );

    return (
        <div>
            { /* Your cropper components */ }
            <button onClick={ reset }>Reset to Custom State</button>
        </div>
    );
}

版本号 #2
由 壳壳虫 创建于 2026-10-31 18:22:15 UTC
由 壳壳虫 更新于 2026-10-31 18:22:17 UTC