插件介绍

这是一个我在写以前的项目的途中发现的一个国人写的jQuery图像裁剪插件,当时想实现用户资料的头像上传功能,并且能够预览图片,和对图片进行简单的裁剪、旋转,花了不少时间才看到了这个插件,感觉功能挺全面,代码实现起来也挺简单,再加上用的是Bootstrap,对移动端操作也有适配,于是就用了。现在稍微有点时间就记录一下,方便以后再用的时候查阅。另外也有对应的js版本。

官方文档(英文)

兼容性

兼容所有支持了Canvas的浏览器(IE9+),一小部分功能例外,具体请查看官方文档。

参数

viewMode

Type: Number

Default: 0

Options: 0,1,2,3

这个具体每个值对应的效果我也不是很清楚,推荐在上面的官方示例里都试一试,我都是比较喜欢2。

dragMode

Type: String

Default: 'crop'

Options:

'crop': 在裁剪框外拖动鼠标会生成一个新的裁剪框。

'move': 在裁剪框外拖动鼠标会移动原图。

'none': 在裁剪框外拖动鼠标则什么也不做。

aspectRatio

Type: Number

Default: NaN

这个是裁剪框的纵横比,默认是不限制的。例如1:1的头像就写1,16:9可写成16 / 9。

data

Type: Object

Default: null

The previous cropped data if you had stored, will be passed to setData method automatically.

(没怎么用过,都是直接用setData方法)

preview

Type: String (jQuery selector)

Default: ''

预览图的位置,用jQuery选择器表示。

responsive

Type: Boolean

Default: true

在更改窗口大小后是否重新渲染cropper。

restore

Type: Boolean

Default: true

在更改窗口大小后是否恢复裁剪区域。

checkCrossOrigin

Type: Boolean

Default: true

检查图像是否是跨域图像。(具体查看官方文档)

checkOrientation

Type: Boolean

Default: true

(具体查看官方文档)

modal

Type: Boolean

Default: true

非裁剪区域是否用黑罩遮盖。

guides

Type: Boolean

Default: true

裁剪区域是否显示虚线。

center

Type: Boolean

Default: true

裁剪区域正中央是否显示+号。

highlight

Type: Boolean

Default: true

裁剪区域是否高亮显示。

background

Type: Boolean

Default: true

是否显示背景的黑白方格(类似PS里透明图层的显示方式)。

autoCrop

Type: Boolean

Default: true

cropper初始化完成后是否自动显示裁剪框

autoCropArea

Type: Number

Default: 0.8 (80% of the image)

自动显示的裁剪框的大小。因此,数字应当在0~1之间。

movable

Type: Boolean

Default: true

是否允许移动原图。(如果这里填false那么尽管dragMode的值是move,在裁剪框外拖动也不会移动原图)

rotatable

Type: Boolean

Default: true

是否可以旋转原图。

scalable

Type: Boolean

Default: true

是否可以对原图进行纵横拉伸。

例如把原图宽度拉长为原来的2倍或者拉长为原来的-1倍(即水平翻转)。

zoomable

Type: Boolean

Default: true

是否可以对原图进行缩小放大。

zoomOnTouch

Type: Boolean

Default: true

是否允许在移动端上使用双指触摸缩放原图。

zoomOnWheel

Type: Boolean

Default: true

是否允许使用鼠标滚轮缩放原图。

wheelZoomRatio

Type: Number

Default: 0.1

当使用鼠标滚轮缩放时的比例。

cropBoxMovable

Type: Boolean

Default: true

是否允许移动裁剪框。

cropBoxResizable

Type: Boolean

Default: true

是否允许通过拖动裁剪框的边框来调整裁剪框的大小。

toggleDragModeOnDblclick

Type: Boolean

Default: true

是否允许通过双击来在crop和move之间切换dragMode。

minContainerWidth

Type: Number

Default: 200

容器宽度最小值。

minContainerHeight

Type: Number

Default: 100

容器高度最小值。

minCanvasWidth

Type: Number

Default: 0

canvas(原图)宽度最小值。

minCanvasHeight

Type: Number

Default: 0

canvas(原图)高度最小值。

minCropBoxWidth

Type: Number

Default: 0

剪切框宽度最小值。

Note: This size is relative to the page, not the image.

minCropBoxHeight

Type: Number

Default: 0

剪切框高度最小值。

Note: This size is relative to the page, not the image.

ready

Type: Function

Default: null

A shortcut of the "ready" event.

cropstart

Type: Function

Default: null

A shortcut of the "cropstart" event.

cropmove

Type: Function

Default: null

A shortcut of the "cropmove" event.

cropend

Type: Function

Default: null

A shortcut of the "cropend" event.

crop

Type: Function

Default: null

A shortcut of the "crop" event.

zoom

Type: Function

Default: null

A shortcut of the "zoom" event.

常用方法

除了"setAspectRatio","replace"和"destroy"以外,所有的方法都要在ready后才能使用。这里只介绍几个常用的方法,全部的方法请到官方文档查阅。

方法的使用格式为

$().cropper('method',arg0,arg1,arg2,...);

crop()

手动显示裁剪框。

$().cropper({

autoCrop: false,

ready: function () {

// Do something here

// ...

// And then

$(this).cropper('crop');

}

});

reset()

恢复全部到初始状态。

replace(url[, onlyColorChanged])

url:

Type: String

A new image url.

onlyColorChanged (optional):

Type: Boolean

If only change the color, not the size, then the cropper only need to change the srcs of all related images, not need to rebuild the cropper. This can be used for applying filters.

If not present, its default value is false.

替换cropper中的图像文件,通常第二个参数不管。

destroy()

销毁cropper,并且会移除img标签的src属性的值。

getCroppedCanvas([options])

options (optional):

Type: Object

Properties:

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".

(return value):

Type: HTMLCanvasElement

A canvas drawn the cropped image.

Notes:

输出的canvas的纵横比会自动适应于裁剪框的纵横比.

如果打算得到JPEG图像,那么应该先设置fillColor参数,否则裁剪后的透明部分默认会由黑色填充。

Browser support:

Basic image: requires Canvas support (IE 9+).

Rotated image: requires CSS3 2D Transforms support (IE 9+).

Cross-origin image: requires HTML5 CORS settings attributes support (IE 11+).

得到裁剪到的图像的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));

});

简单实例

在页面直接使用cropper

接下来只是实现一个简单的功能:网页中可以上传图片,然后对图片进行裁剪,点击确定后会显示出裁剪后的图片。

代码如下:

裁剪图片

.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;

}

选择图片

预览(100*100):

裁剪图片

结果:

// 修改自官方demo的js

var initCropper = function (img, input){

var $image = img;

var options = {

aspectRatio: 1, // 纵横比

viewMode: 2,

preview: '.img-preview' // 预览图的class名

};

$image.cropper(options);

var $inputImage = input;

var uploadedImageURL;

if (URL) {

// 给input添加监听

$inputImage.change(function () {

var files = this.files;

var file;

if (!$image.data('cropper')) {

return;

}

if (files && files.length) {

file = files[0];

// 判断是否是图像文件

if (/^image\/\w+$/.test(file.type)) {

// 如果URL已存在就先释放

if (uploadedImageURL) {

URL.revokeObjectURL(uploadedImageURL);

}

uploadedImageURL = URL.createObjectURL(file);

// 销毁cropper后更改src属性再重新创建cropper

$image.cropper('destroy').attr('src', uploadedImageURL).cropper(options);

$inputImage.val('');

} else {

window.alert('请选择一个图像文件!');

}

}

});

} else {

$inputImage.prop('disabled', true).addClass('disabled');

}

}

var crop = function(){

var $image = $('#photo');

var $target = $('#result');

$image.cropper('getCroppedCanvas',{

width:300, // 裁剪后的长宽

height:300

}).toBlob(function(blob){

// 裁剪后将图片放到指定标签

$target.attr('src', URL.createObjectURL(blob));

});

}

$(function(){

initCropper($('#photo'),$('#input'));

});

在bootstrap模态框中使用cropper

虽然在模态框中可以像上面一样使用cropper,甚至我以前写的项目也是跟上面一样,但是这次整理的时候突然发现了一个bug:当隐藏模态框后调整浏览器大小(甚至按f12),再打开模态框后cropper的容器会改变,导致难以使用。于是,我在GitHub中翻找了issue,在官方的example中找到了对应的解决方法。但其实这个解决方法也是一种暴力解法,即模态框隐藏后销毁cropper,打开后重新创建cropper,可能会有别的方法,因为不确定会不会有别的bug,所以暂时还是用官方的方法比较好。

代码如下:

上传头像

body{

text-align: center;

}

#user-photo {

width:300px;

height:300px;

margin-top: 10px;

}

#photo {

max-width:100%;

max-height:350px;

}

.img-preview-box {

text-align: center;

}

.img-preview-box > div {

display: inline-block;;

margin-right: 10px;

}

.img-preview {

overflow: hidden;

}

.img-preview-box .img-preview-lg {

width: 150px;

height: 150px;

}

.img-preview-box .img-preview-md {

width: 100px;

height: 100px;

}

.img-preview-box .img-preview-sm {

width: 50px;

height: 50px;

border-radius: 50%;

}

打开

cropper初始化_【jQuery插件分享】Cropper——一个简单方便的图片裁剪插件相关推荐

  1. 【jQuery插件分享】Cropper——一个简单方便的图片裁剪插件

    插件介绍 这是一个我在写以前的项目的途中发现的一个国人写的jQuery图像裁剪插件,当时想实现用户资料的头像上传功能,并且能够预览图片,和对图片进行简单的裁剪.旋转,花了不少时间才看到了这个插件,感觉 ...

  2. vue 图片剪辑_一个简单的Vue图片剪辑插件

    vue 图片剪辑 Vue-Crpopper (vue-crpopper) A simple picture clipping plugin for vue. 一个简单的Vue图片剪辑插件. 安装 (i ...

  3. 一个简单的 jQuery 图片裁剪插件----cropper

    浏览器支持 Chrome (latest 2) Firefox (latest 2) Internet Explorer 8+ Opera (latest 2) Safari (latest 2) C ...

  4. Cropper – 简单的 jQuery 图片裁剪插件

    Cropper 是一个简单的 jQuery 图像裁剪插件.它支持选项,方法,事件,触摸(移动),缩放,旋转.输出的裁剪数据基于原始图像大小,这样你就可以用它们来直接裁剪图像. 如果你尝试裁剪跨域图像, ...

  5. jquery手写轮播图_用jQuery如何手写一个简单的轮播图?(附代码)

    用jQuery如何手写一个简单的轮播图?下面本篇文章通过代码示例来给大家介绍一下.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. 用 jQuery 手写轮播图 先上个效果截图: 主要 ...

  6. jquery 图片裁剪 java_[Java教程]5 款最新的 jQuery 图片裁剪插件

    [Java教程]5 款最新的 jQuery 图片裁剪插件 0 2015-05-18 16:00:20 这篇文章主要介绍最新的 5 款 jQuery 图片裁剪插件,可以帮助你轻松的实现你网站需要的图像裁 ...

  7. jQuery 图片裁剪插件 Jcrop

    Jcrop是一个jQuery图片裁剪插件,它能为你的WEB应用程序快速简单地提供图片裁剪的功能.特点如下: 对所有图片均unobtrusively(无侵入的,保持DOM简洁) 支持宽高比例锁定 支持 ...

  8. Croppic – 免费开源的 jQuery 图片裁剪插件

    Croppic 这款开源的 jQuery 图片裁剪插件能够满足网站开发人员各种不同的使用需要.只需要简单的上传图片,就可以实现你想要的图像缩放和裁剪功能.因为使用了 HTML5 FormData  对 ...

  9. 几个jQuery的图片裁剪插件

    [url]http://hi.baidu.com/coffeant/item/9931bd684220d237ac3e8396[/url] 项目里最近也要用到图片裁剪插件,刚巧发现了几个就记录在这里. ...

  10. 小程序图片裁剪插件image-cropper实现个人头像上传裁剪功能

    小程序图片裁剪插件image-cropper实现个人头像上传裁剪功能 参考文档:小程序图片裁剪插件 image-cropper 整体效果流程图 一.第一步引入image-cropper,放在dist文 ...

最新文章

  1. Java中switch都可以支持哪些数据类型
  2. RocketMQ集群搭建-4.2.0版本
  3. tomcat 绑定ipv4端口
  4. [LeetCode]二进制求和
  5. C#中静态方法和非静态方法的区别
  6. python 知乎接口_Python采用Django制作简易的知乎日报API
  7. mfc140dll 丢失 微软常用运行库_微软常用运行库合集 2020.9月(32amp;64位)
  8. 【JS 逆向百例】PEDATA 加密资讯以及 zlib.gunzipSync() 的应用
  9. Qt:Qt实现Winsock网络编程—TCP服务端和客户端通信(多线程)
  10. 真的不再使用*狗输入法了,改七七拼音
  11. Python 练习: 打印0到99小于50或大于70的数字
  12. 网络安全实验室CTF—注入关 writeup
  13. 手撕包菜BT搜索引擎带爬虫自动抓取安装
  14. webQQ协议分析2018.11.9
  15. Java基本数据类型字节长度
  16. Activity生命周期走向分析
  17. 下载kaggle数据集出现的一系列问题
  18. 系统中flvplayer.swf播放器的使用及配置
  19. 博图买什么样配置的笔记本_西门子PLC编程软件-博图软件用什么配置的电脑最好?...
  20. ipad已停用恢复系统 2种方法

热门文章

  1. android toast 显示在最上面,Android Toast在屏幕上移动
  2. linux系统日志message 分析,Linux系统日志及日志分析
  3. powerdesign新手入门详细教程
  4. 计算机dll修复工具,DLL修复工具哪个好?五款修复能力强推荐
  5. HCSE设计知识点50个
  6. 关于联想硬盘保护卡还原卡同传不能连接无法登录的解决方法
  7. JSP技术的优缺点介绍
  8. linux hd4000显卡驱动,AMD 即将放弃 Radeon HD2000/3000/4000 系列显卡的驱动支持
  9. 人工智能之启发式搜索算法
  10. 微信扫码登录原理解析