logo dile-components dile-components

dile-image-uploader

The dile-image-uploader component is a tool to upload images vía Ajax.

Installation

npm i @dile/crud

Usage

Import the dile-image-uploader component.

import '@dile/crud/components/image-uploader/image-uploader.js';

Use the component.

<dile-image-uploader 
  endpoint="/api/board-games/1/upload-card"
  language="es"
></dile-image-uploader>

This component is implemented with the dile-ajax component, so refer to the dile-ajax documentation for more information on configuration requirements.

Properties

Image validation and preview

After selecting a file, the component decodes it in the browser to check that it's a real, non-empty, non-corrupted image before accepting it. If maxFileSize, minWidth, maxWidth, minHeight or maxHeight are set and the image doesn't comply, the selection is rejected and a translated error message is shown, the same way an invalid extension is reported.

Once a valid image is accepted, a thumbnail and a summary (width × height px · file size) are shown below the drop zone, so the user can confirm the file is the expected one before submitting.

Image cropping

When crop is set, after a valid image is selected the thumbnail is replaced by a dile-image-cropper component. The user selects the area to keep and, when the form is submitted, the cropped image is sent in the image field instead of the original file. The validation rules (maxFileSize, minWidth...) are checked against the original image.

<dile-image-uploader
  endpoint="/api/users/1/avatar"
  crop
  cropAspectRatio="1"
  cropOutputWidth="400"
  cropOutputHeight="400"
  cropRound
></dile-image-uploader>

The cropper is based on Cropper.js v2, which is only loaded when crop is enabled and an image is selected, through a dynamic import. If you don't use crop, Cropper.js is never downloaded.

Custom Events

The dile-image-uploader component is based on dile-ajax-form, so you can listen to the same custom events documented for that component.