photo_05.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Photo</title>
  5. <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
  6. <link rel="stylesheet" href="./css/cropper.min.css" type="text/css" />
  7. <style>
  8. body{
  9. margin: 0px;
  10. background-color:rgb(240,240,240);
  11. }
  12. .croper-box {
  13. width: 560px;
  14. height: 300px;
  15. overflow: hidden;
  16. float:left;
  17. }
  18. .img-preview {
  19. margin-left: 5px;
  20. width: 150px;
  21. height: 150px;
  22. overflow: hidden;
  23. float:left;
  24. }
  25. .btn-preview {
  26. margin-left: 5px;
  27. margin-top:40px;
  28. width: 150px;
  29. height: 150px;
  30. overflow: hidden;
  31. float:left;
  32. }
  33. .btn-preview input{
  34. margin-top:5px;
  35. width:120px;
  36. height:33px;
  37. color:#FFF;
  38. background:#09A3C8;
  39. border: 1px solid #09A3C8;
  40. border-radius: 3px;
  41. }
  42. </style>
  43. <script src="./js/jquery.min.js"></script>
  44. <script src="./js/cropper.min.js"></script>
  45. <script type="text/javascript">
  46. function initCropper(image, input){
  47. var $image = image;
  48. var $inputImage = input;
  49. var options = {
  50. aspectRatio: 1, // 纵横比
  51. viewMode: 2,
  52. preview: '.img-preview', // 预览图的class名
  53. crop: function(event) {
  54. event.src = $('#photoPath').val();
  55. //console.log("event="+JSON.stringify(event));
  56. $('#detail').val(JSON.stringify(event));
  57. }
  58. };
  59. $image.cropper(options);
  60. var uploadedImageURL;
  61. var URL = window.URL || window.webkitURL;
  62. if (URL) {
  63. // 给input添加监听
  64. $inputImage.change(function () {
  65. var files = this.files;
  66. var file;
  67. if (!$image.data('cropper')) {
  68. return;
  69. }
  70. if (files && files.length) {
  71. file = files[0];
  72. // 判断是否是图像文件
  73. if (/^image\/\w+$/.test(file.type)) {
  74. // 如果URL已存在就先释放
  75. if (uploadedImageURL) {
  76. URL.revokeObjectURL(uploadedImageURL);
  77. }
  78. uploadedImageURL = URL.createObjectURL(file);
  79. // 销毁cropper后更改src属性再重新创建cropper
  80. $image.cropper('destroy').attr('src', uploadedImageURL).cropper(options);
  81. $inputImage.val('');
  82. } else {
  83. window.alert('请选择一个图像文件!');
  84. }
  85. }
  86. });
  87. } else {
  88. $inputImage.prop('disabled', true).addClass('disabled');
  89. }
  90. }
  91. function changePhoto(){
  92. let photoPath = window.changeImage();
  93. $('#photoPath').val(photoPath);
  94. // 更换cropper的图片
  95. $('#photo').cropper('replace', photoPath, false);
  96. }
  97. function savePhoto(){
  98. console.log($('#detail').val());
  99. window.saveImage($('#detail').val());
  100. }
  101. function defaultPhoto(){
  102. window.saveImage('');
  103. }
  104. function setPhotoPath(data){
  105. $('#photoPath').val(data);
  106. $('#photo').attr('src', data);
  107. initCropper($('#photo'),$('#inputImage'));
  108. }
  109. </script>
  110. </head>
  111. <body>
  112. <div style="width: 760px">
  113. <div class="croper-box">
  114. <img id="photo" src="" class="wait-crop croper-box">
  115. <input id="photoPath" type="hidden" value="">
  116. </div>
  117. <div>
  118. <div class="img-preview"></div>
  119. <input id="detail" type="hidden" value="">
  120. <div class="btn-preview">
  121. <input type="button" style="" class="btn btn-default" onclick="changePhoto()" value="Change" />
  122. <input type="button" class="btn btn-default" onclick="savePhoto();" value="Save" />
  123. <input type="button" class="btn btn-default" onclick="defaultPhoto();" value="Default" />
  124. <input type="file" id="inputImage" accept=".png,.jpg" style="display:none;">
  125. </div>
  126. </div>
  127. </div>
  128. </body>
  129. </html>