cropbox php,jQuery上传、拖动、裁剪头像插件cropbox.js

做网页开发少不了就是头像上传,但是我们并不希望用户选择多大的图片就有多大,那么服务器吃不消的。我们需要用户自己裁剪头像到程序固定的大小。就介绍一款我非常喜欢的插件cropbox.js,如果还在纠结怎么弄?那么看这篇文章就对了!咳咳咳~~~源码里面有很多美图哦

6c269f0b48c0

3087014508.png

首先肯定需要导入需要的插件cropbox.js

然后需要一个上传的按钮

缩小、放大、裁剪按钮,不用说了吧。

现在就是js代码了,和运用cropbox.js

1.上传头像事件

var options =

{

thumbBox: '.thumbBox',

spinner: '.spinner',

imgSrc: 'img/5.jpg'

}

var cropper = $('.imageBox').cropbox(options);

$('#upload-file').on('change', function()

{

var reader = new FileReader();

reader.onload = function(e) {

options.imgSrc = e.target.result;

cropper = $('.imageBox').cropbox(options);

}

reader.readAsDataURL(this.files[0]);

this.files = [];

})

2.裁剪

$('#btnCrop').on('click', function(){

var img = cropper.getDataURL();

$('.cropped').html('');

$('.cropped').append('![]('+img+')

64px*64px

');

$('.cropped').append('![]('+img+')

128px*128px

');

$('.cropped').append('![]('+img+')

180px*180px

');

})

3.缩放

$('#btnZoomIn').on('click', function(){

cropper.zoomIn();

})

$('#btnZoomOut').on('click', function(){

cropper.zoomOut();

})

都是很简单的代码。注意,所有图片数据都是Base64编码。

完整js代码

$(window).load(function() {

var options =

{

thumbBox: '.thumbBox',

spinner: '.spinner',

imgSrc: 'img/5.jpg'

}

var cropper = $('.imageBox').cropbox(options);

$('#upload-file').on('change', function(){

var reader = new FileReader();

reader.onload = function(e) {

options.imgSrc = e.target.result;

cropper = $('.imageBox').cropbox(options);

}

reader.readAsDataURL(this.files[0]);

this.files = [];

})

$('#btnCrop').on('click', function(){

var img = cropper.getDataURL();

$('.cropped').html('');

$('.cropped').append('![]('+img+')

64px*64px

');

$('.cropped').append('![]('+img+')

128px*128px

');

$('.cropped').append('![]('+img+')

180px*180px

');

})

$('#btnZoomIn').on('click', function(){

cropper.zoomIn();

})

$('#btnZoomOut').on('click', function(){

cropper.zoomOut();

})

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值