> ## 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

> Compress and resize images, save images and videos to the gallery, and convert files to ImageBitmap

[See the platform support matrix](https://filekit.mintlify.site/platform-support.md#filekit-core)

`FileKit.compressImage` re-encodes an image as JPEG or PNG, optionally downscaled, and returns the new bytes. It is declared in `nonWebMain` and throws a `FileKitException` on Linux native, Windows native, and watchOS.

## Usage

```kotlin nonWebMain theme={null}
val compressed: ByteArray = FileKit.compressImage(
    file = photo,
    quality = 80,
    maxWidth = 1024,
    maxHeight = 1024,
)
(FileKit.cacheDir / "photo-small.jpg").write(compressed)
```

Pass `bytes = imageBytes` instead of `file` when the image is already in memory.

## Options

**file / bytes** `PlatformFile | ByteArray`

The source image.

**imageFormat** `ImageFormat` (default: JPEG)

Output format: `ImageFormat.JPEG` or `ImageFormat.PNG`.

**quality** `Int` (default: 80)

`0` to `100`. Applies to JPEG only.

**maxWidth / maxHeight** `Int?` (default: null)

Downscales to fit, keeping the aspect ratio. Smaller images are never upscaled.

On Android, the EXIF orientation is applied before resizing, so the output is upright.

## Save to the gallery

[See the platform support matrix](https://filekit.mintlify.site/platform-support.md#filekit-core)

`saveImageToGallery` and `saveVideoToGallery` return a `Result<Unit>` instead of throwing:

```kotlin nonWebMain theme={null}
FileKit.saveImageToGallery(file = photo)
    .onFailure { error -> showError(error.message) }

FileKit.saveVideoToGallery(file = video, filename = "holiday.mp4")
```

`filename` defaults to the file's name. `saveImageToGallery(bytes, filename)` saves an image from memory.

| Platform | Destination |
| - | - |
| Android | MediaStore, in `DCIM/<app name>` on Android 10 and later. |
| iOS | The Photos library. `filename` is ignored. |
| macOS, JVM, Linux native, Windows native | The user's Pictures folder for images and Videos (Movies on macOS) folder for videos. |
| watchOS | Returns a failed `Result`. |
| Web | Not declared. |

> [!WARNING]
>
> On iOS, add `NSPhotoLibraryAddUsageDescription` to your `Info.plist` before saving to the gallery. Without it, the system terminates your app on the first save.

> [!NOTE]
>
> On Android 9 and lower, writing to MediaStore requires the `WRITE_EXTERNAL_STORAGE` permission. FileKit doesn't declare it, so add it to your manifest with `android:maxSdkVersion="28"` if you support those versions.

## Convert to and from ImageBitmap in Compose

[See the platform support matrix](https://filekit.mintlify.site/platform-support.md#compose-and-coil)

`PlatformFile.toImageBitmap()` and `ImageBitmap.encodeToByteArray()` come from `filekit-dialogs-compose`, not `filekit-core`. Add it to `commonMain` (see [Installation](https://filekit.mintlify.site/installation.md) for the current version):

```kotlin build.gradle.kts theme={null}
implementation("io.github.vinceglb:filekit-dialogs-compose:<version>")
```

```kotlin theme={null}
import io.github.vinceglb.filekit.dialogs.compose.util.encodeToByteArray
import io.github.vinceglb.filekit.dialogs.compose.util.toImageBitmap

val bitmap: ImageBitmap = file.toImageBitmap()
val png: ByteArray = bitmap.encodeToByteArray(format = ImageFormat.PNG)
```

Both are suspend functions. `encodeToByteArray` defaults to `ImageFormat.JPEG` with `quality = 100`. To display a file in an `Image`, the [Coil](https://filekit.mintlify.site/integrations/coil.md) loads it for you, with caching.

## Related

* [Gallery picker](https://filekit.mintlify.site/dialogs/gallery-picker.md) to let users pick photos and videos.
* [Camera picker](https://filekit.mintlify.site/dialogs/camera-picker.md) to take a photo.
* [Coil](https://filekit.mintlify.site/integrations/coil.md) to display a `PlatformFile` image.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.