Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. // input specification to method post
  2. // Recommended: add inside ~style="display:none;" to hide it.
  3. <input type="file" class="form-control img_location" id="img_location" accept="image/*">
  4.  
  5. // replaced file upload method to camera-icon.png
  6. <img src="(MyFoderHere)/camera-icon.png" id="clickToUpload" style="cursor:pointer" width="32px" />
  7.  
  8. // below is the function triggered
  9. <script>
  10. $(document).ready(function(e) {
  11. $(".showonhover").click(function(){
  12. $("#selectfile").trigger('click');
  13. });
  14. });
  15.  
  16. var input = document.querySelector('input[type=file]'); // see Example 4
  17.  
  18. input.onchange = function () {
  19. var file = input.files[0];
  20.  
  21. drawOnCanvas(file); // see Example 6
  22. displayAsImage(file); // see Example 7
  23. };
  24.  
  25. function drawOnCanvas(file) {
  26. var reader = new FileReader();
  27.  
  28. reader.onload = function (e) {
  29. var dataURL = e.target.result,
  30. c = document.querySelector('canvas'), // see Example 4
  31. ctx = c.getContext('2d'),
  32. img = new Image();
  33.  
  34. img.onload = function() {
  35. c.width = img.width;
  36. c.height = img.height;
  37. ctx.drawImage(img, 0, 0);
  38. };
  39.  
  40. img.src = dataURL;
  41. };
  42.  
  43. reader.readAsDataURL(file);
  44. }
  45.  
  46. function displayAsImage(file) {
  47. var imgURL = URL.createObjectURL(file),
  48. img = document.createElement('img');
  49.  
  50. img.onload = function() {
  51. URL.revokeObjectURL(imgURL);
  52. };
  53.  
  54. img.src = imgURL;
  55. document.body.appendChild(img);
  56. }
  57.  
  58. $("#clickToUpload1").click(function () {
  59. $("#img_location").trigger('click');
  60. });
  61. /*
  62. $("#clickToUpload2").click(function () {
  63. $("#file2").trigger('click');
  64. });
  65. $("#clickToUpload2").click(function () {
  66. $("#file3").trigger('click');
  67. });
  68. */
  69. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement