> ## Documentation Index
> Fetch the complete documentation index at: https://filekit.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Image and Video utilities

> Utilities for working with image & Video in Kotlin Multiplatform

<Check>Supported on Android, iOS, macOS, JVM targets</Check>

FileKit Core provides powerful utilities for working with images across platforms. These utilities enable common image operations like compression, resizing, and saving to the device gallery, with a consistent API across all supported platforms.

## Compressing Images

You can compress images to reduce their file size while maintaining acceptable quality:

```kotlin theme={null}
suspend fun compressAndSaveImage() {
    // Read an image file
    val originalImageFile = PlatformFile("/path/to/original.jpg")
    val imageBytes = originalImageFile.readBytes()
    
    // Compress the image
    val compressedBytes = FileKit.compressImage(
        bytes = imageBytes,
        quality = 80, // 0-100, where 100 is highest quality
        maxWidth = 1024, // Optional maximum width
        maxHeight = 1024, // Optional maximum height
        imageFormat = ImageFormat.JPEG // JPEG or PNG
    )
    
    // Save the compressed image
    val compressedFile = PlatformFile(FileKit.filesDir, "compressed.jpg")
    compressedFile.write(compressedBytes)
}
```

The `compressImage` function provides several key features:

* **Quality control**: Reduces the image quality based on the `quality` parameter (0-100)
* **Resizing**: Resizes the image if `maxWidth` or `maxHeight` is specified, maintaining the aspect ratio
* **Format conversion**: Converts the image to the specified format (JPEG or PNG)
* **Metadata handling**: On Android, automatically handles EXIF orientation metadata

## Saving Images to Gallery

FileKit provides a way to save images to the device's photo gallery:

```kotlin theme={null}
suspend fun saveImageToDeviceGallery() {
    // Read an image file
    val imageFile = PlatformFile(FileKit.filesDir, "photo.jpg")

    // Save to the device's photo gallery
    val result = FileKit.saveImageToGallery(file = imageFile)
    result.onFailure { error ->
        println("Failed to save image: ${error.message}")
    }
}
```

The behavior of `saveImageToGallery` varies slightly by platform:

| Platform  | Behavior                                                                         |
| --------- | -------------------------------------------------------------------------------- |
| Android   | Saves to the device's Pictures directory and makes it visible in the gallery app |
| iOS       | Saves to the device's Camera Roll                                                |
| macOS/JVM | Saves to the user's Pictures directory                                           |
| JS/WASM   | Not supported (no-op)                                                            |

## Saving Videos to Gallery

FileKit provides a way to save videos to the device's gallery:

```kotlin theme={null}
suspend fun saveVideoToDeviceGallery() {
    // Read a video file
    val videoFile = PlatformFile(FileKit.filesDir, "movie.mp4")

    // Save to the device's video gallery
    val result = FileKit.saveVideoToGallery(file = videoFile)
    result.onFailure { error ->
        println("Failed to save video: ${error.message}")
    }
}
```

The behavior of `saveVideoToGallery` varies slightly by platform:

| Platform  | Behavior                                                                       |
| --------- | ------------------------------------------------------------------------------ |
| Android   | Saves to the device's Videos directory and makes it visible in the gallery app |
| iOS       | Saves to the device's Camera Roll                                              |
| macOS/JVM | Saves to the user's videos directory                                           |
| JS/WASM   | Not supported (no-op)                                                          |

## Encoding and Converting Images

FileKit provides utilities for encoding `ImageBitmap` objects into byte arrays and converting `PlatformFile` objects into `ImageBitmap` instances. These methods are useful for image manipulation and storage across platforms.

### Encoding an ImageBitmap to ByteArray

The `ImageBitmap.encodeToByteArray` method allows you to encode an `ImageBitmap` into a `ByteArray` using a specified format and quality.

```kotlin theme={null}
suspend fun encodeImageToByteArray(imageBitmap: ImageBitmap): ByteArray {
    return imageBitmap.encodeToByteArray(
        format = ImageFormat.JPEG, // JPEG or PNG
        quality = 90 // Compression quality (0-100)
    )
}
```

### Converting a PlatformFile to ImageBitmap

The `PlatformFile.toImageBitmap` method converts a file into an `ImageBitmap` for use in Compose UI or other image operations.

```kotlin theme={null}
suspend fun convertFileToImageBitmap(file: PlatformFile): ImageBitmap {
    return file.toImageBitmap()
}
```

## Example: Creating a Thumbnail

This function creates a thumbnail version of an image by resizing it to a maximum dimension while maintaining the aspect ratio.

```kotlin theme={null}
suspend fun createThumbnail(originalImageFile: PlatformFile, maxSize: Int = 200): PlatformFile {
    // Read the original image
    val imageBytes = originalImageFile.readBytes()
    
    // Create a thumbnail by compressing and resizing
    val thumbnailBytes = FileKit.compressImage(
        bytes = imageBytes,
        quality = 85,
        maxWidth = maxSize,
        maxHeight = maxSize,
        imageFormat = ImageFormat.JPEG
    )
    
    // Save the thumbnail
    val thumbnailFile = PlatformFile(
        FileKit.cacheDir,
        "thumb_${originalImageFile.nameWithoutExtension}.jpg"
    )
    thumbnailFile write thumbnailBytes
    
    return thumbnailFile
}
```
