image cropper for javascript
npm install --save imagecropper
Include files:
<link rel="stylesheet" type="text/css" href="/path/to/cropper.css"/>
<script src="/path/to/cropper.js"></script>
Initialize with new Cropper
method.
<!-- Wrap the image or canvas element with a block element (container) -->
<div>
<img id="image" src="picture.jpg" />
</div>
<div id="preview"></div>
var cropper = new Cropper({
element: document.getElementById("image"),
toolbar: true,
aspectRatio: 1,
preview: document.getElementById("preview")
})
- Type: Number
- Default: 0
Set the aspect ratio of the crop box. By default, the crop box is free ratio.
- Type: String
- Default: 'none'
- Options:
- border
- image
- none
Restrict the cropper to the border, the image, or with no restrictions.
- Type: DOMElement(img)
- Needed: Needed
Image element to crop
- Type: String
- Default: 'crop'
- Options:
- crop
- move
- none
Dragging move of the cropper.
- Type: Boolean
- Default: true
Enable to move the image.
- Type: DOMElement
- Needed: Optional
Add extra elements (containers) for previewing.
- Type: Boolean
- Default: true
Rerender the cropper when resizing the window.
- Type: Boolean
- Default: true
Whether to display toolbar.
- Type: Boolean
- Default: true
Enable to zoom the image.
- type: A String indicating the image format. The default format type is image/png
return cropped-image data encoded into base64
var image = cropper.getImage()