引入cropper插件
<link rel="stylesheet" href="../plugins/cropper/css/cropper.min.css">
<link rel="stylesheet" href="../plugins/cropper/css/imgcropping.css">
<script src="../plugins/cropper/js/cropper.min.js"></script>
<script src="../plugins/cropper/js/cropper.upload.js"></script>
imgcropping.css自定义样式:
*{
margin: 0;padding: 0;
}
.l-btn{
display: inline-block;
outline: none;
resize: none;
border: none;
padding:5px 10px;
background: rgb(24,166,137);
color: #fff;
border:solid 0px;
border-radius: 3px;
font-size: 14px;
}
.l-btn:hover{
background: rgb(24,166,137,0.7);
animation: anniu 1s infinite;
}
.l-btn:active{
box-shadow: 0 2px 3px rgba(0,0,0,.2) inset;
}
.hidden{
display: none;
}
.tailoring-container, .tailoring-container div, .tailoring-container p{
margin: 0;padding: 0;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
}
.tailoring-container{
position: fixed;
width: 100%;
height: 100%;
z-index: 1000;
top: 0;
left: 0;
}
.tailoring-container .black-cloth{
position: fixed;
width: 100%;
height: 100%;
background: #111;
opacity: .9;
z-index: 1001;
}
.tailoring-container .tailoring-content{
position: absolute;
width: 768px;
height: 560px;
background: #fff;
z-index: 1002;
left: 0;
top: 0;
/* 水平垂直居中 浏览器版本号低的不支持transform */
/*left: 50%;
top: 50%;
transform: translate(-50%,-50%);
-weblit-transform: translate(-50%,-50%);
-moz-transform: translate(-50%,-50%);
-ms-transform: translate(-50%,-50%);
-o-transform: translate(-50%,-50%);*/
border-radius: 10px;
box-shadow: 0 0 10px #000;
padding: 10px;
}
.tailoring-content-one{
height: 40px;
width: 100%;
border-bottom: 1px solid #DDD ;
}
.tailoring-content .choose-btn{
float: left;
}
.tailoring-content .close-tailoring{
display: inline-block;
height: 30px;
width: 30px;
border-radius: 100%;
background: #ebebeb;
color: #666;
font-size: 22px;
text-align: center;
line-height: 30px;
float: right;
cursor: pointer;
}
.tailoring-content .close-tailoring:hover{
background: #ccc;
color: #fff;
}
.tailoring-content .tailoring-content-two{
width: 100%;
height: 460px;
position: relative;
padding: 5px 0;
}
.tailoring-content .tailoring-box-parcel{
width: 520px;
height: 450px;
position: absolute;
left: 0;
border: solid 1px #ddd;
}
.tailoring-content .preview-box-parcel{
display: inline-block;
width: 228px;
height: 450px;
position: absolute;
right: 0;
padding: 4px 14px;
}
.preview-box-parcel p{
color: #555;
}
.previewImg{
width: 200px;
height: 200px;
overflow: hidden;
}
.preview-box-parcel .square{
margin-top: 10px;
border: solid 1px #ddd;
}
.preview-box-parcel .circular{
border-radius: 100%;
margin-top: 10px;
border: solid 1px #ddd;
}
.tailoring-content .tailoring-content-three{
width: 100%;
height: 40px;
border-top: 1px solid #DDD ;
padding-top: 10px;
}
.sureCut{
float: right;
}
@media all and (max-width: 768px) {
.tailoring-container .tailoring-content{
width: 100%;
min-width: 320px;
height: 460px;
}
.tailoring-content .tailoring-content-two{
height: 360px;
}
.tailoring-content .tailoring-box-parcel{
height: 350px;
}
.tailoring-container .tailoring-box-parcel{
width: 100%;
}
.tailoring-container .preview-box-parcel{
display: none;
}
}
cropper.upload.js 自定义js组件,参数默认值不知从何时起已经支持(img={})样式,真是方便
$.fn.imgUpload = function (img = {width:800,height:800,type:'base64'}) {
var casWin = ''+
'<div style="display: none" class="tailoring-container">'+
'<div class="black-cloth"></div>'+
'<div class="tailoring-content">'+
'<div class="tailoring-content-one">'+
'<label title="上传图片" for="chooseImg" class="l-btn choose-btn">'+
'<input type="file" accept="image/jpg,image/jpeg,image/png" name="file" id="chooseImg" class="hidden">'+
'选择图片'+
'</label>'+
'<div class="close-tailoring">×</div>'+
'</div>'+
'<div class="tailoring-content-two">'+
'<div class="tailoring-box-parcel">'+
'<img id="tailoringImg">'+
'</div>'+
'<div class="preview-box-parcel">'+
'<p>图片预览:</p>'+
'<div class="square previewImg"></div>'+
'<div class="circular previewImg"></div>'+
'</div>'+
'</div>'+
'<div class="tailoring-content-three">'+
'<button class="l-btn cropper-reset-btn">复位</button> '+
'<button class="l-btn cropper-rotate-btn">顺时针15°</button> '+
'<button class="l-btn cropper-scaleX-btn">水平翻转</button>'+
'<button class="l-btn sureCut" id="sureCut">确定</button>'+
'</div>'+
'</div>'+
'</div>';
if(!$('body.tailoring-container')[0]){$('body').append(casWin);}
var win_height = $(window).height();
var win_width = $(window).width();
if (win_width <= 768){
$(".tailoring-content").css({
"top": (win_height - $(".tailoring-content").outerHeight())/2,
"left": 0
});
}else{
$(".tailoring-content").css({
"top": (win_height - $(".tailoring-content").outerHeight())/2>0?(win_height - $(".tailoring-content").outerHeight())/2:10,
"left": (win_width - $(".tailoring-content").outerWidth())/2
});
}
var me = $(this);
var o = $('#tailoringImg');
var upload = {};
var options = {
aspectRatio: img.width/img.height,//默认比例
preview: '.previewImg',//预览视图
guides: true, //裁剪框的虚线(九宫格)
autoCropArea: 0.9, //0-1之间的数值,定义自动剪裁区域的大小,默认0.8
movable: false, //是否允许移动图片
dragCrop: true, //是否允许移除当前的剪裁框,并通过拖动来新建一个剪裁框区域
movable: true, //是否允许移动剪裁框
resizable: true, //是否允许改变裁剪框的大小
zoomable: true, //是否允许缩放图片大小
mouseWheelZoom: false, //是否允许通过鼠标滚轮来缩放图片
touchDragZoom: true, //是否允许通过触摸移动来缩放图片
rotatable: true, //是否允许旋转图片
crop: function(e) {
// 输出结果数据裁剪图像。
}
};
o.cropper('destroy').attr('src', null).cropper(options) //生成,保持每次载入都是空的
upload.close = function (casDiv) {$(casDiv).toggle();}
upload.submit = function (callback) {
var that = this;
$('#sureCut').on('click', function () {
if (o.attr("src") == null ){
return false;
}else{
that.close('.tailoring-container');
var imgSize = {width:img.width,height:img.height}; //裁剪后的图片尺寸
var cas = o.cropper('getCroppedCanvas',imgSize); //获取被裁剪后的canvas
if(img.type == 'base64'){
var base64url = cas.toDataURL('image/jpg'); //转换为base64地址形式
callback && callback(base64url);
}else{
cas.toBlob(function (e) {
var timestamp = Date.parse(new Date());
e.name = timestamp+".jpeg";
// console.log(e); //生成Blob的图片格式
callback && callback(e);
},'image/jpeg',0.95);
}
}
})
}
me.on("click",function () {
upload.close('.tailoring-container');
});
//关闭
$(".close-tailoring,.black-cloth").on("click",function () {
upload.close('.tailoring-container');
});
//旋转
$(".cropper-rotate-btn").on("click",function () {
o.cropper("rotate", 15);
});
//复位
$(".cropper-reset-btn").on("click",function () {
o.cropper("reset");
});
//换向
var flagX = true;
$(".cropper-scaleX-btn").on("click",function () {
if(flagX){
o.cropper("scaleX", -1);
flagX = false;
}else{
o.cropper("scaleX", 1);
flagX = true;
}
flagX != flagX;
});
var uploadedImageURL;
//选择文件
$('#chooseImg').on("change",function () {
var files = this.files;
var file;
if (!o.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
o.cropper('destroy').attr('src', uploadedImageURL).cropper(options);
}
}
});
return upload;
};
多图片时批量上传情景应用示例1:(根据当前项目需求,所以用base64数据流传)
//返回800*800 base64格式的图片
//产品图片上传 #replaceImg按钮 .goods_imgs图片容器
$("#replaceImg").imgUpload({width:800,height:800,type:'base64'}).submit(function (base64url) {
var imgHtml = '<img class="up_img_view" src="' + base64url + '" v="0">';
$(".goods_imgs").append(imgHtml);
});
单图片上传情景应用示例2:
//返回200*200 blob格式的图片并上传到后台
//头像上传
$("#staffFace").imgUpload({width:200,height:200,type:'blob'}).submit(function (e) {
// var blobUrl = window.URL.createObjectURL(e);
// $("#staffFace img").attr('src',blobUrl);
var params = new FormData();
var nameImg = new Date().getTime()+'.jpg';
params.append("image", e,nameImg);
params.append("cmd", 'upload_blob');
params.append("type", 'headface');
$.ajax({
type: "post",
url: "xxxx.php",
async: true,
data: params,
dataType:"json",
processData: false, //必带项
contentType: false, //必带项
success: function(rs) {
if(Number(rs.code)==1){
$("#staffFace img").attr('src',rs.data);
}
}
});
});
完成图: