这是一个我在写之前的项目的途中发现的一个国人写的jQuery图像裁剪插件,当时想实现用户资料的头像上传功能,而且可以预览图片,和对图片进行简单的裁剪、旋转,花了很多时间才看到了这个插件,感受功能挺全面,代码实现起来也挺简单,再加上用的是Bootstrap,对移动端操做也有适配,因而就用了。如今稍微有点时间就记录一下,方便之后再用的时候查阅。另外也有对应的js版本。javascript
兼容全部支持了Canvas的浏览器(IE9+),一小部分功能例外,具体请查看官方文档。css
Number
0
这个具体每一个值对应的效果我也不是很清楚,推荐在上面的官方示例里都试一试,我都是比较喜欢2。html
String
'crop'
'crop'
: 在裁剪框外拖动鼠标会生成一个新的裁剪框。'move'
: 在裁剪框外拖动鼠标会移动原图。'none'
: 在裁剪框外拖动鼠标则什么也不作。Number
NaN
这个是裁剪框的纵横比,默认是不限制的。例如1:1的头像就写1,16:9可写成16 / 9
。java
Object
null
The previous cropped data if you had stored, will be passed to setData
method automatically.jquery
(没怎么用过,都是直接用setData
方法)git
String
(jQuery selector)''
预览图的位置,用jQuery选择器表示。github
Boolean
true
在更改窗口大小后是否从新渲染cropper。web
Boolean
true
在更改窗口大小后是否恢复裁剪区域。ajax
Boolean
true
检查图像是不是跨域图像。(具体查看官方文档)json
Boolean
true
(具体查看官方文档)
Boolean
true
非裁剪区域是否用黑罩遮盖。
Boolean
true
裁剪区域是否显示虚线。
Boolean
true
裁剪区域正中央是否显示+号。
Boolean
true
裁剪区域是否高亮显示。
Boolean
true
是否显示背景的黑白方格(相似PS里透明图层的显示方式)。
Boolean
true
cropper初始化完成后是否自动显示裁剪框
Number
0.8
(80% of the image)自动显示的裁剪框的大小。所以,数字应当在0~1之间。
Boolean
true
是否容许移动原图。(若是这里填false
那么尽管dragMode的值是move
,在裁剪框外拖动也不会移动原图)
Boolean
true
是否能够旋转原图。
Boolean
true
是否能够对原图进行纵横拉伸。
例如把原图宽度拉长为原来的2倍或者拉长为原来的-1倍(即水平翻转)。
Boolean
true
是否能够对原图进行缩小放大。
Boolean
true
是否容许在移动端上使用双指触摸缩放原图。
Boolean
true
是否容许使用鼠标滚轮缩放原图。
Number
0.1
当使用鼠标滚轮缩放时的比例。
Boolean
true
是否容许移动裁剪框。
Boolean
true
是否容许经过拖动裁剪框的边框来调整裁剪框的大小。
Boolean
true
是否容许经过双击来在crop
和move
之间切换dragMode。
Number
200
容器宽度最小值。
Number
100
容器高度最小值。
Number
0
canvas(原图)宽度最小值。
Number
0
canvas(原图)高度最小值。
Number
0
剪切框宽度最小值。
Note: This size is relative to the page, not the image.
Number
0
剪切框高度最小值。
Note: This size is relative to the page, not the image.
Function
null
A shortcut of the "ready" event.
Function
null
A shortcut of the "cropstart" event.
Function
null
A shortcut of the "cropmove" event.
Function
null
A shortcut of the "cropend" event.
Function
null
A shortcut of the "crop" event.
Function
null
A shortcut of the "zoom" event.
除了"setAspectRatio","replace"和"destroy"之外,全部的方法都要在ready后才能使用。这里只介绍几个经常使用的方法,所有的方法请到官方文档查阅。
方法的使用格式为
$().cropper('method',arg0,arg1,arg2,...);
手动显示裁剪框。
$().cropper({ autoCrop: false, ready: function () { // Do something here // ... // And then $(this).cropper('crop'); } });
恢复所有到初始状态。
String
A new image url.
Boolean
If not present, its default value is false
.
替换cropper中的图像文件,一般第二个参数无论。
销毁cropper,而且会移除img标签的src属性的值。
Object
width
: the destination width of the output canvas.height
: the destination height of the output canvas.minWidth
: the minimum destination width of the output canvas, the default value is 0
.minHeight
: the minimum destination height of the output canvas, the default value is 0
.maxWidth
: the maximum destination width of the output canvas, the default value is Infinity
.maxHeight
: the maximum destination height of the output canvas, the default value is Infinity
.fillColor
: a color to fill any alpha values in the output canvas, the default value is transparent
.imageSmoothingEnabled
: set to change if images are smoothed (true
, default) or not (false
).imageSmoothingQuality
: set the quality of image smoothing, one of "low" (default), "medium", or "high".HTMLCanvasElement
fillColor
参数,不然裁剪后的透明部分默认会由黑色填充。获得裁剪到的图像的canvas,若是没有裁剪,那么就返回的是整个原图图像的canvas。
这是最重要的一个方法,经过这个方法就能够获得裁剪后的图像,再使用toDataURL()
获得base64 dataURL(不指定格式的话会是png格式)或者toBlob()
获得Blob,而后就能够很轻松地将图片上传至服务器上或者显示在某个img标签中了。例如:
// 转换为png格式的dataURL var dataURL = $().cropper('getCroppedCanvas', { width:100, height:100 }).toDataURL('image/png'); // 转换为Blob后显示在img标签中 var URL = window.URL || window.webkitURL; $().cropper('getCroppedCanvas', { width:100, height:100 }).toBlob(function (blob) { $().attr('src',URL.createObjectURL(blob)); });
接下来只是实现一个简单的功能:网页中能够上传图片,而后对图片进行裁剪,点击肯定后会显示出裁剪后的图片。
代码以下:
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>裁剪图片</title> <link href="https://cdn.bootcss.com/cropper/3.1.3/cropper.min.css" rel="stylesheet"> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <style> .row{ margin-bottom: 5px; } #photo { max-width: 100%; } .img-preview { width: 100px; height: 100px; overflow: hidden; } button { margin-top:10px; } #result { width: 150px; height: 150px; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-sm-12 text-center"> <label for="input" class="btn btn-danger" id=""> <span>选择图片</span> <input type="file" id="input" class="sr-only"> </label>