christianesperar 89d7f6df20 Update libraries | 8 лет назад | |
---|---|---|
.. | ||
dist | 8 лет назад | |
src | 8 лет назад | |
.bower.json | 8 лет назад | |
CHANGELOG.md | 8 лет назад | |
LICENSE | 8 лет назад | |
README.md | 8 лет назад | |
bower.json | 8 лет назад | |
package.json | 8 лет назад |
A simple jQuery image cropping plugin.
dist/
├── cropper.css ( 5 KB)
├── cropper.min.css ( 4 KB)
├── cropper.js (79 KB)
└── cropper.min.js (28 KB)
Four quick start options are available:
git clone https://github.com/fengyuanchen/cropper.git
.npm install cropper
.bower install cropper
.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>
The cdnjs provides CDN support for Cropper's CSS and JavaScript. You can find the links here.
Initialize with $.fn.cropper
method.
<!-- Wrap the image or canvas element with a block element (container) -->
<div>
<img id="image" src="picture.jpg">
</div>
/* Limit image width to avoid overflow the container */
img {
max-width: 100%; /* This rule is very important, please do not ignore this! */
}
$('#image').cropper({
aspectRatio: 16 / 9,
crop: function(e) {
// Output the result data for cropping image.
console.log(e.x);
console.log(e.y);
console.log(e.width);
console.log(e.height);
console.log(e.rotate);
console.log(e.scaleX);
console.log(e.scaleY);
}
});
See the FAQ documentation.
The size of the cropper inherits from the size of the image's parent element (wrapper), so be sure to wrap the image with a visible block element.
If you are using cropper in a modal, you should initialize the cropper after the modal shown completely. Otherwise, you will not get a correct cropper.
The outputted cropped data bases on the original image size, so you can use them to crop the image directly.
If you try to start cropper on a cross-origin image, please make sure that your browser supports HTML5 CORS settings attributes, and your image server supports the Access-Control-Allow-Origin
option (see the HTTP access control (CORS)).
Known iOS resource limits: As iOS devices limit memory, the browser may crash when you are cropping a large image (iPhone camera resolution). To avoid this, you may resize the image first (below 1024px) before start a cropper.
Known image size increase: When export the cropped image on browser-side with the HTMLCanvasElement.toDataURL
method, the the exported image'size may be greater than the original image's. This is because the exported image'type is not the same as the original image's. So just pass the original image's type as the first parameter to toDataURL
to fix this. For example, if the original type is JPEG, then use $().cropper('getCroppedCanvas').toDataURL('image/jpeg')
to export image.
You may set cropper options with $().cropper(options)
.
If you want to change the global default options, You may use $.fn.cropper.setDefaults(options)
.
Number
0
0
: the crop box is just within the container1
: the crop box should be within the canvas2
: the canvas should not be within the container3
: the container should be within the canvasDefine the view mode of the cropper.
String
'crop'
'crop'
: create a new crop box'move'
: move the canvas'none'
: do nothingDefine the dragging mode of the cropper.
Number
NaN
Set the aspect ratio of the crop box. By default, the crop box is free ratio.
Object
null
The previous cropped data if you had stored, will be passed to setData
method automatically.
String
(jQuery selector)''
Add extra elements (containers) for previewing.
Notes:
aspectRatio
option, be sure to set the preview container with the same aspect ratio.overflow:hidden
to the preview container.Boolean
true
Re-render the cropper when resize the window.
Boolean
true
Restore the cropped area after resize the window.
Boolean
true
Check if the current image is a cross-origin image.
If it is, when clone the image, a crossOrigin
attribute will be added to the cloned image element and a timestamp will be added to the src
attribute to reload the source image to avoid browser cache error.
By adding crossOrigin
attribute to image will stop adding timestamp to image url, and stop reload of image.
Boolean
true
Check the current image's Exif Orientation information.
More exactly, read the Orientation value for rotating or flipping the image, and then override the Orientation value with 1
(the default value) to avoid some issues (#120, #509) on iOS devices.
Note: Don't trust this all the time as some JPG images have incorrect (not standard) Orientation values.
Requires Typed Arrays support (IE 10+).
Boolean
true
Show the black modal above the image and under the crop box.
Boolean
true
Show the dashed lines above the crop box.
Boolean
true
Show the center indicator above the crop box.
Boolean
true
Show the white modal above the crop box (highlight the crop box).
Boolean
true
Show the grid background of the container.
Boolean
true
Enable to crop the image automatically when initialize.
Number
0.8
(80% of the image)A number between 0 and 1. Define the automatic cropping area size (percentage).
Boolean
true
Enable to move the image.
Boolean
true
Enable to rotate the image.
Boolean
true
Enable to scale the image.
Boolean
true
Enable to zoom the image.
Boolean
true
Enable to zoom the image by dragging touch.
Boolean
true
Enable to zoom the image by wheeling mouse.
Number
0.1
Define zoom ratio when zoom the image by wheeling mouse.
Boolean
true
Enable to move the crop box by dragging.
Boolean
true
Enable to resize the crop box by dragging.
Boolean
true
Enable to toggle drag mode between "crop" and "move" when click twice on the cropper.
Number
200
The minimum width of the container.
Number
100
The minimum height of the container.
Number
0
The minimum width of the canvas (image wrapper).
Number
0
The minimum height of the canvas (image wrapper).
Number
0
The minimum width of the crop box.
Note: This size is relative to the page, not the image.
Number
0
The minimum height of the crop box.
Note: This size is relative to the page, not the image.
Function
null
A shortcut of the "build.cropper" event.
Function
null
A shortcut of the "built.cropper" event.
Function
null
A shortcut of the "cropstart.cropper" event.
Function
null
A shortcut of the "cropmove.cropper" event.
Function
null
A shortcut of the "cropend.cropper" event.
Function
null
A shortcut of the "crop.cropper" event.
Function
null
A shortcut of the "zoom.cropper" event.
As there is an asynchronous process when load the image, you should call most of the methods after built, except "setAspectRatio", "replace" and "destroy".
$().cropper({
built: function () {
$().cropper('method', argument1, , argument2, ..., argumentN);
}
});
Show the crop box manually.
$().cropper({
autoCrop: false,
built: function () {
// Do something here
// ...
// And then
$(this).cropper('crop');
}
});
Reset the image and crop box to their initial states.
Clear the crop box.
url:
String
onlyColorChanged (optional):
Boolean
false
.Replace the image's src and rebuild the cropper.
Enable (unfreeze) the cropper.
Disable (freeze) the cropper.
Destroy the cropper and remove the instance from the image.
offsetX:
Number
offsetY (optional):
Number
offsetX
.Move the canvas (image wrapper) with relative offsets.
$().cropper('move', 1);
$().cropper('move', 1, 0);
$().cropper('move', 0, -1);
x:
Number
left
value of the canvasy (optional):
Number
top
value of the canvasx
.Move the canvas (image wrapper) to an absolute point.
Number
Zoom the canvas (image wrapper) with a relative ratio.
$().cropper('zoom', 0.1);
$().cropper('zoom', -0.1);
Number
Zoom the canvas (image wrapper) to an absolute ratio.
$().cropper('zoomTo', 1); // 1:1 (canvasData.width === canvasData.naturalWidth)
Number
Rotate the image with a relative degree.
Requires CSS3 2D Transforms support (IE 9+).
$().cropper('rotate', 90);
$().cropper('rotate', -90);
Number
Rotate the image to an absolute degree.
scaleX:
Number
1
1
it does nothing.scaleY (optional):
Number
scaleX
.Scale the image.
Requires CSS3 2D Transforms support (IE 9+).
$().cropper('scale', -1); // Flip both horizontal and vertical
$().cropper('scale', -1, 1); // Flip horizontal
$().cropper('scale', 1, -1); // Flip vertical
Number
1
1
it does nothing.Scale the abscissa of the image.
Number
1
1
it does nothing.Scale the ordinate of the image.
rounded (optional):
Boolean
false
true
to get rounded values.(return value):
Object
x
: the offset left of the cropped areay
: the offset top of the cropped areawidth
: the width of the cropped areaheight
: the height of the cropped arearotate
: the rotated degrees of the imagescaleX
: the scaling factor to apply on the abscissa of the imagescaleY
: the scaling factor to apply on the ordinate of the imageOutput the final cropped area position and size data (base on the natural size of the original image).
You can send the data to server-side to crop the image directly.
Object
getData
method.Change the cropped area position and size with new data (base on the original image).
Note: This method only available when the
viewMode
option great than or equal to1
.
Object
width
: the current width of the containerheight
: the current height of the containerOutput the container size data.
Object
left
: the offset left of the imagetop
: the offset top of the imagewidth
: the width of the imageheight
: the height of the imagenaturalWidth
: the natural width of the imagenaturalHeight
: the natural height of the imageaspectRatio
: the aspect ratio of the imagerotate
: the rotated degrees of the image if rotatedscaleX
: the scaling factor to apply on the abscissa of the image if scaledscaleY
: the scaling factor to apply on the ordinate of the image if scaledOutput the image position, size and other related data.
Object
left
: the offset left of the canvastop
: the offset top of the canvaswidth
: the width of the canvasheight
: the height of the canvasnaturalWidth
: the natural width of the canvas (read only)naturalHeight
: the natural height of the canvas (read only)Output the canvas (image wrapper) position and size data.
var imageData = $().cropper('getImageData');
var canvasData = $().cropper('getCanvasData');
if (imageData.rotate % 180 === 0) {
console.log(canvasData.naturalWidth === imageData.naturalWidth); // true
}
Object
left
: the new offset left of the canvastop
: the new offset top of the canvaswidth
: the new width of the canvasheight
: the new height of the canvasChange the canvas (image wrapper) position and size with new data.
Object
left
: the offset left of the crop boxtop
: the offset top of the crop boxwidth
: the width of the crop boxheight
: the height of the crop boxOutput the crop box position and size data.
Object
left
: the new offset left of the crop boxtop
: the new offset top of the crop boxwidth
: the new width of the crop boxheight
: the new height of the crop boxChange the crop box position and size with new data.
options (optional):
Object
width
: the destination width of the output canvasheight
: the destination height of the output canvasfillColor
: a color to fill any alpha values in the output canvas(return value):
HTMLCanvasElement
Browser support:
Get a canvas drawn the cropped image. If it is not cropped, then returns the whole canvas.
After then, you can display the canvas as an image directly, or use HTMLCanvasElement.toDataURL to get a Data URL, or use HTMLCanvasElement.toBlob to get a blob and upload it to server with FormData if the browser supports these APIs.
$().cropper('getCroppedCanvas');
$().cropper('getCroppedCanvas', {
width: 160,
height: 90
});
// Upload cropped image to server if the browser supports `HTMLCanvasElement.toBlob`
$().cropper('getCroppedCanvas').toBlob(function (blob) {
var formData = new FormData();
formData.append('croppedImage', blob);
$.ajax('/path/to/upload', {
method: "POST",
data: formData,
processData: false,
contentType: false,
success: function () {
console.log('Upload success');
},
error: function () {
console.log('Upload error');
}
});
});
Number
Change the aspect ratio of the crop box.
String
'none'
'none'
, 'crop'
, 'move'
Change the drag mode.
Tips: You can toggle the "crop" and "move" mode by double click on the cropper.
This event fires when a cropper instance starts to load an image.
This event fires when a cropper instance has built completely.
event.originalEvent:
Event
mousedown
, touchstart
and pointerdown
event.action:
String
'crop'
: create a new crop box'move'
: move the canvas (image wrapper)'zoom'
: zoom in / out the canvas (image wrapper) by touch.'e'
: resize the east side of the crop box'w'
: resize the west side of the crop box's'
: resize the south side of the crop box'n'
: resize the north side of the crop box'se'
: resize the southeast side of the crop box'sw'
: resize the southwest side of the crop box'ne'
: resize the northeast side of the crop box'nw'
: resize the northwest side of the crop box'all'
: move the crop box (all directions)This event fires when the canvas (image wrapper) or the crop box starts to change.
$().on('cropstart.cropper', function (e) {
console.log(e.type); // cropstart
console.log(e.namespace); // cropper
console.log(e.action); // ...
console.log(e.originalEvent.pageX);
// Prevent to start cropping, moving, etc if necessary
if (e.action === 'crop') {
e.preventDefault();
}
});
event.originalEvent:
Event
mousemove
, touchmove
and pointermove
.event.action: the same as "cropstart.cropper".
This event fires when the canvas (image wrapper) or the crop box is changing.
event.originalEvent:
Event
mouseup
, touchend
, touchcancel
, pointerup
and pointercancel
.event.action: the same as "cropstart.cropper".
This event fires when the canvas (image wrapper) or the crop box stops to change.
About these properties, see the
getData
method.
This event fires when the canvas (image wrapper) or the crop box changed.
event.originalEvent:
Event
wheel
, touchmove
.event.oldRatio:
Number
event.ratio:
Number
canvasData.width / canvasData.naturalWidth
)This event fires when a cropper instance starts to zoom in or zoom out its canvas (image wrapper).
$().on('zoom.cropper', function (e) {
// Zoom in
if (e.ratio > e.oldRatio) {
// Prevent zoom in
e.preventDefault();
}
// Zoom out
// ...
});
If you have to use other plugin with the same namespace, just call the $.fn.cropper.noConflict
method to revert to it.
<script src="other-plugin.js"></script>
<script src="cropper.js"></script>
<script>
$.fn.cropper.noConflict();
// Code that uses other plugin's "$().cropper" can follow here.
</script>
As a jQuery plugin, you also need to see the jQuery Browser Support.
Please read through our contributing guidelines.
Maintained under the Semantic Versioning guidelines.