croppers.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*!
  2. * Cropper v3.0.0
  3. */
  4. layui.config({
  5. base: '/layuiadmin/modules/' //layui自定义layui组件目录
  6. }).define(['jquery','layer','cropper'],function (exports) {
  7. var $ = layui.jquery
  8. ,layer = layui.layer;
  9. var html = "<link rel=\"stylesheet\" href=\"/admin/css/cropper.css\">\n" +
  10. "<div class=\"layui-fluid showImgEdit\" style=\"display: none; padding:20px\">\n" +
  11. " <div class=\"layui-form-item\">\n" +
  12. " <div class=\"layui-input-inline layui-btn-container\" style=\"width: auto;\">\n" +
  13. " <label for=\"cropper_avatarImgUpload\" class=\"layui-btn layui-btn-primary\">\n" +
  14. " <i class=\"layui-icon\">&#xe67c;</i>选择图片\n" +
  15. " </label>\n" +
  16. " <input class=\"layui-upload-file\" id=\"cropper_avatarImgUpload\" type=\"file\" value=\"选择图片\" name=\"file\">\n" +
  17. " </div>\n" +
  18. " <div class=\"layui-form-mid layui-word-aux\"></div>\n" +
  19. " </div>\n" +
  20. " <div class=\"layui-row layui-col-space15\">\n" +
  21. " <div class=\"layui-col-xs9\">\n" +
  22. " <div class=\"readyimg\" style=\"height:450px;background-color: rgb(247, 247, 247);\">\n" +
  23. " <img src=\"\" >\n" +
  24. " </div>\n" +
  25. " </div>\n" +
  26. " <div class=\"layui-col-xs3\">\n" +
  27. " <div class=\"img-preview\" style=\"width:200px;height:200px;overflow:hidden\">\n" +
  28. " </div>\n" +
  29. " </div>\n" +
  30. " </div>\n" +
  31. " <div class=\"layui-row layui-col-space15\">\n" +
  32. " <div class=\"layui-col-xs9\">\n" +
  33. " <div class=\"layui-row\">\n" +
  34. " <div class=\"layui-col-xs6\">\n" +
  35. " <button type=\"button\" class=\"layui-btn layui-icon layui-icon-left\" cropper-event=\"rotate\" data-option=\"-15\" title=\"Rotate -90 degrees\"> 向左旋转</button>\n" +
  36. " <button type=\"button\" class=\"layui-btn layui-icon layui-icon-right\" cropper-event=\"rotate\" data-option=\"15\" title=\"Rotate 90 degrees\"> 向右旋转</button>\n" +
  37. " </div>\n" +
  38. " <div class=\"layui-col-xs5\" style=\"text-align: right;\">\n" +
  39. " <button type=\"button\" class=\"layui-btn layui-icon layui-icon-refresh\" cropper-event=\"reset\" title=\"重置图片\"></button>\n" +
  40. " </div>\n" +
  41. " </div>\n" +
  42. " </div>\n" +
  43. " <div class=\"layui-col-xs3\">\n" +
  44. " <button class=\"layui-btn layui-btn-fluid\" cropper-event=\"confirmSave\" type=\"button\"> 保存修改</button>\n" +
  45. " </div>\n" +
  46. " </div>\n" +
  47. "\n" +
  48. "</div>";
  49. var obj = {
  50. render: function(e){
  51. $('body').append(html);
  52. var self = this,
  53. elem = e.elem,
  54. saveW = e.saveW,
  55. saveH = e.saveH,
  56. mark = e.mark,
  57. area = e.area,
  58. url = e.url,
  59. done = e.done;
  60. var content = $('.showImgEdit')
  61. ,image = $(".showImgEdit .readyimg img")
  62. ,preview = '.showImgEdit .img-preview'
  63. ,file = $(".showImgEdit input[name='file']")
  64. , options = {aspectRatio: mark,preview: preview,viewMode:1};
  65. $(elem).on('click',function () {
  66. layer.open({
  67. type: 1
  68. , content: content
  69. , area: area
  70. , success: function () {
  71. image.cropper(options);
  72. }
  73. , cancel: function (index) {
  74. layer.close(index);
  75. image.cropper('destroy');
  76. }
  77. });
  78. });
  79. $(".layui-btn").on('click',function () {
  80. var event = $(this).attr("cropper-event");
  81. //监听确认保存图像
  82. if(event === 'confirmSave'){
  83. image.cropper("getCroppedCanvas",{
  84. width: saveW,
  85. height: saveH
  86. }).toBlob(function(blob){
  87. var formData=new FormData();
  88. formData.append('file',blob,'head.jpg');
  89. $.ajax({
  90. method:"post",
  91. url: url, //用于文件上传的服务器端请求地址
  92. data: formData,
  93. processData: false,
  94. contentType: false,
  95. success: function (result) {
  96. layer.closeAll('page');
  97. return done(result);
  98. //if(result.code == 0){
  99. // layer.msg(result.msg,{icon: 1});
  100. // layer.closeAll('page');
  101. // return done(result.data.src);
  102. //}else if(result.code == -1){
  103. // layer.alert(result.msg,{icon: 2});
  104. //}
  105. }
  106. });
  107. });
  108. //监听旋转
  109. }else if(event === 'rotate'){
  110. var option = $(this).attr('data-option');
  111. image.cropper('rotate', option);
  112. //重设图片
  113. }else if(event === 'reset'){
  114. image.cropper('reset');
  115. }
  116. //文件选择
  117. file.change(function () {
  118. var r= new FileReader();
  119. var f=this.files[0];
  120. r.readAsDataURL(f);
  121. r.onload=function (e) {
  122. image.cropper('destroy').attr('src', this.result).cropper(options);
  123. };
  124. });
  125. });
  126. }
  127. };
  128. exports('croppers', obj);
  129. });