| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
A simple jQuery image cropping plugin.
dist/ ├── cropper.css ( 5 KB) ├── cropper.min.css ( 4 KB) ├── cropper.js (80 KB, UMD) ├── cropper.min.js (30 KB, UMD, compressed) ├── cropper.common.js (80 KB, CommonJS) └── cropper.esm.js (80 KB, ES Module)
Four quick start options are available:
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);
}
});Just double click your mouse to enter crop mode.
Just double click your mouse to enter move mode.
Just hold the shift key when you resize the crop box.
Just hold the shift key when you crop on the image.
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 size of the exported image may be greater than the original image's. This is because the type of the exported image is not the same as the original image's. So just pass the type the original image's 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).
Define the view mode of the cropper. If you set viewMode to 0, the crop box can extend outside the canvas, while a value of 1, 2 or 3 will restrict the crop box to the size of the canvas. A viewMode of 2 or 3 will additionally restrict the canvas to the container. Note that if the proportions of the canvas and the container are the same, there is no difference between 2 and 3.
Define the dragging mode of the cropper.
Set the aspect ratio of the crop box. By default, the crop box is free ratio.
The previous cropped data if you had stored, will be passed to setData method automatically.
Add extra elements (containers) for previewing.
Notes:
Re-render the cropper when resize the window.
Restore the cropped area after resize the window.
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.
If the value of the image's crossOrigin attribute is "use-credentials", then the withCredentials attribute will set to true when read the image data by XMLHttpRequest.
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.
Requires to set both the rotatable and scalable options to true at the same time.
Note: Don't trust this all the time as some JPG images have incorrect (not standard) Orientation values.
Requires Typed Arrays support (IE 10+).
Show the black modal above the image and under the crop box.
Show the dashed lines above the crop box.
Show the center indicator above the crop box.
Show the white modal above the crop box (highlight the crop box).
Show the grid background of the container.
Enable to crop the image automatically when initialize.
A number between 0 and 1. Define the automatic cropping area size (percentage).
Enable to move the image.
Enable to rotate the image.
Enable to scale the image.
Enable to zoom the image.
Enable to zoom the image by dragging touch.
Enable to zoom the image by wheeling mouse.
Define zoom ratio when zoom the image by wheeling mouse.
Enable to move the crop box by dragging.
Enable to resize the crop box by dragging.
Enable to toggle drag mode between "crop" and "move" when click twice on the cropper.
The minimum width of the container.
The minimum height of the container.
The minimum width of the canvas (image wrapper).
The minimum height of the canvas (image wrapper).
The minimum width of the crop box.
Note: This size is relative to the page, not the image.
The minimum height of the crop box.
Note: This size is relative to the page, not the image.
A shortcut of the "ready" event.
A shortcut of the "cropstart" event.
A shortcut of the "cropmove" event.
A shortcut of the "cropend" event.
A shortcut of the "crop" event.
A shortcut of the "zoom" event.
As there is an asynchronous process when load the image, you should call most of the methods after ready, except "setAspectRatio", "replace" and "destroy".
$().cropper({
ready: function () {
$().cropper('method', argument1, , argument2, ..., argumentN);
}
});Show the crop box manually.
$().cropper({
autoCrop: false,
ready: function () {
// Do something here
// ...
// And then
$(this).cropper('crop');
}
});Reset the image and crop box to their initial states.
Clear the crop box.
url:
onlyColorChanged (optional):
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:
offsetY (optional):
Move the canvas (image wrapper) with relative offsets.
$().cropper('move', 1);
$().cropper('move', 1, 0);
$().cropper('move', 0, -1);x:
y (optional):
Move the canvas (image wrapper) to an absolute point.
Zoom the canvas (image wrapper) with a relative ratio.
$().cropper('zoom', 0.1);
$().cropper('zoom', -0.1);Zoom the canvas (image wrapper) to an absolute ratio.
$().cropper('zoomTo', 1); // 1:1 (canvasData.width === canvasData.naturalWidth)Rotate the image with a relative degree.
Requires CSS3 2D Transforms support (IE 9+).
$().cropper('rotate', 90);
$().cropper('rotate', -90);Rotate the image to an absolute degree.
scaleX:
scaleY (optional):
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 verticalScale the abscissa of the image.
Scale the ordinate of the image.
rounded (optional):
(return value):
Output 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:
- Rotate the image with the rotate property.
- Scale the image with the scaleX and scaleY properties.
- Crop the image with the x, y, width and height properties.
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 to 1.
Output the container size data.
Output the image position, size and other related data.
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
}Change the canvas (image wrapper) position and size with new data.
Output the crop box position and size data.
Change the crop box position and size with new data.
options (optional):
(return value):
Notes:
Browser support:
Get a canvas drawn the cropped image. If it is not cropped, then returns a canvas drawn the whole image.
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,
fillColor: '#fff',
imageSmoothingEnabled: false,
imageSmoothingQuality: 'high',
});
// 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');
}
});
});Change the aspect ratio of the crop box.
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 has built completely.
event.originalEvent:
event.action:
This event fires when the canvas (image wrapper) or the crop box starts to change.
$().on('cropstart', 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.action: the same as "cropstart".
This event fires when the canvas (image wrapper) or the crop box is changing.
event.originalEvent:
event.action: the same as "cropstart".
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.oldRatio:
event.ratio:
This event fires when a cropper instance starts to zoom in or zoom out its canvas (image wrapper).
$().on('zoom', 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.
| Back | FazBrowse Home | New Git URL |