paste.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <style>
  2. #image-preview {
  3. max-width: 100%;
  4. max-height: 300px;
  5. display: none;
  6. }
  7. </style>
  8. <div class="ui container" style="margin-top: 20px;">
  9. <div class="ui segment" id="paste-area" tabindex="0" style="min-height: 200px; text-align: center;">
  10. <h3>Paste an image here (Ctrl + V)</h3>
  11. <img id="image-preview" alt="Pasted Image" style="margin: 0 auto; display: block;">
  12. <div class="ui divider"></div>
  13. <p id="image_properties">No pasted image</p>
  14. <button class="ui basic button" onclick="uploadImage()" style="margin-top: 20px;">
  15. <i class="blue upload icon"></i> Upload
  16. </button>
  17. </div>
  18. </div>
  19. <script>
  20. var pasteArea = document.getElementById('paste-area');
  21. var imagePreview = document.getElementById('image-preview');
  22. pasteArea.addEventListener('paste', (event) => {
  23. const items = event.clipboardData.items;
  24. for (let item of items) {
  25. if (item.type.startsWith('image/')) {
  26. const file = item.getAsFile();
  27. const reader = new FileReader();
  28. reader.onload = (e) => {
  29. imagePreview.src = e.target.result;
  30. imagePreview.style.display = 'block';
  31. updateImageProperties(); // Update image properties after loading
  32. };
  33. reader.readAsDataURL(file);
  34. break;
  35. }
  36. }
  37. });
  38. function updateImageProperties() {
  39. const imageProperties = document.getElementById('image_properties');
  40. if (imagePreview.src) {
  41. const img = new Image();
  42. img.src = imagePreview.src;
  43. img.onload = () => {
  44. imageProperties.textContent = `Width: ${img.width}px, Height: ${img.height}px`;
  45. };
  46. } else {
  47. imageProperties.textContent = 'No pasted image';
  48. }
  49. }
  50. //Upload to /site/img/{filename}.jpg
  51. function uploadImage() {
  52. const formData = new FormData();
  53. const file = imagePreview.src; // Use the source of the image element
  54. if (file) {
  55. let filename = Date.now() + '.jpg'; // Generate a unique filename based on timestamp
  56. compressImageToJpeg(filename).then((compressedFile) => {
  57. formData.append("file1", compressedFile);
  58. var ajax = new XMLHttpRequest();
  59. ajax.addEventListener("load", function(event) {
  60. let responseText = event.target.responseText;
  61. if (responseText.includes("ok")){
  62. msgbox("Image uploaded successfully!", 3000);
  63. $("#image_properties").text("Image uploaded to /site/img/" + filename);
  64. }
  65. }, false);
  66. ajax.addEventListener("error", function(event) {
  67. alert("Failed to upload image: " + event.target.responseText);
  68. }, false);
  69. ajax.open("POST", "/upload?dir=/site/img");
  70. ajax.send(formData);
  71. }).catch(error => {
  72. console.error('Error compressing image:', error);
  73. alert('Failed to compress image.');
  74. });
  75. } else {
  76. alert('No image to upload!');
  77. }
  78. }
  79. function compressImageToJpeg(filename) {
  80. return new Promise((resolve, reject) => {
  81. const canvas = document.createElement('canvas');
  82. const ctx = canvas.getContext('2d');
  83. const img = new Image();
  84. img.onload = () => {
  85. const maxWidth = 1024; // Set maximum width for compression
  86. const maxHeight = 1024; // Set maximum height for compression
  87. let width = img.width;
  88. let height = img.height;
  89. // Maintain aspect ratio
  90. if (width > maxWidth || height > maxHeight) {
  91. if (width / height > maxWidth / maxHeight) {
  92. height = Math.round((height * maxWidth) / width);
  93. width = maxWidth;
  94. } else {
  95. width = Math.round((width * maxHeight) / height);
  96. height = maxHeight;
  97. }
  98. }
  99. canvas.width = width;
  100. canvas.height = height;
  101. ctx.drawImage(img, 0, 0, width, height);
  102. canvas.toBlob(
  103. (blob) => {
  104. if (blob) {
  105. const compressedFile = new File([blob], filename, {
  106. type: 'image/jpeg',
  107. });
  108. resolve(compressedFile);
  109. } else {
  110. reject(new Error('Compression failed.'));
  111. }
  112. },
  113. 'image/jpeg',
  114. 0.8 // Compression quality (0.0 to 1.0)
  115. );
  116. };
  117. img.onerror = () => reject(new Error('Failed to load image.'));
  118. img.src = imagePreview.src; // Use the source of the image element
  119. });
  120. }
  121. </script>