- Apply resize to the image
- Resizing event
Contact Support
Resize in the React Image Editor component
25 Mar 20258 minutes to read
The resize feature in an Image Editor is a valuable tool that empowers users to modify the size or dimensions of an image to meet their specific requirements. Whether it’s for printing, web display, or any other purpose, this feature allows users to tailor images to their desired specifications.
Apply resize to the image
The Image Editor control includes a resize
method, which allows you to adjust the size of an image. This method takes three parameters that define how the resizing should be carried out:
-
width: Specifies the resizing width of the image.
-
height: Specifies the resizing height of the image.
-
isAspectRatio: Specifies a boolean value indicating whether the image should maintain its original aspect ratio during resizing.
- When set to
true
, the image maintains its original aspect ratio. The width is applied as specified, and the height is automatically adjusted to maintain the aspect ratio. - When set to
false
, the image is resized according to the specified width and height, without maintaining the aspect ratio. - The default value is
false
.
- When set to
Here is an example of resizing the image using the resize
method.
import { ImageEditorComponent } 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 {
toolbar = [];
imgObj;
imageEditorCreated() {
if (Browser.isDevice) {
this.imgObj.open('https://ej2.syncfusion.com/react/demos/src/image-editor/images/flower.png');
}
else {
this.imgObj.open('https://ej2.syncfusion.com/react/demos/src/image-editor/images/bridge.png');
}
}
aspectClick() {
this.imgObj.resize(300, 400, true);
}
nonaspectClick() {
this.imgObj.resize(400, 100, false);
}
render() {
return (<div className='e-img-editor-sample'>
<ImageEditorComponent ref={(img) => { this.imgObj = img; }} height="350px" created={this.imageEditorCreated.bind(this)} toolbar = {this.toolbar}>
</ImageEditorComponent>
<div>
<ButtonComponent cssClass='e-primary' content='Aspect Ratio' onClick={this.aspectClick.bind(this)}/>
<ButtonComponent cssClass='e-primary' content='Non Aspect Ratio' onClick={this.nonaspectClick.bind(this)}/>
</div>
</div>);
}
}
ReactDOM.render(<App />, document.getElementById('image-editor'));
import { ImageEditorComponent } 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;
let toolbar = [];
function imageEditorCreated(): void {
if (Browser.isDevice) {
imgObj.open('https://ej2.syncfusion.com/react/demos/src/image-editor/images/flower.png');
} else {
imgObj.open('https://ej2.syncfusion.com/react/demos/src/image-editor/images/bridge.png');
}
}
function aspectClick(): void {
imgObj.resize(450, 342, true);
}
function nonaspectClick(): void {
imgObj.resize(450, 342, false);
}
return (
<div className='e-img-editor-sample'>
<ImageEditorComponent ref={(img) => { imgObj = img }} created={imageEditorCreated} toolbar = {toolbar}>
</ImageEditorComponent>
<div>
<ButtonComponent cssClass='e-primary' content='Aspect Ratio' onClick = {aspectClick}/>
<ButtonComponent cssClass='e-primary' content='Non Aspect Ratio' onClick = {nonaspectClick}/>
</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/29.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-navigations/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-image-editor/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/material.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>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='image-editor'>
<div id='loader'>Loading....</div>
</div>
</body>
</html>
Resizing event
The resizing
event is triggered when resizing the image. This event provides information encapsulated within an object, which includes details about the previous and current height and width of an image.
The parameter available in ResizeEventArgs
is,
-
ResizeEventArgs.previousWidth
- The width of the image before resizing is performed. -
ResizeEventArgs.previousHeight
- The height of the image before resizing is performed. -
ResizeEventArgs.width
- The width of the image after resizing is performed. -
ResizeEventArgs.height
- The width of the image after resizing is performed. -
ResizeEventArgs.isAspectRatio
- The type of resizing performed such as aspect ratio or non-aspect ratio. -
ResizeEventArgs.cancel
- Specifies a boolean value to cancel the resizing action.