Advertisement
taktikhek

Untitled

Jun 29th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. function dataURItoBlob(dataURI) {
  2. // convert base64/URLEncoded data component to raw binary data held in a string
  3. var byteString;
  4. if (dataURI.split(',')[0].indexOf('base64') >= 0)
  5. byteString = atob(dataURI.split(',')[1]);
  6. else
  7. byteString = unescape(dataURI.split(',')[1]);
  8.  
  9. // separate out the mime component
  10. var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
  11.  
  12. // write the bytes of the string to a typed array
  13. var ia = new Uint8Array(byteString.length);
  14. for (var i = 0; i < byteString.length; i++) {
  15. ia[i] = byteString.charCodeAt(i);
  16. }
  17.  
  18. return new Blob([ia], {type:mimeString});
  19. }
  20.  
  21. window.uploadPicture = function(callback){
  22. croppie.result({
  23. size: "viewport"
  24. }).then(function(dataURI){
  25. var formData = new FormData();
  26. formData.append("design", $("#fg").data("design"));
  27. formData.append("image", dataURItoBlob(dataURI));
  28. $.ajax({
  29. url: "<?=base_url();?>produk/crop",
  30. data: formData,
  31. type: "POST",
  32. contentType: false,
  33. processData: false,
  34. success: callback,
  35. error: function(){
  36. document.getElementById("download").innerHTML = "Download Profile Picture";
  37. },
  38. xhr: function() {
  39. var myXhr = $.ajaxSettings.xhr();
  40. if(myXhr.upload){
  41. myXhr.upload.addEventListener('progress', function(e){
  42. if(e.lengthComputable){
  43. var max = e.total;
  44. var current = e.loaded;
  45.  
  46. var percentage = Math.round((current * 100)/max);
  47. document.getElementById("download").innerHTML = "Uploading... Please Wait... " + percentage + "%";
  48. }
  49. }, false);
  50. }
  51. return myXhr;
  52. },
  53. });
  54. });
  55. }
  56.  
  57. window.updatePreview = function(url) {
  58. document.getElementById("crop-area").innerHTML = "";
  59. window.croppie = new Croppie(document.getElementById("crop-area"), {
  60. "url": url,
  61. boundary: {
  62. height: 500,
  63. width: 500
  64. },
  65. viewport: {
  66. width: 500,
  67. height: 500
  68. },
  69. });
  70.  
  71. $("#fg").on('mouseover touchstart', function(){
  72. document.getElementById("fg").style.zIndex = -1;
  73. });
  74. $(".cr-boundary").on('mouseleave touchend', function(){
  75. document.getElementById("fg").style.zIndex = 10;
  76. });
  77.  
  78. document.getElementById("download").onclick = function(){
  79. this.innerHTML = "Uploading... Please wait...";
  80. uploadPicture(function(r){
  81. document.getElementById("download").innerHTML = "Uploaded";
  82. window.location = "download.php?i=" + r;
  83. });
  84. };
  85. document.getElementById("download").removeAttribute("disabled");
  86. };
  87.  
  88. window.onFileChange = function(input){
  89. if (input.files && input.files[0]) {
  90. var reader = new FileReader();
  91.  
  92. reader.onload = function (e) {
  93. image = new Image();
  94. image.onload = function() {
  95. var width = this.width;
  96. var height = this.height;
  97. if(width >= 500 && height >= 500)
  98. updatePreview(e.target.result);
  99. else
  100. alert("Image should be atleast have 500px width and 500px height");
  101. };
  102. image.src = e.target.result;
  103. }
  104.  
  105. reader.readAsDataURL(input.files[0]);
  106. }
  107. }
  108.  
  109. $(document).ready(function(){
  110. $(".design").on("click", function(){
  111. $("#fg").attr("src", $(this).attr("src")).data("design", $(this).data("design"));
  112. $(".design.active").removeClass("active");
  113. $(this).addClass("active");
  114. });
  115. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement