# ImageEditor — SvelteKit UI

Package: sveltekit-ui 1.1.94

Page: https://www.sveltekit-ui.com/components/image_editor

Category: Media

provides a comprehensive interface for editing images (crop, 90-degree rotation, resize, filters, AI-based transformations, file export) within a popover or inline panel

Public component import: `ImageEditor`

Public manager import: `create_image_editor_manager`

## Usage

```svelte
<script>
  // Import the shared stylesheet once in your application's root layout.
  import "sveltekit-ui/style.css"
  import { ImageEditor, create_image_editor_manager, Button } from "sveltekit-ui"

  let saved_name = $state("")

  const manager = create_image_editor_manager({
    is_popover: false,
    on_finish: (file) => { saved_name = file?.name ?? "" },
  })
</script>

<p>Choose a local image, adjust its crop or filters, then select Finish.</p>
<ImageEditor {manager} />
<Button manager={manager.finish_button_manager} />
{#if saved_name}<p role="status">Edited file: {saved_name}</p>{/if}
```

## Configuration

### `on_finish`

- type: function
- default: null
- description: invoked when the user taps 'Finish', receives the final edited file

### `ai_image_generator`

- type: function
- default: null
- description: optional async function for generating an image from text, should return { is_success: boolean, data?: base64StringOrURL, error_message?: string }

### `ai_remove_image_background`

- type: function
- default: null
- description: optional async function to remove an image’s background, returns { is_success: boolean, data?: fileUrl, error_message?: string }

### `ai_restore_resolution`

- type: function
- default: null
- description: optional async function to upscale/rest. resolution, returns { is_success: boolean, data?: fileUrl, error_message?: string }

### `ai_restore_face`

- type: function
- default: null
- description: optional async function to restore or enhance a face in an image

## Methods

### `init()`

- description: initializes manager state, sets up popovers, panels, and button managers
- parameters:
  - config: object
- returns: void

### `init_file()`

- description: assigns a new file for editing, sets up hidden image for transformations, updates UI states
- parameters:
  - file_input: object containing { file: File, file_name?: string, mime_type?: string }
- returns: void

### `on_destroy()`

- description: cleanup callback run on svelte onDestroy, removes hidden image from the dom
- parameters:
- returns: void

### `set_screen_tab()`

- description: switches the editor’s main tab: 'filters','crop','resize','ai','file'
- parameters:
  - screen_tab_loc: string
- returns: void

### `rotate()`

- description: rotates the current source in 90-degree steps and adds the result to undo/redo history; positive values rotate clockwise
- parameters:
  - degrees: number, multiple of 90; defaults to 90
- returns: Promise<boolean>, false if disabled, unchanged or unsuccessful

### `undo()`

- description: reverts to a previous image state (if available)
- parameters:
- returns: void

### `redo()`

- description: restores a more recent image state (if undone)
- parameters:
- returns: void

### `clear()`

- description: clears the current image(s), resets to no file
- parameters:
- returns: void

### `finish()`

- description: exports owned canvas/file bytes locally, awaits on_finish, and closes the popover only after success; failures preserve the source and set error_message
- parameters:
- returns: void

### `set_base_image()`

- description: sets the hidden base image src, resets transformations
- parameters:
  - new_src: string (data url or blob url)
- returns: void

### `apply_filters()`

- description: applies current filter settings to the canvas, saves updated data url to the history stack
- parameters:
- returns: void

### `reset_filters()`

- description: resets all filter sliders to their default values
- parameters:
- returns: void

### `main_image_transformer()`

- description: the core function that assembles the image changes (crop, resize, filter) into the final canvas
- parameters:
- returns: void

### `crop_on_up()`

- description: cancels the current pointer-based crop action when pointer is lifted
- parameters:
  - e: event
- returns: void

### `crop_on_move()`

- description: moves or resizes the crop box based on user pointer drag, updating internal state
- parameters:
  - e: event
- returns: void

### `crop_on_down()`

- description: begins the pointer-based crop drag from a handle or the main area, storing initial positions
- parameters:
  - e: event
  - type: string (the drag handle name)
- returns: void

### `set_crop_box()`

- description: sets the crop area to a given aspect ratio or free if w/h are null
- parameters:
  - w: number or null
  - h: number or null
- returns: void

### `crop_image()`

- description: applies the crop changes into a new data url, updates the history stack
- parameters:
  - w: number or null
  - h: number or null
- returns: void

### `toggle_is_lock_resize_aspect_ratio()`

- description: toggles aspect ratio lock for resizing
- parameters:
- returns: void

### `set_resize_dimensions()`

- description: updates the width/height for resizing, honoring aspect ratio lock if enabled
- parameters:
  - w: number or null
  - h: number or null
- returns: void

### `file_input_change()`

- description: handles changes to file name, mime type, or quality; triggers a file size recalculation for the edited image
- parameters:
  - file_name: string
  - mime_type: string
  - quality: number
- returns: void

### `download_image()`

- description: downloads the currently displayed image as a file with the specified mime type and name
- parameters:
- returns: void

### `set_file_from_inputs()`

- description: applies the chosen file name, mime type, quality changes to the final data url, updates state
- parameters:
- returns: void

### `drop_handler()`

- description: handles drag-drop events for files, calls init_file if a valid image file is found
- parameters:
  - e: drag-event
- returns: void

## Returned state

### `id`

- type: string
- description: unique id for this editor instance

### `file_picker_file`

- type: object
- description: the initially picked file object if set

### `image_sources`

- type: array
- description: an array of data urls for undo/redo functionality (history stack)

### `image_source_index`

- type: number
- description: the current index in the history stack

### `selected_image_source`

- type: string
- description: the currently displayed image data url

### `screen_tab`

- type: string
- description: the active tab in the editor: 'filters','crop','resize','ai','file'

### `crop_left`

- type: number
- description: the crop box left position in canvas px

### `crop_top`

- type: number
- description: the crop box top position in canvas px

### `crop_width`

- type: number
- description: the crop box width in canvas px

### `crop_height`

- type: number
- description: the crop box height in canvas px

### `is_disabled`

- type: boolean
- description: disables the editor UI if true

### `current_image_width`

- type: number
- description: the current image’s actual width after base or transformations

### `current_image_height`

- type: number
- description: the current image’s actual height after base or transformations

### `image_pixels_crop_width`

- type: number
- description: the final cropped width in pixels

### `image_pixels_crop_height`

- type: number
- description: the final cropped height in pixels

### `current_image_ar`

- type: number
- description: the current image aspect ratio after transformations (width/height)

### `crop_aspect_ratio`

- type: number
- description: the aspect ratio for the crop box if set, else null for free

### `is_lock_resize_aspect_ratio`

- type: boolean
- description: if true, the width/height maintain the original aspect ratio

### `resize_width`

- type: number
- description: the final width after resizing

### `resize_height`

- type: number
- description: the final height after resizing

### `file_size_data`

- type: object
- description: metadata on file size at various qualities (for certain formats)

### `mime_type_input`

- type: string
- description: the user-selected mime type for the final image

### `file_size_kb`

- type: number
- description: the approximate size in kilobytes of the final image at the chosen settings

### `file_name`

- type: string
- description: the base name for the final file

### `file_name_derived`

- type: string
- description: the sanitized version of the file name (alphanumeric, underscores, etc.)

### `file_name_derived_with_extension`

- type: string
- description: the final file name with an appropriate file extension

### `ai_error_message`

- type: string
- description: error messages generated by AI tasks (removing BG, upscaling, etc.)

### `file_input_manager`

- type: object
- description: manages the underlying file input used to load images

### `screen_tab_filters_button_manager`

- type: object
- description: button manager to open the 'filters' tab

### `screen_tab_crop_button_manager`

- type: object
- description: button manager to open the 'crop' tab

### `screen_tab_resize_button_manager`

- type: object
- description: button manager to open the 'resize' tab

### `screen_tab_ai_button_manager`

- type: object
- description: button manager to open the 'ai' tab

### `screen_tab_file_button_manager`

- type: object
- description: button manager to open the 'file' tab

### `undo_button_manager`

- type: object
- description: button manager for undo

### `redo_button_manager`

- type: object
- description: button manager for redo

### `clear_button_manager`

- type: object
- description: button manager that clears all images from the editor

### `finish_button_manager`

- type: object
- description: button manager that finalizes editing

### `filter_brightness_slider_manager`

- type: object
- description: slider manager controlling brightness

### `filter_contrast_slider_manager`

- type: object
- description: slider manager controlling contrast

### `filter_saturation_slider_manager`

- type: object
- description: slider manager controlling saturation

### `filter_grayscale_slider_manager`

- type: object
- description: slider manager controlling grayscale

### `filter_hue_slider_manager`

- type: object
- description: slider manager controlling hue rotation

### `filter_red_slider_manager`

- type: object
- description: slider manager controlling red channel

### `filter_green_slider_manager`

- type: object
- description: slider manager controlling green channel

### `filter_blue_slider_manager`

- type: object
- description: slider manager controlling blue channel

### `filter_blur_slider_manager`

- type: object
- description: slider manager controlling blur effect

### `filter_opacity_slider_manager`

- type: object
- description: slider manager controlling opacity

### `filters_set_default_button_manager`

- type: object
- description: button manager to reset filter sliders to default

### `filters_apply_filters_button_manager`

- type: object
- description: button manager to apply filters permanently and store in history stack

### `crop_free_button_manager`

- type: object
- description: button manager setting the crop box to free aspect ratio

### `crop_one_to_one_button_manager`

- type: object
- description: button manager for 1:1 aspect ratio

### `crop_four_to_three_button_manager`

- type: object
- description: button manager for 4:3 aspect ratio

### `crop_three_to_two_button_manager`

- type: object
- description: button manager for 3:2 aspect ratio

### `crop_sixteen_to_nine_button_manager`

- type: object
- description: button manager for 16:9 aspect ratio

### `crop_three_to_four_button_manager`

- type: object
- description: button manager for 3:4 aspect ratio

### `crop_two_to_three_button_manager`

- type: object
- description: button manager for 2:3 aspect ratio

### `crop_nine_to_sixteen_button_manager`

- type: object
- description: button manager for 9:16 aspect ratio

### `crop_width_ratio_text_input_manager`

- type: object
- description: text input manager specifying custom crop aspect ratio width

### `crop_height_ratio_text_input_manager`

- type: object
- description: text input manager specifying custom crop aspect ratio height

### `crop_image_button_manager`

- type: object
- description: button manager that applies the current crop to the image

### `crop_aspect_width_loc`

- type: number
- description: the currently set aspect ratio width for custom crop

### `crop_aspect_height_loc`

- type: number
- description: the currently set aspect ratio height for custom crop

### `file_name_text_input_manager`

- type: object
- description: text input manager controlling the user’s chosen file name

### `mime_type_webp_button_manager`

- type: object
- description: button manager to select webp output format

### `mime_type_jpeg_button_manager`

- type: object
- description: button manager to select jpeg output format

### `mime_type_png_button_manager`

- type: object
- description: button manager to select png output format

### `download_image_button_manager`

- type: object
- description: button manager to download the final image

### `set_file_changes_button_manager`

- type: object
- description: button manager to apply the chosen file name, mime type, quality

### `shown_file_size_data`

- type: object
- description: the data for the currently selected mime type’s file size chart, if available

### `file_quality_slider_manager`

- type: object
- description: slider manager for adjusting export compression quality (1–100)

### `ai_tab_dropdown_manager`

- type: object
- description: dropdown manager to switch among AI-related tasks

### `ai_image_generator_is_loading`

- type: boolean
- description: true if the text-to-image AI call is in progress

### `ai_generate_image_from_text_text_input_manager`

- type: object
- description: text input manager containing the user’s text prompt for AI image generation

### `ai_generate_image_images_prompt_file_input_manager`

- type: object
- description: file input manager containing the user’s image prompt for AI image generation

### `ai_generate_image_include_workspace_in_images_prompt_checkbox_manager`

- type: object
- description: checkbox input manager containing if include workspace image in image prompt for AI image generation

### `ai_generate_image_from_text_button_manager`

- type: object
- description: button manager to trigger AI image generation from text

### `ai_remove_bg_button_manager`

- type: object
- description: button manager to trigger AI background removal

### `ai_restore_resolution_button_manager`

- type: object
- description: button manager to trigger AI resolution restoration (upscale)

### `ai_restore_face_button_manager`

- type: object
- description: button manager to restore a face in an image via AI

### `resize_width_dropdown_manager`

- type: object
- description: dropdown manager specifying common width values for resize

### `resize_height_dropdown_manager`

- type: object
- description: dropdown manager specifying common height values for resize

### `resize_width_text_input_manager`

- type: object
- description: text input manager specifying custom resize width

### `resize_height_text_input_manager`

- type: object
- description: text input manager specifying custom resize height

### `resize_aspect_ratio_lock_button_manager`

- type: object
- description: button manager toggling aspect ratio lock for resizing

### `resize_image_button_manager`

- type: object
- description: button manager that applies the resize changes

### `is_popover`

- type: boolean
- description: if true, the entire editor appears in a popover

### `popover_manager`

- type: object
- description: popover manager controlling the editor’s popover if is_popover is true
