baseEdit.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. {% load staticfiles %}
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>基于cropper.js的图片裁剪</title>
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  8. <meta name="format-detection" content="telephone=no">
  9. <link rel="stylesheet" href="{% static '/lib/css/cropper.css' %}">
  10. <link rel="stylesheet" href="{% static '/css/ImgCropping.css' %}">
  11. <style>
  12. .baseedit>div {
  13. margin-top: 20px;
  14. }
  15. .titleTxt {
  16. color: #626262;
  17. display: inline-block;
  18. width: 200px;
  19. text-align: right;
  20. vertical-align: top;
  21. }
  22. .baseContent {
  23. display: inline-block;
  24. }
  25. #baseName {
  26. border-radius: 0!important;
  27. color: #858585;
  28. background-color: #FFF;
  29. border: 1px solid #D5D5D5;
  30. padding: 5px 4px 6px;
  31. font-size: 14px;
  32. font-family: inherit;
  33. width: 220px;
  34. }
  35. #finalImg {
  36. width: 100%;
  37. }
  38. .cancel {
  39. background: #ABBAC3;
  40. color: #fff;
  41. border: solid 1px #ABBAC3;
  42. }
  43. .cancel:hover {
  44. background: #d4d9db;
  45. color: #fff;
  46. border: solid 1px #d4d9db;
  47. }
  48. #hint {
  49. display: none;
  50. position: fixed;
  51. left: 50%;
  52. top: 50%;
  53. background: #b2b2b2;
  54. padding: 20px;
  55. border-radius: 5px;
  56. color: #fff;
  57. transform: translateX(-50%) translateY(-50%);
  58. }
  59. </style>
  60. </head>
  61. <body>
  62. <div class="baseedit">
  63. <div>
  64. <label class="titleTxt">基地名称:</label>
  65. <div class="baseContent">
  66. <input id="baseName" type="text" value="{{base_name|default_if_none:" "}}">
  67. </div>
  68. </div>
  69. <div>
  70. <label class="titleTxt">基地图片:</label>
  71. <div class="baseContent">
  72. <button id="replaceImg" class="l-btn">更换图片</button>
  73. <div style="padding: 5px;margin-top: 10px;width: 230px;height: 165px;border: 1px solid #D5D5D5;">
  74. <img id="finalImg" src="{{base_photo}}">
  75. </div>
  76. </div>
  77. </div>
  78. <div>
  79. <label class="titleTxt"> </label>
  80. <div class="baseContent">
  81. <button id="replaceImg" class="l-btn" onclick="savemsg()">保存</button>
  82. <button id="replaceImg" class="l-btn cancel" onclick="cancel()">取消</button>
  83. </div>
  84. </div>
  85. </div>
  86. <div style="display: none" class="tailoring-container">
  87. <div class="black-cloth" onclick="closeTailor(this)"></div>
  88. <div class="tailoring-content">
  89. <div class="tailoring-content-one">
  90. <label title="上传图片" for="chooseImg" class="l-btn choose-btn">
  91. <input type="file" accept="image/jpg,image/jpeg,image/png" name="file" id="chooseImg" class="hidden" onchange="selectImg(this)"> 选择图片
  92. </label>
  93. <div class="close-tailoring" onclick="closeTailor(this)">×</div>
  94. </div>
  95. <div class="tailoring-content-two">
  96. <div class="tailoring-box-parcel">
  97. <img id="tailoringImg">
  98. </div>
  99. <div class="preview-box-parcel">
  100. <p>图片预览:</p>
  101. <div class="square previewImg"></div>
  102. <!-- <div class="circular previewImg"></div> -->
  103. </div>
  104. </div>
  105. <div class="tailoring-content-three">
  106. <button class="l-btn cropper-reset-btn">复位</button>
  107. <button class="l-btn cropper-rotate-btn">旋转</button>
  108. <button class="l-btn cropper-scaleX-btn">换向</button>
  109. <button class="l-btn sureCut" id="sureCut">确定</button>
  110. </div>
  111. </div>
  112. </div>
  113. <div id="hint"></div>
  114. <script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script>
  115. <script src="{% static '/lib/js/cropper.js' %}" type="text/javascript" charset="utf-8"></script>
  116. <script>
  117. var oldfinalImg = $('#finalImg').attr('src');
  118. var oldbaseName = $('#baseName').val();
  119. //弹出框水平垂直居中
  120. (window.onresize = function () {
  121. var win_height = $(window).height();
  122. var win_width = $(window).width();
  123. if (win_width <= 768) {
  124. $(".tailoring-content").css({
  125. "top": (win_height - $(".tailoring-content").outerHeight()) / 2,
  126. "left": 0
  127. });
  128. } else {
  129. $(".tailoring-content").css({
  130. "top": (win_height - $(".tailoring-content").outerHeight()) / 2,
  131. "left": (win_width - $(".tailoring-content").outerWidth()) / 2
  132. });
  133. }
  134. })();
  135. //弹出图片裁剪框
  136. $("#replaceImg").on("click", function () {
  137. $(".tailoring-container").toggle();
  138. });
  139. //图像上传
  140. function selectImg(file) {
  141. if (!file.files || !file.files[0]) {
  142. return;
  143. }
  144. var reader = new FileReader();
  145. reader.onload = function (evt) {
  146. var replaceSrc = evt.target.result;
  147. //更换cropper的图片
  148. $('#tailoringImg').cropper('replace', replaceSrc, false);//默认false,适应高度,不失真
  149. }
  150. reader.readAsDataURL(file.files[0]);
  151. }
  152. //cropper图片裁剪
  153. $('#tailoringImg').cropper({
  154. aspectRatio: 1221 / 874,//默认比例
  155. viewMode: 1,
  156. preview: '.previewImg',//预览视图
  157. guides: false, //裁剪框的虚线(九宫格)
  158. autoCropArea: 0.5, //0-1之间的数值,定义自动剪裁区域的大小,默认0.8
  159. movable: false, //是否允许移动图片
  160. dragCrop: true, //是否允许移除当前的剪裁框,并通过拖动来新建一个剪裁框区域
  161. movable: true, //是否允许移动剪裁框
  162. resizable: true, //是否允许改变裁剪框的大小
  163. zoomable: false, //是否允许缩放图片大小
  164. mouseWheelZoom: false, //是否允许通过鼠标滚轮来缩放图片
  165. touchDragZoom: true, //是否允许通过触摸移动来缩放图片
  166. rotatable: true, //是否允许旋转图片
  167. crop: function (e) {
  168. // 输出结果数据裁剪图像。
  169. }
  170. });
  171. //旋转
  172. $(".cropper-rotate-btn").on("click", function () {
  173. $('#tailoringImg').cropper("rotate", 45);
  174. });
  175. //复位
  176. $(".cropper-reset-btn").on("click", function () {
  177. $('#tailoringImg').cropper("reset");
  178. });
  179. //换向
  180. var flagX = true;
  181. $(".cropper-scaleX-btn").on("click", function () {
  182. if (flagX) {
  183. $('#tailoringImg').cropper("scaleX", -1);
  184. flagX = false;
  185. } else {
  186. $('#tailoringImg').cropper("scaleX", 1);
  187. flagX = true;
  188. }
  189. flagX != flagX;
  190. });
  191. //裁剪后的处理
  192. $("#sureCut").on("click", function () {
  193. if ($("#tailoringImg").attr("src") == null) {
  194. return false;
  195. } else {
  196. var cas = $('#tailoringImg').cropper('getCroppedCanvas');//获取被裁剪后的canvas
  197. var base64url = cas.toDataURL('image/png'); //转换为base64地址形式
  198. $("#finalImg").prop("src", base64url);//显示为图片的形式
  199. //关闭裁剪框
  200. closeTailor();
  201. }
  202. });
  203. //关闭裁剪框
  204. function closeTailor() {
  205. $(".tailoring-container").toggle();
  206. }
  207. function savemsg() {
  208. var baseName = $('#baseName').val();
  209. var finalImg = $('#finalImg').attr('src');
  210. if((finalImg == oldfinalImg) && (baseName == oldbaseName)){
  211. $('#hint').html('暂未更改');
  212. $('#hint').show();
  213. $('#hint').fadeOut(2000);
  214. return false;
  215. }
  216. if (baseName == '') {
  217. $('#hint').html('请填写基地名称');
  218. $('#hint').show();
  219. $('#hint').fadeOut(2000);
  220. return false;
  221. }
  222. if((finalImg == oldfinalImg) && (baseName != oldbaseName)){
  223. var formFile = new FormData();
  224. formFile.append("basename", baseName);
  225. }
  226. if(finalImg != oldfinalImg){
  227. var fileObj = dataURLtoFile(finalImg, 'filename')
  228. var formFile = new FormData();
  229. formFile.append("basephoto", fileObj);
  230. formFile.append("basename", baseName);
  231. }
  232. var data = formFile;
  233. $.ajax({
  234. url: "user_base_save",
  235. data: data,
  236. type: "Post",
  237. dataType: "json",
  238. cache: false,//上传文件无需缓存
  239. processData: false,//用于对data参数进行序列化处理 这里必须false
  240. contentType: false, //必须
  241. success: function (result) {
  242. $('#hint').html('更换成功');
  243. $('#hint').show();
  244. $('#hint').fadeOut(2000)
  245. },
  246. })
  247. }
  248. function dataURLtoFile(dataurl, filename) {
  249. var arr = dataurl.split(',');
  250. var mime = arr[0].match(/:(.*?);/)[1];
  251. var bstr = atob(arr[1]);
  252. var n = bstr.length;
  253. var u8arr = new Uint8Array(n);
  254. while (n--) {
  255. u8arr[n] = bstr.charCodeAt(n);
  256. }
  257. //转换成file对象
  258. return new File([u8arr], filename, { type: mime });
  259. //转换成成blob对象
  260. //return new Blob([u8arr],{type:mime});
  261. }
  262. function cancel(){
  263. $('#baseName').val(oldbaseName);
  264. $('#finalImg').attr('src',oldfinalImg);
  265. }
  266. </script>
  267. </body>
  268. </html>