HTML:
... <div class="img-container"> <img src="img/picture-1.jpg"> </div> ... <div class="img-preview"></div> ...
Javascript:
var $image = $(".img-container img"), $dataX = $("#dataX"), $dataY = $("#dataY"), $dataHeight = $("#dataHeight"), $dataWidth = $("#dataWidth"); $image.cropper({ aspectRatio: 16 / 9, data: { x: 480, y: 60, width: 640, height: 360 }, preview: ".img-preview", done: function(data) { $dataX.val(Math.round(data.x)); $dataY.val(Math.round(data.y)); $dataHeight.val(Math.round(data.height)); $dataWidth.val(Math.round(data.width)); } }); ... $("#reset").click(function() { $image.cropper("reset"); }); ...
Include files:
<script src="/path/to/jquery.js"></script><!-- jQuery is required --> <link href="/path/to/cropper.css" rel="stylesheet"> <script src="/path/to/cropper.js"></script>
Initialize with $.fn.cropper
method.
HTML:
<!-- Wrap the image with a block element --> <div class="container"> <img src="picture.jpg"> </div>
Javascript:
$(".container > img").cropper({ aspectRatio: 1.618, done: function(data) { // Output the result data for cropping image. } });
Notes:
- The size of the cropper inherits from the size of the image's parent element, so be sure to wrap the image with a visible block element.
- The values of the result data was computed with the original size of the image, so you can use them to crop the image directly.
You may set cropper options with $().cropper(options)
.
If you want to change the global default options, You may use $.fn.cropper.setDefaults(options)
.
Name | Type | Default | Description |
---|---|---|---|
aspectRatio | string / number | "auto" |
The aspect ratio of the cropping zone. By default, the cropping zone is free ratio. |
data | object | {} |
Only supports four properties: "x", "y", "width" and "height". By default, the cropping zone will appear in the center of the image. |
done | function | function (data) {} |
This function will be executed when the cropping zone changes by a move, resize or crop. |
preview | selector | "" |
A jquery selector, add extra elements for preview. |
multiple | boolean | false |
By default, the plugin only supports one cropper per page. If you intend to use more than one, just initialize them with this option set to true . |
modal | boolean | true |
Show (true) or hide (false) the black modal layer above the cropper. |
dashed | boolean | true |
Show (true) or hide (false) the dashed lines above the cropping zone. |
autoCrop | boolean | true |
Render the cropping zone automatically when initialize. |
autoCropArea | number | 0.8 |
A number between 0 and 1. Define the automatic cropping area size (percentage). |
dragCrop | boolean | true |
Enable to remove the current cropping zone and create a new one by dragging over the image. |
movable | boolean | true |
Enable to move the cropping zone. |
resizable | boolean | true |
Enable to resize the cropping zone. |
zoomable | boolean | true |
Enable to zoom the image. |
rotatable | boolean | true |
Enable to rotate the image. |
checkImageOrigin | boolean | true |
By default, the plugin will check the image origin, and if it is a cross-origin image, a "crossOrigin" attribute will be added to the image element to enable "rotate" and "getDataURL". |
minWidth | number | 0 |
The minimum width (px of original image) of the cropping zone. Use this option only when you are sure that the image has this minimum width. |
minHeight | number | 0 |
The minimum height (px of original image) of the cropping zone. Use this option only when you are sure that the image has this minimum height. |
maxWidth | number | Infinity |
The maximum width (px of original image) of the cropping zone. Use this option only when you are sure that the image has this maximum width. |
maxHeight | number | Infinity |
The maximum height (px of original image) of the cropping zone. Use this option only when you are sure that the image has this maximum height. |
minContainerWidth | number | 300 |
The minimum width of the cropper container. |
minContainerHeight | number | 150 |
The minimum height of the cropper container. |
build | function | null |
An event handler of the "build.cropper" event. |
built | function | null |
An event handler of the "built.cropper" event. |
dragstart | function | null |
An event handler of the "dragstart.cropper" event. |
dragmove | function | null |
An event handler of the "dragmove.cropper" event. |
dragend | function | null |
An event handler of the "dragend.cropper" event. |
$().cropper("zoom", 0.1)
or $().cropper("zoom", -0.1)
$().cropper("rotate", 180)
or $().cropper("rotate", -180)
$().cropper("enable")
$().cropper("disable")
true
param to reset the cropping zone to the default state.$().cropper("reset")
or $().cropper("reset", true)
$().cropper("clear")
.$().cropper("replace", "example.jpg")
.$().cropper("destroy")
.true
parameter to get rounded data.$().cropper("getData")
or $().cropper("getData", true)
.$().cropper("setData", {width: 480, height: 270})
.null
. For example: $().cropper("setData", {})
or $().cropper("setData", null)
.$().cropper("setAspectRatio", 1.618)
.$().cropper("getImageData")
.$().cropper("setDragMode", "crop")
.Object
contains: "width", "height". Define the sizes of the result image.String
indicating the image format. The default type is image/png. Other types: "image/jpeg", "image/webp".Number
between 0 and 1 indicating image quality if the requested type is image/jpeg or image/webp.$().cropper("getDataURL")
or $().cropper("getDataURL", {width:100, height: 100}, "image/jpeg", 0.6)
or $().cropper("getDataURL", "image/jpeg")
or $().cropper("getDataURL", "image/jpeg", 0.6)
.This event will be fired when the Cropper starts to build.
This event will be fired when the Cropper has been built.
This event will be fired before the cropping zone start to move.
This event will be fired when the cropping zone was moving.
This event will be fired after the cropping zone stop to move.
Demo:
HTML:
<div class="fixed-dragger-cropper"> <img src="img/picture-1.jpg"> </div>
JavaScript:
$(".fixed-dragger-cropper > img").cropper({ aspectRatio: 640 / 320, autoCropArea: 0.6, // Center 60% multiple: false, dragCrop: false, dashed: false, movable: false, resizable: false, built: function () { $(this).cropper("zoom", 0.5); } });
HTML:
<div class="modal" id="bootstrap-modal"> <div class="modal-dialog"> ... <div class="modal-body"> <div class="bootstrap-modal-cropper"> <img src="img/picture-1.jpg"> </div> </div> ... </div> </div>
Demo:
JavaScript:
var $image = $(".bootstrap-modal-cropper > img"), originalData = {}; $("#bootstrap-modal").on("shown.bs.modal", function() { $image.cropper({ multiple: true, data: originalData, done: function(data) { console.log(data); } }); }).on("hidden.bs.modal", function() { originalData = $image.cropper("getData"); $image.cropper("destroy"); });
This example require a PHP server, please download and test it.