cdnjs provides CDN support for Cropper.js's CSS and JavaScript. You can find the links here.
Usage
Syntax
newCropper(element[,options])
element
Type: HTMLImageElement or HTMLCanvasElement
The target image or canvas element for cropping.
options (optional)
Type: Object
The options for cropping. Check out the available options.
Example
<!-- Wrap the image or canvas element with a block element (container) --><div><imgid="image" src="picture.jpg"></div>
/* Ensure the size of the image fit the container perfectly */img {
display: block;
/* This rule is very important, please don't ignore this */max-width:100%;
}
How to crop a new area after zooming in or zooming out?
Just double-click your mouse to enter crop mode.
How to move the image after cropping an area?
Just double-click your mouse to enter move mode.
How to fix the aspect ratio in free ratio mode?
Just hold the Shift key when you resize the crop box.
How to crop a square area in free ratio mode?
Just hold the Shift key when you crop on the image.
Notes
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 is shown completely. Otherwise, you will not get the correct cropper.
The outputted cropped data is based 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 issues
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 (preferably below 1024 pixels) before starting a cropper.
Known image size increase: When exporting the cropped image on the 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. 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 new Cropper(image, options).
If you want to change the global default options, You may use Cropper.setDefaults(options).
viewMode
Type: Number
Default: 0
Options:
0: no restrictions
1: restrict the crop box not to exceed the size of the canvas.
2: restrict the minimum canvas size to fit within the container. If the proportions of the canvas and the container differ, the minimum canvas will be surrounded by extra space in one of the dimensions.
3: restrict the minimum canvas size to fill fit the container. If the proportions of the canvas and the container are different, the container will not be able to fit the whole canvas in one of the dimensions.
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. viewMode of 2 or 3 will additionally restrict the canvas to the container. There is no difference between 2 and 3 when the proportions of the canvas and the container are the same.
dragMode
Type: String
Default: 'crop'
Options:
'crop': create a new crop box
'move': move the canvas
'none': do nothing
Define the dragging mode of the cropper.
initialAspectRatio
Type: Number
Default: NaN
Define the initial aspect ratio of the crop box. By default, it is the same as the aspect ratio of the canvas (image wrapper).
Only available when the aspectRatio option is set to NaN.
aspectRatio
Type: Number
Default: NaN
Define the fixed aspect ratio of the crop box. By default, the crop box has a free ratio.
data
Type: Object
Default: null
The previous cropped data you stored will be passed to the setData method automatically when initialized.
Only available when the autoCrop option had set to the true.
preview
Type: Element, Array (elements), NodeList or String (selector)
Default: ''
An element or an array of elements or a node list object or a valid selector for Document.querySelectorAll
Add extra elements (containers) for preview.
Notes:
The maximum width is the initial width of the preview container.
The maximum height is the initial height of the preview container.
If you set an aspectRatio option, be sure to set the same aspect ratio to the preview container.
If the preview does not display correctly, set the overflow: hidden style to the preview container.
responsive
Type: Boolean
Default: true
Re-render the cropper when resizing the window.
restore
Type: Boolean
Default: true
Restore the cropped area after resizing the window.
checkCrossOrigin
Type: Boolean
Default: true
Check if the current image is a cross-origin image.
If so, a crossOrigin attribute will be added to the cloned image element, and a timestamp parameter will be added to the src attribute to reload the source image to avoid browser cache error.
Adding a crossOrigin attribute to the image element will stop adding a timestamp to the image URL and stop reloading the image. But the request (XMLHttpRequest) to read the image data for orientation checking will require a timestamp to bust the cache to avoid browser cache error. You can set the checkOrientation option to false to cancel this request.
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.
checkOrientation
Type: Boolean
Default: true
Check the current image's Exif Orientation information. Note that only a JPEG image may contain Exif Orientation information.
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 (1, 2) on iOS devices.
Requires to set both the rotatable and scalable options to true at the same time.
Note: Do not trust this all the time as some JPG images may have incorrect (non-standard) Orientation values
As there is an asynchronous process when loading the image, you should call most of the methods after ready, except setAspectRatio, replace and destroy.
If a method doesn't need to return any value, it will return the cropper instance (this) for chain composition.