Advertisement
taktikhek

Untitled

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