/*! * Cropper v3.0.0 */ layui.config({ base: laybase+'Scripts/layui/ext/' //layui自定义layui组件目录 }).define(['jquery','layer','cropper'],function (exports) { var $ = layui.jquery ,layer = layui.layer; var html = "\n" + "
\n" + "
\n" + "
\n" + " \n" + " \n" + "
\n" + "
头像的尺寸限定150x150px,大小在50kb以内
\n" + "
\n" + "
\n" + "
\n" + "
\n" + " \n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + " \n" + " \n" + "
\n" + "
\n" + " \n" + "
\n" + "
\n" + "
\n" + "
\n" + " \n" + "
\n" + "
\n" + "\n" + "
"; var obj = { render: function(e){ $('body').append(html); var self = this, elem = e.elem, saveW = e.saveW, saveH = e.saveH, mark = e.mark, area = e.area, url = e.url, done = e.done; var content = $('.showImgEdit') ,image = $(".showImgEdit .readyimg img") ,preview = '.showImgEdit .img-preview' ,file = $(".showImgEdit input[name='file']") , options = {aspectRatio: mark,preview: preview,viewMode:1}; $(elem).on('click',function () { layer.open({ type: 1 , content: content , area: area , success: function () { image.cropper(options); } , cancel: function (index) { layer.close(index); image.cropper('destroy'); } }); }); $(".layui-btn").on('click',function () { var event = $(this).attr("cropper-event"); //监听确认保存图像 if(event === 'confirmSave'){ image.cropper("getCroppedCanvas",{ width: saveW, height: saveH }).toBlob(function(blob){ var formData=new FormData(); formData.append('file',blob,'head.jpg'); $.ajax({ method:"post", url: url, //用于文件上传的服务器端请求地址 data: formData, processData: false, contentType: false, success: function (result) { result = JSON.parse(result); if (result.SourceImage!=undefined){ layer.closeAll('page'); return done(result); } else if (result.err !=undefined){ layer.alert("文件上传报错"); } } }); }); //监听旋转 }else if(event === 'rotate'){ var option = $(this).attr('data-option'); image.cropper('rotate', option); //重设图片 }else if(event === 'reset'){ image.cropper('reset'); } //文件选择 file.change(function () { var r= new FileReader(); var f=this.files[0]; r.readAsDataURL(f); r.onload=function (e) { image.cropper('destroy').attr('src', this.result).cropper(options); }; }); }); } }; exports('croppers', obj); });