Skip to content

NbImageCropper allows users to select and crop an image with precision. It supports circular and rectangular crops, aspect ratio locking, and a real-time preview of the cropped output.

TIP

The component automatically handles image scaling and positioning to fit within the display area while maintaining the original aspect ratio.

vue
<template>
  <NbImageCropper :image="selectedImage" @crop="handleCrop" />
  <input
    type="file"
    ref="fileInput"
    accept="image/*"
    style="display: none"
    @change="onFileChange"
  />
  <NbButton @click="() => fileInput.click()">Open File</NbButton>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const fileInput = ref<HTMLInputElement | null>(null)
const selectedImage = ref<File | null>(null)

const onFileChange = (event: Event) => {
  const input = event.target as HTMLInputElement
  if (input.files?.[0]) selectedImage.value = input.files[0]
}

const handleCrop = (data: {
  blob: Blob
  geometry: { x: number; y: number; width: number; height: number }
}) => {
  const formData = new FormData()
  formData.append('file', data.blob, 'cropped.png')
  formData.append('geometry', JSON.stringify(data.geometry))
}
</script>

Circle crop ​

When cropAsCircle is enabled, the crop area is displayed as a circle overlay.

Locked aspect ratio ​

When lockAspectRatio is enabled, the crop area maintains its proportions during resizing. Combined with cropAsCircle, this creates a perfect circle.

With live preview ​

Enable showPreview to display a real-time preview of the cropped area and its geometry data.

Circular output ​

When outputAsCircle is enabled, the final cropped image has transparent corners, producing a circular PNG.

Props ​

PropTypeDefaultDescription
imageFile | nullnullThe image file to crop
cropAsCirclebooleanfalseDisplay the crop area as a circle overlay
outputAsCirclebooleanfalseOutput the cropped image with transparent corners (circle PNG)
lockAspectRatiobooleanfalseMaintain the crop area aspect ratio during resizing
showPreviewbooleanfalseDisplay a live preview of the cropped image and geometry

Events ​

EventPayloadDescription
crop{ blob: Blob; geometry: { x, y, width, height } }Emitted when the user crops the image

The blob is ready to upload via FormData. The geometry object contains the crop rectangle in the original image's coordinate space.