croppers.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\" title=\"移动\"></button>\n" +
  40. " <button type=\"button\" class=\"layui-btn\" title=\"放大图片\"></button>\n" +
  41. " <button type=\"button\" class=\"layui-btn\" title=\"缩小图片\"></button>\n" +
  42. " <button type=\"button\" class=\"layui-btn layui-icon layui-icon-refresh\" cropper-event=\"reset\" title=\"重置图片\"></button>\n" +
  43. " </div>\n" +
  44. " </div>\n" +
  45. " </div>\n" +
  46. " <div class=\"layui-col-xs3\">\n" +
  47. " <button class=\"layui-btn layui-btn-fluid\" cropper-event=\"confirmSave\" type=\"button\"> 保存修改</button>\n" +
  48. " </div>\n" +
  49. " </div>\n" +
  50. "\n" +
  51. "</div>";
  52. var obj = {
  53. render: function(e){
  54. $('body').append(html);
  55. var self = this,
  56. elem = e.elem,
  57. saveW = e.saveW,
  58. saveH = e.saveH,
  59. mark = e.mark,
  60. area = e.area,
  61. url = e.url,
  62. done = e.done;
  63. var content = $('.showImgEdit')
  64. ,image = $(".showImgEdit .readyimg img")
  65. ,preview = '.showImgEdit .img-preview'
  66. ,file = $(".showImgEdit input[name='file']")
  67. , options = {aspectRatio: mark,preview: preview,viewMode:1};
  68. $(elem).on('click',function () {
  69. layer.open({
  70. type: 1
  71. , content: content
  72. , area: area
  73. , success: function () {
  74. image.cropper(options);
  75. }
  76. , cancel: function (index) {
  77. layer.close(index);
  78. image.cropper('destroy');
  79. }
  80. });
  81. });
  82. $(".layui-btn").on('click',function () {
  83. var event = $(this).attr("cropper-event");
  84. //监听确认保存图像
  85. if(event === 'confirmSave'){
  86. image.cropper("getCroppedCanvas",{
  87. width: saveW,
  88. height: saveH
  89. }).toBlob(function(blob){
  90. var formData=new FormData();
  91. formData.append('file',blob,'head.jpg');
  92. $.ajax({
  93. method:"post",
  94. url: url, //用于文件上传的服务器端请求地址
  95. data: formData,
  96. processData: false,
  97. contentType: false,
  98. success: function (result) {
  99. layer.closeAll('page');
  100. return done(result);
  101. //if(result.code == 0){
  102. // layer.msg(result.msg,{icon: 1});
  103. // layer.closeAll('page');
  104. // return done(result.data.src);
  105. //}else if(result.code == -1){
  106. // layer.alert(result.msg,{icon: 2});
  107. //}
  108. }
  109. });
  110. });
  111. //监听旋转
  112. }else if(event === 'rotate'){
  113. var option = $(this).attr('data-option');
  114. image.cropper('rotate', option);
  115. //重设图片
  116. }else if(event === 'reset'){
  117. image.cropper('reset');
  118. }
  119. //文件选择
  120. file.change(function () {
  121. var r= new FileReader();
  122. var f=this.files[0];
  123. r.readAsDataURL(f);
  124. r.onload=function (e) {
  125. image.cropper('destroy').attr('src', this.result).cropper(options);
  126. };
  127. });
  128. });
  129. }
  130. };
  131. exports('croppers', obj);
  132. });