avatar.html 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <!DOCTYPE html>
  2. <html lang="zh" xmlns:th="http://www.thymeleaf.org" >
  3. <head>
  4. <th:block th:include="include :: header('修改用户头像')" />
  5. <th:block th:include="include :: cropbox-css" />
  6. </head>
  7. <body class="white-bg">
  8. <div class="container">
  9. <div class="imageBox">
  10. <div class="thumbBox"></div>
  11. <div class="spinner" style="display: none">Loading...</div>
  12. </div>
  13. <div class="action">
  14. <div class="new-contentarea tc">
  15. <a href="javascript:void(0)" class="upload-img"> <label for="avatar">上传图像</label> </a>
  16. <input type="file" class="" name="avatar" id="avatar" accept="image/*"/>
  17. </div>
  18. <input type="button" id="btnCrop" class="Btnsty_peyton" value="裁切" />
  19. <input type="button" id="btnZoomIn" class="Btnsty_peyton" value="+" />
  20. <input type="button" id="btnZoomOut" class="Btnsty_peyton" value="-" />
  21. </div>
  22. <div class="cropped"></div>
  23. </div>
  24. <th:block th:include="include :: footer" />
  25. <th:block th:include="include :: cropbox-js" />
  26. <script type="text/javascript">
  27. var cropper;
  28. $(window).load(function() {
  29. var avatar = '[[${user.avatar}]]';
  30. var options = {
  31. thumbBox: '.thumbBox',
  32. spinner: '.spinner',
  33. imgSrc: $.common.isEmpty(avatar) ? ctx + 'img/profile.jpg' : ctx + 'profile/avatar/' + avatar
  34. }
  35. cropper = $('.imageBox').cropbox(options);
  36. $('#avatar').on('change', function() {
  37. var reader = new FileReader();
  38. reader.onload = function(e) {
  39. options.imgSrc = e.target.result;
  40. //根据MIME判断上传的文件是不是图片类型
  41. if((options.imgSrc).indexOf("image/")==-1){
  42. $.modal.alertWarning("文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。");
  43. } else {
  44. cropper = $('.imageBox').cropbox(options);
  45. }
  46. }
  47. reader.readAsDataURL(this.files[0]);
  48. })
  49. $('#btnCrop').on('click', function(){
  50. var img = cropper.getDataURL();
  51. $('.cropped').html('');
  52. $('.cropped').append('<img src="'+img+'" align="absmiddle" style="width:64px;margin-top:4px;border-radius:64px;box-shadow:0px 0px 12px #7E7E7E;" ><p>64px*64px</p>');
  53. $('.cropped').append('<img src="'+img+'" align="absmiddle" style="width:128px;margin-top:4px;border-radius:128px;box-shadow:0px 0px 12px #7E7E7E;"><p>128px*128px</p>');
  54. $('.cropped').append('<img src="'+img+'" align="absmiddle" style="width:180px;margin-top:4px;border-radius:180px;box-shadow:0px 0px 12px #7E7E7E;"><p>180px*180px</p>');
  55. })
  56. $('#btnZoomIn').on('click', function(){
  57. cropper.zoomIn();
  58. })
  59. $('#btnZoomOut').on('click', function(){
  60. cropper.zoomOut();
  61. })
  62. });
  63. function submitHandler() {
  64. var img = cropper.getBlob();
  65. var formdata = new FormData();
  66. formdata.append("avatarfile", img);
  67. $.ajax({
  68. url: ctx + "system/user/profile/updateAvatar",
  69. data: formdata,
  70. type: "post",
  71. processData: false,
  72. contentType: false,
  73. success: function(result) {
  74. $.operate.saveSuccess(result);
  75. }
  76. })
  77. }
  78. </script>
  79. </body>
  80. </html>