uploadcrop.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * Uses cropper javascript widget from https://github.com/fengyuanchen/cropper
  3. *
  4. * Author: Joseba Juaniz
  5. * Year: 2015
  6. */
  7. $.fn.uploadCrop = function (options) {
  8. var inputId = options['inputField'];
  9. var _jcropOptions = options['jcropOptions'];
  10. var sizemax = options['sizemax'];
  11. var formId = options['formId'];
  12. var uploadField = $(this);
  13. var parent = $(this).parents('.uploadcrop');
  14. var divImageContainer = $(parent).find('div#image-source');
  15. var divModelBody = $(parent).find('div.modal-body');
  16. var imageField = $(divImageContainer).find('img').prop('outerHTML');
  17. var imageID = $(divImageContainer).find('img').attr('id');
  18. var x_hidden = $('#' + inputId + '-x');
  19. var width_hidden = $('#' + inputId + '-width');
  20. var y_hidden = $('#' + inputId + '-y');
  21. var height_hidden = $('#' + inputId + '-height');
  22. var modal = $(parent).find('div.modal');
  23. var $pcimg = $(parent).find('#preview-pane div.preview-container img.preview_image');
  24. // INITIALIZATIONS
  25. $pcimg.css('maxWidth', sizemax + 'px').css('maxHeight', sizemax + 'px').hide();
  26. uploadField.change(function (e) {
  27. $(".spinner").show();
  28. var file = e.target.files[0],
  29. imageType = /image.*/;
  30. if (!file.type.match(imageType))
  31. return;
  32. var reader = new FileReader();
  33. reader.onload = fileOnload;
  34. reader.readAsDataURL(file);
  35. });
  36. function fileOnload(e) {
  37. // basic initializations
  38. $(divImageContainer).html('').append(imageField);
  39. $('<div id="slider"></div>').appendTo(divModelBody);
  40. $('#' + imageID).prop('src', e.target.result.toString()).hide();
  41. $(x_hidden).prop('disabled', false);
  42. $(width_hidden).prop('disabled', false);
  43. $(y_hidden).prop('disabled', false);
  44. $(height_hidden).prop('disabled', false);
  45. $('#' + imageID + '_button_accept').off('click');
  46. $('#' + imageID + '_button_cancel').off('click');
  47. $(modal).off('shown.bs.modal');
  48. // get crop data and put it in the preview image
  49. $('#' + imageID + '_button_accept').on('click', function () {
  50. // if there is no selected area, then all the image will be uploaded
  51. // with its real size as cropping data
  52. var croppedImage = $('#' + imageID).cropper('getDataURL');
  53. if (croppedImage == '')
  54. {
  55. var sizes = $('#' + imageID).cropper("getImageData");
  56. // we will crop by the size of the image, so no real cropping will be made
  57. $(x_hidden).val('0');
  58. $(width_hidden).val(sizes.naturalWidth);
  59. $(y_hidden).val('0');
  60. $(height_hidden).val(sizes.naturalHeight);
  61. // we will put all the image as preview
  62. $pcimg.prop('src', $('#' + imageID).prop('src')).show();
  63. }
  64. else
  65. {
  66. croppedImage = $('#' + imageID).cropper('getCroppedCanvas');
  67. // console.log(croppedImage.toBlob('blob'));
  68. croppedImage.toBlob(function(blob) {
  69. var newImg = document.createElement('img'),
  70. url = URL.createObjectURL(blob);
  71. // $(newImg).css('display','none');
  72. $(newImg).attr('id','himg');
  73. newImg.onload = function() {
  74. URL.revokeObjectURL(url);
  75. };
  76. console.log(url);
  77. // $pcimg.prop('src') = url;
  78. newImg.src = url;
  79. // document.body.appendChild(newImg);
  80. $pcimg.prop('src', url).show();
  81. }, 'image/jpeg', 0.95);
  82. // $pcimg.prop('src', $('#himg').prop('src')).show();
  83. //$pcimg.prop('src', croppedImage).show();
  84. }
  85. // $('#' +formId).submit();
  86. // window.document.data_crop = $(this).cropper;
  87. $(divImageContainer).html('');
  88. $(modal).modal('hide');
  89. });
  90. // if cancel, the image won't be cropped
  91. $('#' + imageID + '_button_cancel').on('click', function () {
  92. // reset all the values
  93. $(uploadField).val('');
  94. $pcimg.hide();
  95. $(divImageContainer).html('');
  96. $(x_hidden).val('');
  97. $(width_hidden).val('');
  98. $(y_hidden).val('');
  99. $(height_hidden).val('');
  100. $(x_hidden).prop('disabled', true);
  101. $(width_hidden).prop('disabled', true);
  102. $(y_hidden).prop('disabled', true);
  103. $(height_hidden).prop('disabled', true);
  104. $(modal).modal('hide');
  105. });
  106. // this wil be launched on modal shown in order to get real widths from the elements
  107. $(modal).on('shown.bs.modal', function (a) {
  108. // change the image container size because otherwise the image will
  109. // grow to fit in its parent and we want smaller images in case they are
  110. // too big, not oversized versions of them
  111. if ($(divImageContainer).width() > $('#' + imageID).width()) {
  112. $(divImageContainer).css({
  113. width: $('#' + imageID).width() + 'px'
  114. });
  115. }
  116. // sets the data when cropbox is made, cropbox is draggin, zooming
  117. _jcropOptions['crop'] = function () {
  118. var crop = $(this).cropper("getData");
  119. $(x_hidden).val(crop.x);
  120. $(width_hidden).val(crop.width);
  121. $(y_hidden).val(crop.y);
  122. $(height_hidden).val(crop.height);
  123. };
  124. _jcropOptions['cropend'] = function () {
  125. var crop = $(this).cropper("getData");
  126. $(x_hidden).val(crop.x);
  127. $(width_hidden).val(crop.width);
  128. $(y_hidden).val(crop.y);
  129. $(height_hidden).val(crop.height);
  130. };
  131. _jcropOptions['zoom'] = function () {
  132. var crop = $(this).cropper("getData");
  133. $(x_hidden).val(crop.x);
  134. $(width_hidden).val(crop.width);
  135. $(y_hidden).val(crop.y);
  136. $(height_hidden).val(crop.height);
  137. };
  138. // don't select anything from start, it's ugly
  139. _jcropOptions['built'] = function()
  140. {
  141. $('#' + imageID).cropper('reset');
  142. // important in case the image is bigger than the window size
  143. $(modal).modal();
  144. };
  145. // start cropper itself
  146. $('#' + imageID).cropper(_jcropOptions);
  147. //zoom in button
  148. $( "#zoom-in" ).click(function() {
  149. console.log('zoom-in');
  150. $('#' + imageID).cropper('zoom', 0.1);
  151. });
  152. //zoom out button
  153. $( "#zoom-out" ).click(function() {
  154. console.log('zoom-out');
  155. $('#' + imageID).cropper('zoom', -0.1);
  156. });
  157. $(".spinner").hide();
  158. });
  159. // Zhu li, do the thing!
  160. $(modal).modal('show');
  161. }
  162. }