# Image

> Insert an image with alternative text and optional dimensions.

`image()` adds a block image with a source, alternative text, optional title, and optional dimensions. Use `image_with_placement(Placement::Inline)` for images inside text.

## Try it

[Try the image editor example](https://gpdoc.dev/docs/plugins/media/image/#image-example)

## Install

```sh
cargo add gpdoc-plugin-image
```

## Example

Register the view that draws each image at its stored size in the [Quickstart editor configuration](/docs/quickstart/#configure-the-editor).

```rust
use gpdoc::{EditorConfig, PluginViewExt, ViewPlugin};
use gpdoc_plugin_image::{image, image_content};
use gpdoc_plugin_paragraph::paragraph;


let config = EditorConfig::builder()
    .plugin(paragraph())
    .plugin(image().with_view(image_tools))
    .build()
    .expect("configure editor");


fn image_tools() -> ViewPlugin {
    image_content()
}
```

Type `![A swallow](images/swallow.png)` at the start of a paragraph. Supply the asset at that path to see the image. The editor shows the alt text while the image is unavailable.

## Compose an image control

Use `image_controls` to place a GPUI element over the picture. The editor clips the element to the image bounds.

### Anatomy

```text
image_controls(...)       Draws the picture and supplies its ImageNode
└── div()                 Your overlay
    └── div().on_click()  Your button to apply image.resize(...)
```

### Restore the original image size

In the example above, replace the `image_content` import with `image_controls`. Use this version of `image_tools` to add a reset button to images with a stored width or height.

```rust
use gpui::{div, prelude::*};


fn image_tools() -> ViewPlugin {
    image_controls(|image, _, _| {
        let overlay = div();
        if image.dimensions() == (None, None) {
            return overlay;
        }


        let reset = image.resize(None, None);
        let editor = image.slot.view.clone();
        let button = div()
            .id(("original-image-size", image.slot.before.0))
            .child("Original size")
            .on_click(move |_, window, cx| {
                editor.update(cx, |view, cx| {
                    if let Err(error) = view.apply_node_view(reset.clone(), window, cx) {
                        eprintln!("Could not resize image: {error}");
                    }
                });
            });
        overlay.child(button)
    })
}
```

Insert the 240-pixel image from the `setImage` example below. The image shows an **Original size** button. Click the button to clear both stored dimensions. Undo restores the width and the button. To set a specific size, pass a positive pixel value to `resize` for either dimension. Leaving one dimension at `None` preserves the aspect ratio.

## Settings

### `placement`

`image()` uses `Placement::Block`. To keep pictures inline with text, register `image_with_placement(placement: Placement)` in your config.

```rust
let image = gpdoc_plugin_image::image_with_placement(gpdoc_plugin::core::Placement::Inline);
```

## Commands

### `setImage`

Inserts an image at the caret or replaces the selection. Supply the image source and any optional display attributes.

```rust
editor.run(gpdoc_plugin_image::Set {
    src: "images/swallow.png".into(),
    alt: Some("A swallow in flight".into()),
    width: std::num::NonZeroU32::new(240),
    ..Default::default()
})?;
```

| Argument          | Value                                                                                   |
| ----------------- | --------------------------------------------------------------------------------------- |
| `src`             | Resource path or URL string. A nonempty value enables image rendering.                  |
| `alt`, `title`    | Strings. GPUI labels the node with the first nonempty value, falling back to `"image"`. |
| `width`, `height` | Positive pixel sizes (`Option<NonZeroU32>`).                                            |

GPUI can display app assets and HTTP(S) URLs through the host’s configured HTTP client. Supply your own resize controls for pointer-driven resizing.

## Typing shortcuts

At the start of a paragraph, type `![A swallow](images/swallow.png)`. The closing parenthesis converts the bracketed text to `alt` and the path to `src`.

## Source code

[image/src/lib.rs](/source/plugins/image/src/lib.rs.txt)