Redact in React Image Editor

03 Sep 202612 minutes to read

The redact feature in an React Image Editor is a valuable tool that empowers users to conceal sensitive information by applying blur or pixel effects to specific areas of an image. This feature is particularly valuable for protecting privacy and complying with data protection regulations, making it easier to securely share images without compromising sensitive information.

Apply redact to the image

The React Image Editor control includes a drawRedact method, which allows you to draw redaction on an image. This method takes six parameters that define how the redact should be carried out:

  • type: Specifies the type of redaction to be drawn on the image such as blur or pixelate. If not specified, the default type is blur.

  • x: Specifies the x-coordinate of the redact area in pixels. If not specified, the redact is positioned at the horizontal center of the image.

  • y: Specifies the y-coordinate of the redact area in pixels. If not specified, the redact is positioned at the vertical center of the image.

  • width: Specifies the width of the redact area in pixels. The default value is 100.

  • height: Specifies the height of the redact area in pixels. The default value is 50.

  • value: Specifies the blur radius (pixels) for blur type or the pixel size for pixelate type. The default value is 20 (pixels).

Selecting a redact

Use the selectRedact method to select a redact by id. Use getRedacts() to obtain redact objects (with ids). This method takes one parameter that defines how the redact should be selected:

  • id: Specifies the shape id of the redact to select.

Deleting a redact

Use the deleteRedact method to delete a redact by id. Use getRedacts() to obtain redact objects (with ids). This method takes one parameter that defines how the redact should be deleted:

  • id: Specifies the shape id of the redact to delete.

Updating a redact

Use the updateRedact method to update an existing redact. Use getRedacts() to obtain redact objects. This method takes two parameters that define how the redact should be updated:

  • setting: Specifies the redact settings to update for the shape.

  • isSelected: Specifies whether to show the redact in the selected state.

Getting redacts

Use the getRedacts method to get all redact details that are drawn on the React Image Editor.

Here’s an example demonstrating how to draw, select, delete, update, and get redacts on an image using the drawRedact, selectRedact, deleteRedact, updateRedact and getRedacts methods.

import { ImageEditorComponent, RedactType } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

export default class App extends React.Component {
    imgObj;
    imageEditorCreated() {
        if (Browser.isDevice) {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        }
        else {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    }
    addRedact() {
        let dimension = this.imgObj.getImageDimension();
        this.imgObj.drawRedact(RedactType.Blur, dimension?.x, dimension.y, 200, 300);
    }
    updateRedact() {
        let redacts = this.imgObj.getRedacts();
        if (redacts.length > 0) {
            redacts[redacts.length - 1].blurIntensity = 100;
            this.imgObj.updateRedact(redacts[redacts.length - 1]);
        }
    }
    selectRedact() {
        let redacts = this.imgObj.getRedacts();
        if (redacts.length > 0) {
            this.imageEditorObj?.selectRedact(redacts[redacts.length - 1].id);
        }
    }
    deleteRedact() {
        let redacts = this.imgObj.getRedacts();
        if (redacts.length > 0) {
            this.imgObj.deleteRedact(redacts[redacts.length - 1].id);
        }
    }
    render() {
        return (<div className='e-img-editor-sample'>
            <ImageEditorComponent ref={(img) => { this.imgObj = img; }} height="350px" created={this.imageEditorCreated.bind(this)} toolbar = {[]}>
            </ImageEditorComponent>
                    <div>
                        <ButtonComponent cssClass='e-primary' content='Add Redact' onClick={this.addRedact.bind(this)}/>
                        <ButtonComponent cssClass='e-primary' content='Update Redact' onClick={this.updateRedact.bind(this)}/>
                        <ButtonComponent cssClass='e-primary' content='Select Redact' onClick={this.selectRedact.bind(this)}/>
                        <ButtonComponent cssClass='e-primary' content='Delete Redact' onClick={this.deleteRedact.bind(this)}/>
                    </div>
                </div>);
    }
}
ReactDOM.render(<App />, document.getElementById('image-editor'));
import { ImageEditorComponent, RedactType, RedactSettings } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
    let imgObj: ImageEditorComponent;
    function imageEditorCreated(): void {
        if (Browser.isDevice) {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        } else {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    }
    function addRedact(): void {
        let dimension: any = imgObj.getImageDimension();
        imgObj.drawRedact(RedactType.Blur, dimension.x, dimension.y, 200, 300);
    }
    function updateRedact(): void {
        let redacts: RedactSettings[] = imgObj.getRedacts();
        if (redacts.length > 0) {
            redacts[redacts.length - 1].blurIntensity = 100;
            imgObj.updateRedact(redacts[redacts.length - 1]);
        }
    }
    function selectRedact(): void {
        let redacts: RedactSettings[] = imgObj.getRedacts();
        if (redacts.length > 0) {
            imgObj.selectRedact(redacts[redacts.length - 1].id);
        }
    }
    function deleteRedact(): void {
        let redacts: RedactSettings[] = imgObj.getRedacts();
        if (redacts.length > 0) {
            imgObj.deleteRedact(redacts[redacts.length - 1].id);
        }
    }

    return (
        <div className='e-img-editor-sample'>
        <ImageEditorComponent ref={(img) => { imgObj = img }} created={imageEditorCreated} toolbar = {[]}>
        </ImageEditorComponent>
                <div>
                    <ButtonComponent cssClass='e-primary' content='Add Redact' onClick = {addRedact}/>
                    <ButtonComponent cssClass='e-primary' content='Update Redact' onClick = {updateRedact}/>
                    <ButtonComponent cssClass='e-primary' content='Select Redact' onClick = {selectRedact}/>
                    <ButtonComponent cssClass='e-primary' content='Delete Redact' onClick = {deleteRedact}/>
                </div>
            </div>
    );
}
export default App;
ReactDOM.render(<App />, document.getElementById('image-editor'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Image Editor</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="https://cdn.syncfusion.com/ej2/35.2.12/tailwind3.css"rel="stylesheet">
	<link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
        <div id='image-editor'>
            <div id='loader'>Loading....</div>
        </div>
</body>

</html>