File size: 4,302 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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`

<table>
	<tr><th>Type</th><td>Number</td></tr>
	<tr><th>Required</th><td>No</td></tr>
	<tr><th>Default</th><td>selected site</td></tr>
</table>

Id of a site the edited image belongs to.

### `media`

<table>
	<tr><th>Type</th><td>Object</td></tr>
	<tr><th>Required</th><td>Yes</td></tr>
</table>

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](https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL) 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 to `default`
- `media.ID` `{number}`: An ID of the media item.
- `media.mime_type` `{string}`: the MIME of the edited image (e.g. `image/jpeg`), defaults to `image/png`
- `media.title` `{string}`: the title of the edited image (e.g. `some image file`), defaults to `default`

### `defaultAspectRatio`

<table>
	<tr><th>Type</th><td>string</td></tr>
	<tr><th>Required</th><td>No</td></tr>
	<tr><th>Default</th><td>'FREE'</td></tr>
</table>

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`

<table>
	<tr><th>Type</th><td>array</td></tr>
	<tr><th>Required</th><td>No</td></tr>
	<tr><th>Default</th><td>all possible aspect ratios</td></tr>
</table>

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`

<table>
	<tr><th>Type</th><td>Function</td></tr>
	<tr><th>Required</th><td>No</td></tr>
</table>

A function which will get called on extracting an edited image after clicking the "Done" button.
It receives three arguments:

- a JS `Error` object if image is not loaded/present, otherwise `null`
- the extracted image in form of `Blob` object or `null` if 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/index` file)

### `onCancel`

<table>
	<tr><th>Type</th><td>Function</td></tr>
	<tr><th>Required</th><td>No</td></tr>
</table>

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`

<table>
	<tr><th>Type</th><td>Function</td></tr>
	<tr><th>Required</th><td>No</td></tr>
</table>

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`

<table>
	<tr><th>Type</th><td>string</td></tr>
	<tr><th>Required</th><td>No</td></tr>
</table>

String of classes (class names) appended to the image editor wrapper (`.image-editor [className]`).

### `doneButtonText`

<table>
	<tr><th>Type</th><td>string</td></tr>
	<tr><th>Required</th><td>No</td></tr>
</table>

Already-translated string which will be used on the 'Done' button. If not used, it will default to 'Done'.

## Example

```js
import ImageEditor from 'calypso/blocks/image-editor';

function render() {
	return <ImageEditor siteId={ siteId } media={ { URL: 'http://example.com/image.jpg' } } />;
}
```