Image Editor
ImageEditor is a block component which enables basic image editing functionality such as:
- cropping, resizing
- rotating,
- changing aspect ratio
- flipping
- resetting to original state
It requires a media object to work properly. You can also pass a site ID to which the edited image belongs to.
Props
siteId
| Type | Number |
|---|---|
| Required | No |
| Default | selected site |
Id of a site the edited image belongs to.
media
| Type | Object |
|---|---|
| Required | Yes |
This object needs to contain at least one of these properties:
media.URL {string}: the url of the image to be edited (e.g. https://my-site.com/full-width1-e1.jpg).
Use this approach if you want to load and edit a remote image file.
or
media.src {string}: the object url of
the image to be edited. Use this approach if you want to edit a local image file (e.g. uploaded file or blob).
It can also contain these optional properties (with defaults if not set):
media.file{string}: the base name of the edited image file (e.g.full-width1-e1.jpg), defaults todefaultmedia.ID{number}: An ID of the media item.media.mime_type{string}: the MIME of the edited image (e.g.image/jpeg), defaults toimage/pngmedia.title{string}: the title of the edited image (e.g.some image file), defaults todefault
defaultAspectRatio
| Type | string |
|---|---|
| Required | No |
| Default | 'FREE' |
Default, pre-selected aspect ratio for the image editor. If allowedAspectRatios prop is present as well,
it must include the defaultAspectRatio. For the list of all possible aspect ratios, see
client/state/editor/image-editor/constants.
allowedAspectRatios
| Type | array |
|---|---|
| Required | No |
| Default | all possible aspect ratios |
List allowed aspect ratios user can select when editing an image. If there is only a single specified aspect ratio in
the allowedAspectRatios array, it will be set as defaultAspectRatio as well.
For the list of all possible aspect ratios, see client/state/editor/image-editor/constants.
onDone
| Type | Function |
|---|---|
| Required | No |
A function which will get called on extracting an edited image after clicking the "Done" button. It receives three arguments:
- a JS
Errorobject if image is not loaded/present, otherwisenull - the extracted image in form of
Blobobject ornullif image is not loaded/present - the props of the image editor which include image meta and functions to reset image editor state (for the full list,
have a look into the
image-editor/indexfile)
onCancel
| Type | Function |
|---|---|
| Required | No |
A function which will get called on clicking the "Cancel" image editor button. If this prop is omitted, the "Cancel" button won't be rendered. The function receives one argument: the props of the image editor.
onReset
| Type | Function |
|---|---|
| Required | No |
A function which will get called on clicking the "Reset" image editor button. The function is called after image editor's state is reset. The function receives one argument: the props of the image editor.
className
| Type | string |
|---|---|
| Required | No |
String of classes (class names) appended to the image editor wrapper (.image-editor [className]).
doneButtonText
| Type | string |
|---|---|
| Required | No |
Already-translated string which will be used on the 'Done' button. If not used, it will default to 'Done'.
Example
import ImageEditor from 'calypso/blocks/image-editor';
function render() {
return <ImageEditor siteId={ siteId } media={ { URL: 'http://example.com/image.jpg' } } />;
}