Image Cropper

Introduction

A customizable Image Cropper component. It expects a base64 string as v-model.

  • Class props for individual styling
  • Props

Props

PropTypeDefaultDescription
v-modelstringThe base64 image
aspect-rationumber1 / 1The aspect ratio of the stencil
rotation-anglenumber90The angle, by which the image will be rotated, when one of the rotation buttons is pressed.
zoombooleantrueIf set false, the image won't be automatically zoomed, as soon as you change the stencil size.
stencil-widthnumber500The width of the stencil
stencil-heightnumber500The height of the stencil

Slots

NameDescription
buttonsBarThe buttons, under the image.

v-bind values

NameDescription
cropperRefThe reference of the actual cropper
emitThe emits

Slot-button functions

FunctionDescription
cropperRef.rotate()Function to rotate the base64 image. Negative values to rotate counter-clockwise. Positive values to rotate clockwise.
cropperRef.getResult().canvas.toDataURL()To get the base64 value, of the stencil-content.

Examples

Minimal working example

parent.vue

<template>
  <div class="wrapper">
    <ImageCropper v-model="***LINKTOBASE64IMAGE***" />
  </div>
</template>

Output:

Example with aspect-ratio, rotation-angle and zoom set

parent.vue

<template>
  <div class="wrapper">
    <ImageCropper
      v-model="***LINKTOBASE64IMAGE***"
      :aspect-ratio="16 / 9"
      :rotation-angle="10"
      :zoom="false"
    />
  </div>
</template>

Output:

Example with all props

parent.vue

<template>
  <div class="wrapper">
    <ImageCropper
      v-model="***LINKTOBASE64IMAGE***"
      :aspect-ratio="16 / 9"
      :rotation-angle="10"
      :zoom="false"
      :stencil-width="300"
      :stencil-height="300"
    />
  </div>
</template>

Output:

In this case, because of the aspect-ratio, the height won't be set to 300, but the width will, to guarantee the aspect-ratio.

Example with own buttons, passed through the slot.

<template>
  <div class="wrapper">
    <ImageCropper v-model="***LINKTOBASE64IMAGE***">
      <template #buttonsBar="{ cropperRef, emit }">
        <button @click="cropperRef?.rotate(-***ROTATIONANGLE***)">
          Rotate Left
        </button>

        <button @click="cropperRef?.rotate(***ROTATIONANGLE***)">
          Rotate Right
        </button>

        <button
          @click="
            emit(
              'update:modelValue',
              cropperRef?.getResult().canvas.toDataURL()
            )
          "
        >
          SAVE
        </button>
      </template>
    </ImageCropper>
  </div>
</template>

Output: