1e41f4b71Sopenharmony_ci# image
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci>  **NOTE**
4e41f4b71Sopenharmony_ci>
5e41f4b71Sopenharmony_ci>  This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciThe **\<image>** component is used to render and display images.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci## Child Components
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ciNot supported
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci## Attributes
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ciIn addition to the [universal attributes](js-components-common-attributes.md), the following attributes are supported.
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci| Name  | Type    | Default Value | Mandatory  | Description                                      |
20e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---- | ---------------------------------------- |
21e41f4b71Sopenharmony_ci| src  | string | -    | No   | Image path, which supports local paths. The supported image formats include PNG, JPG, BMP, SVG, and GIF.<br>- The Base64 string<sup>6+</sup> is supported in the following format: data:image/[png \| jpeg \| bmp \| webp];base64, [base64 data], where **[base64 data]** is a Base64 string.<br>- The path prefix of **dataability://** is supported, which allows access to the image path provided by the Data ability.<sup>6+</sup>|
22e41f4b71Sopenharmony_ci| alt  | string | -    | No   | Alternative information for the image, which is displayed during image loading.                       |
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci## Styles
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ciIn addition to the [universal styles](js-components-common-styles.md), the following styles are supported.
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci| Name                          | Type     | Default Value         | Mandatory  | Description                                      |
30e41f4b71Sopenharmony_ci| ---------------------------- | ------- | ------------ | ---- | ---------------------------------------- |
31e41f4b71Sopenharmony_ci| object-fit                   | string  | cover        | No   | Image scale type. This style is not supported for SVG images. For details about available values, see **object-fit**.|
32e41f4b71Sopenharmony_ci| match-text-direction         | boolean | false        | No   | Whether image orientation changes with the text direction. This style is not supported for SVG images.                    |
33e41f4b71Sopenharmony_ci| fit-original-size            | boolean | false        | No   | Whether the **\<image>** component adapts to the image source size when its width and height are not set. If this style is set to **true**, **object-fit** will not take effect. This style is not supported for SVG images.|
34e41f4b71Sopenharmony_ci| object-position<sup>7+</sup> | string  | 0px 0px | No   | Position of the image in the component.<br>The options are as follows:<br>1. Pixels, in px. For example, **15px 15px** indicates the position to move along the x-axis or y-axis.<br>2. Characters. Optional values are as follows:<br>- **left**: The image is displayed on the left of the component.<<br>- **top**: The image is displayed on the top of the component.<br>- **right**: The image is displayed on the right of the component.<br>- **bottom**: The image is displayed at the bottom of the component.|
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci**Table 1** object-fit
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci| Type        | Description                                  |
39e41f4b71Sopenharmony_ci| ---------- | ------------------------------------ |
40e41f4b71Sopenharmony_ci| cover      | The image is scaled with its aspect ratio retained for both sides to be greater than or equal to the display boundaries and displayed in the middle.|
41e41f4b71Sopenharmony_ci| contain    | The image is scaled with the aspect ratio retained for the image to be completely displayed within the display boundaries and displayed in the middle.  |
42e41f4b71Sopenharmony_ci| fill       | The image is scaled to fill the display area, and its aspect ratio is not retained.           |
43e41f4b71Sopenharmony_ci| none       | The image is displayed in the middle with its aspect ratio and size retained.                       |
44e41f4b71Sopenharmony_ci| scale-down | The image is displayed in the middle with its aspect ratio retained. The size is equal to or smaller than the original size.               |
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci>  **NOTE**
47e41f4b71Sopenharmony_ci>
48e41f4b71Sopenharmony_ci>  When using an SVG image, note that:
49e41f4b71Sopenharmony_ci>
50e41f4b71Sopenharmony_ci>  - The SVG image will not be drawn if the length or width of the **\<image>** component is infinity.
51e41f4b71Sopenharmony_ci>
52e41f4b71Sopenharmony_ci>  - If the image length and width are not specified in the SVG description, the SVG image fills the **\<image>** component area.
53e41f4b71Sopenharmony_ci>
54e41f4b71Sopenharmony_ci>  - If the image length and width are specified in the SVG description, the following rules are adopted to decide the final display effect:
55e41f4b71Sopenharmony_ci>
56e41f4b71Sopenharmony_ci>  1. If the **\<image>** component is too small to afford the SVG image, the SVG image is cropped and only its upper left part is displayed in the component.
57e41f4b71Sopenharmony_ci>
58e41f4b71Sopenharmony_ci>  2. If the **\<image>** component is big enough to afford the SVG image, this SVG image is displayed in the upper left corner of the component.
59e41f4b71Sopenharmony_ci>
60e41f4b71Sopenharmony_ci>  - For SVG images, only the following tags are included in the supported list: **svg**, **rect**, **circle**, **ellipse**, **path**, **line**, **polyline**, **polygon**, **animate**, **animateMotion**, and **animateTransform**.
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci## Events
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ciIn addition to the [universal events](js-components-common-events.md), the following events are supported.
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci| Name      | Parameter                                      | Description                       |
68e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ------------------------- |
69e41f4b71Sopenharmony_ci| complete | {<br> width: width,<br> height: height<br> } | Triggered when an image is successfully loaded. The loaded image size is returned.|
70e41f4b71Sopenharmony_ci| error    | {<br> width: width,<br> height: height<br> } | Triggered when an exception occurs during image loading. In this case, the width and height are **0**.  |
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci## Methods
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ciThe [universal methods](js-components-common-methods.md) are supported.
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci## Example
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci```html
80e41f4b71Sopenharmony_ci<!-- xxx.hml -->
81e41f4b71Sopenharmony_ci<div class="container">
82e41f4b71Sopenharmony_ci  <image src="common/images/example.png" style="width: 300px; height: 300px; object-fit:{{fit}}; object-position: center center; border: 1px solid red;">
83e41f4b71Sopenharmony_ci  </image>
84e41f4b71Sopenharmony_ci  <select class="selects" onchange="change_fit"><option for="{{fits}}" value="{{$item}}">{{$item}}</option></select>
85e41f4b71Sopenharmony_ci</div>
86e41f4b71Sopenharmony_ci```
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci```css
89e41f4b71Sopenharmony_ci/* xxx.css */
90e41f4b71Sopenharmony_ci.container {
91e41f4b71Sopenharmony_ci  justify-content: center;
92e41f4b71Sopenharmony_ci  align-items: center;
93e41f4b71Sopenharmony_ci  flex-direction: column;
94e41f4b71Sopenharmony_ci}
95e41f4b71Sopenharmony_ci.selects{
96e41f4b71Sopenharmony_ci  margin-top: 20px;
97e41f4b71Sopenharmony_ci  width:300px;
98e41f4b71Sopenharmony_ci  border:1px solid #808080;
99e41f4b71Sopenharmony_ci  border-radius: 10px;
100e41f4b71Sopenharmony_ci}
101e41f4b71Sopenharmony_ci```
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci```js
104e41f4b71Sopenharmony_ci// xxx.js
105e41f4b71Sopenharmony_ciexport default {
106e41f4b71Sopenharmony_ci  data: {
107e41f4b71Sopenharmony_ci    fit:"cover",
108e41f4b71Sopenharmony_ci    fits: ["cover", "contain", "fill", "none", "scale-down"],
109e41f4b71Sopenharmony_ci  },
110e41f4b71Sopenharmony_ci  change_fit(e) {
111e41f4b71Sopenharmony_ci    this.fit = e.newValue;
112e41f4b71Sopenharmony_ci  },
113e41f4b71Sopenharmony_ci}
114e41f4b71Sopenharmony_ci```
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci![example](figures/example.gif)
117