main.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. window.onload = function () {
  2. 'use strict';
  3. var Cropper = window.Cropper;
  4. var URL = window.URL || window.webkitURL;
  5. var container = document.querySelector('.img-container');
  6. var image = container.getElementsByTagName('img').item(0);
  7. var download = document.getElementById('download');
  8. var actions = document.getElementById('actions');
  9. var dataX = document.getElementById('dataX');
  10. var dataY = document.getElementById('dataY');
  11. var dataHeight = document.getElementById('dataHeight');
  12. var dataWidth = document.getElementById('dataWidth');
  13. var dataRotate = document.getElementById('dataRotate');
  14. var dataScaleX = document.getElementById('dataScaleX');
  15. var dataScaleY = document.getElementById('dataScaleY');
  16. var options = {
  17. aspectRatio: 16 / 9,
  18. preview: '.img-preview',
  19. ready: function (e) {
  20. //console.log(e.type);
  21. },
  22. cropstart: function (e) {
  23. //console.log(e.type, e.detail.action);
  24. },
  25. cropmove: function (e) {
  26. //console.log(e.type, e.detail.action);
  27. },
  28. cropend: function (e) {
  29. //console.log(e.type, e.detail.action);
  30. },
  31. crop: function (e) {
  32. var data = e.detail;
  33. //console.log(e.type);
  34. dataX.value = Math.round(data.x);
  35. dataY.value = Math.round(data.y);
  36. dataHeight.value = Math.round(data.height);
  37. dataWidth.value = Math.round(data.width);
  38. dataRotate.value = typeof data.rotate !== 'undefined' ? data.rotate : '';
  39. dataScaleX.value = typeof data.scaleX !== 'undefined' ? data.scaleX : '';
  40. dataScaleY.value = typeof data.scaleY !== 'undefined' ? data.scaleY : '';
  41. },
  42. zoom: function (e) {
  43. //console.log(e.type, e.detail.ratio);
  44. }
  45. };
  46. var cropper = new Cropper(image, options);
  47. var originalImageURL = image.src;
  48. var uploadedImageType = 'image/jpeg';
  49. var uploadedImageName = 'cropped.jpg';
  50. var uploadedImageURL;
  51. // Tooltip
  52. $('[data-toggle="tooltip"]').tooltip();
  53. // Buttons
  54. if (!document.createElement('canvas').getContext) {
  55. $('button[data-method="getCroppedCanvas"]').prop('disabled', true);
  56. }
  57. if (typeof document.createElement('cropper').style.transition === 'undefined') {
  58. $('button[data-method="rotate"]').prop('disabled', true);
  59. $('button[data-method="scale"]').prop('disabled', true);
  60. }
  61. // Download
  62. if (typeof download.download === 'undefined') {
  63. download.className += ' disabled';
  64. download.title = 'Your browser does not support download';
  65. }
  66. function aspectRatio(value)
  67. {
  68. if (!cropper) {
  69. return;
  70. }
  71. options.aspectRatio = value;
  72. // Restart
  73. cropper.destroy();
  74. cropper = new Cropper(image, options);
  75. }
  76. // Methods
  77. actions.querySelector('.docs-buttons').onclick = function (event) {
  78. var e = event || window.event;
  79. var target = e.target || e.srcElement;
  80. var cropped;
  81. var result;
  82. var input;
  83. var data;
  84. if (!cropper) {
  85. return;
  86. }
  87. while (target !== this) {
  88. if (target.getAttribute('data-method')) {
  89. break;
  90. }
  91. target = target.parentNode;
  92. }
  93. if (target === this || target.disabled || target.className.indexOf('disabled') > -1) {
  94. return;
  95. }
  96. data = {
  97. method: target.getAttribute('data-method'),
  98. value: target.getAttribute('data-value'),
  99. target: target.getAttribute('data-target'),
  100. option: target.getAttribute('data-option') || undefined,
  101. secondOption: target.getAttribute('data-second-option') || undefined
  102. };
  103. cropped = cropper.cropped;
  104. if (data.method) {
  105. if (data.method == 'save') {
  106. var option = {};
  107. option.param_x = dataX.value;
  108. option.param_y = dataY.value;
  109. option.param_h = dataHeight.value;
  110. option.param_w = dataWidth.value;
  111. option.param_r = dataRotate.value;
  112. option.param_sx = dataScaleX.value;
  113. option.param_sy = dataScaleY.value;
  114. option.pic = $('.img-container img').attr('src');
  115. var cas = cropper.getCroppedCanvas({ maxWidth: 4096, maxHeight: 4096 })
  116. option.img = cas.toDataURL('image/jpeg');//生成base64图片的格式
  117. /*
  118. cas.toBlob(function (e) {
  119. console.log(e); //生成Blob的图片格式
  120. })
  121. */
  122. var url = config.host + 'save.cropper&json=1';
  123. $.post(url, option, function(t)
  124. {
  125. var t = eval('(' + t + ')');
  126. var pic = t.data;
  127. window.parent.picSet(pic);
  128. })
  129. return;
  130. } else if (typeof data.target !== 'undefined') {
  131. input = document.querySelector(data.target);
  132. if (!target.hasAttribute('data-option') && data.target && input) {
  133. try {
  134. data.option = JSON.parse(input.value);
  135. } catch (e) {
  136. console.log(e.message);
  137. }
  138. }
  139. }
  140. switch (data.method) {
  141. case 'rotate':
  142. if (cropped && options.viewMode > 0) {
  143. cropper.clear();
  144. }
  145. break;
  146. case 'getCroppedCanvas':
  147. try {
  148. data.option = JSON.parse(data.option);
  149. } catch (e) {
  150. console.log(e.message);
  151. }
  152. if (uploadedImageType === 'image/jpeg') {
  153. if (!data.option) {
  154. data.option = {};
  155. }
  156. data.option.fillColor = '#fff';
  157. }
  158. break;
  159. }
  160. if (data.method != 'aspectRatio') {
  161. result = cropper[data.method](data.option, data.secondOption);
  162. }
  163. switch (data.method) {
  164. case 'aspectRatio':
  165. cropper.destroy();
  166. var tmp = data.value.split(':');
  167. options.aspectRatio = tmp[0]/tmp[1];
  168. cropper = new Cropper(image, options);
  169. break;
  170. case 'rotate':
  171. if (cropped && options.viewMode > 0) {
  172. cropper.crop();
  173. }
  174. break;
  175. case 'scaleX':
  176. case 'scaleY':
  177. target.setAttribute('data-option', -data.option);
  178. break;
  179. case 'getCroppedCanvas':
  180. if (result) {
  181. // Bootstrap's Modal
  182. $('#getCroppedCanvasModal').modal().find('.modal-body').html(result);
  183. if (!download.disabled) {
  184. download.download = uploadedImageName;
  185. download.href = result.toDataURL(uploadedImageType);
  186. }
  187. }
  188. break;
  189. case 'destroy':
  190. cropper = null;
  191. if (uploadedImageURL) {
  192. URL.revokeObjectURL(uploadedImageURL);
  193. uploadedImageURL = '';
  194. image.src = originalImageURL;
  195. }
  196. break;
  197. }
  198. if (typeof result === 'object' && result !== cropper && input) {
  199. try {
  200. input.value = JSON.stringify(result);
  201. } catch (e) {
  202. console.log(e.message);
  203. }
  204. }
  205. }
  206. };
  207. document.body.onkeydown = function (event) {
  208. var e = event || window.event;
  209. if (e.target !== this || !cropper || this.scrollTop > 300) {
  210. return;
  211. }
  212. switch (e.keyCode) {
  213. case 37:
  214. e.preventDefault();
  215. cropper.move(-1, 0);
  216. break;
  217. case 38:
  218. e.preventDefault();
  219. cropper.move(0, -1);
  220. break;
  221. case 39:
  222. e.preventDefault();
  223. cropper.move(1, 0);
  224. break;
  225. case 40:
  226. e.preventDefault();
  227. cropper.move(0, 1);
  228. break;
  229. }
  230. };
  231. // Import image
  232. var inputImage = document.getElementById('inputImage');
  233. if (URL) {
  234. inputImage.onchange = function () {
  235. var files = this.files;
  236. var file;
  237. if (cropper && files && files.length) {
  238. file = files[0];
  239. if (/^image\/\w+/.test(file.type)) {
  240. uploadedImageType = file.type;
  241. uploadedImageName = file.name;
  242. if (uploadedImageURL) {
  243. URL.revokeObjectURL(uploadedImageURL);
  244. }
  245. image.src = uploadedImageURL = URL.createObjectURL(file);
  246. cropper.destroy();
  247. cropper = new Cropper(image, options);
  248. inputImage.value = null;
  249. } else {
  250. window.alert('Please choose an image file.');
  251. }
  252. }
  253. };
  254. } else {
  255. inputImage.disabled = true;
  256. inputImage.parentNode.className += ' disabled';
  257. }
  258. };