Artixiban commited on
Commit
ac3fe44
1 Parent(s): 16178ba

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +55 -18
index.html CHANGED
@@ -300,22 +300,11 @@ label[for="simple"], label[for="complex"] {
300
  <button id="downloadButton" style="display: block ;" onclick="downloadResultImage()">Download </button>
301
 
302
  <script>
303
- function previewImage(event) {
304
- const file = event.target.files[0];
305
- const imagePreview = document.getElementById('sourceImageContainer');
306
- const uploadButton = document.querySelector('button');
307
-
308
- // Check if file size is larger than 500 MB (500 * 1024 * 1024 bytes)
309
- if (file.size > 4000 * 1024) {
310
- imagePreview.innerHTML = `<p style="color: white;">Please upload an image smaller than 4000 KB.</p>`;
311
- uploadButton.disabled = true; // Disable the button
312
- return;
313
- }
314
-
315
- // If file size is within limit, enable the button and display image preview
316
- imagePreview.innerHTML = `<img src="${URL.createObjectURL(file)}" alt="Uploaded Image" style="max-width: 300px; max-height: 300px;">`;
317
- uploadButton.disabled = false; // Enable the button
318
- }
319
 
320
  async function uploadFile() {
321
  const sourceFileInput = document.getElementById('sourceFile');
@@ -329,9 +318,11 @@ label[for="simple"], label[for="complex"] {
329
 
330
  // Clear the result container
331
  resultContainer.innerHTML = "";
332
-
 
 
333
  const formData = new FormData();
334
- formData.append('file', sourceFileInput.files[0]);
335
  formData.append('version', document.querySelector('input[name="version"]:checked').value);
336
 
337
  try {
@@ -368,7 +359,53 @@ label[for="simple"], label[for="complex"] {
368
  const generateButton = document.querySelector('button');
369
  generateButton.addEventListener('click', uploadFile);
370
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
371
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
372
  function downloadResultImage() {
373
  const resultImage = document.getElementById('resultContainer').querySelector('img');
374
  const link = document.createElement('a');
 
300
  <button id="downloadButton" style="display: block ;" onclick="downloadResultImage()">Download </button>
301
 
302
  <script>
303
+ function previewImage(event) {
304
+ const file = event.target.files[0];
305
+ const imagePreview = document.getElementById('sourceImageContainer');
306
+ imagePreview.innerHTML = `<img src="${URL.createObjectURL(file)}" alt="Uploaded Image" style="max-width: 300px; max-height: 300px;">`;
307
+ }
 
 
 
 
 
 
 
 
 
 
 
308
 
309
  async function uploadFile() {
310
  const sourceFileInput = document.getElementById('sourceFile');
 
318
 
319
  // Clear the result container
320
  resultContainer.innerHTML = "";
321
+ // Compress the image to 500 KB before sending
322
+ const compressedFile = await compressImage(sourceFile.files[0]);
323
+
324
  const formData = new FormData();
325
+ formData.append('file', compressedFile);
326
  formData.append('version', document.querySelector('input[name="version"]:checked').value);
327
 
328
  try {
 
359
  const generateButton = document.querySelector('button');
360
  generateButton.addEventListener('click', uploadFile);
361
 
362
+ async function compressImage(file) {
363
+ return new Promise((resolve, reject) => {
364
+ const reader = new FileReader();
365
+ reader.onload = function(event) {
366
+ const img = new Image();
367
+ img.src = event.target.result;
368
+
369
+ img.onload = function() {
370
+ const canvas = document.createElement('canvas');
371
+ const ctx = canvas.getContext('2d');
372
+
373
+ // Calculate the new dimensions to resize the image while maintaining aspect ratio
374
+ const maxWidth = 1000;
375
+ const maxHeight = 1000;
376
+ let width = img.width;
377
+ let height = img.height;
378
+
379
+ if (width > height) {
380
+ if (width > maxWidth) {
381
+ height *= maxWidth / width;
382
+ width = maxWidth;
383
+ }
384
+ } else {
385
+ if (height > maxHeight) {
386
+ width *= maxHeight / height;
387
+ height = maxHeight;
388
+ }
389
+ }
390
 
391
+ // Set the canvas dimensions
392
+ canvas.width = width;
393
+ canvas.height = height;
394
+
395
+ // Draw the image on the canvas with the new dimensions
396
+ ctx.drawImage(img, 0, 0, width, height);
397
+
398
+ // Convert canvas content to a blob
399
+ canvas.toBlob((blob) => {
400
+ resolve(blob);
401
+ }, 'image/jpeg', 1); // Adjust quality as needed (0.7 is 70% quality)
402
+ }
403
+ }
404
+
405
+ // Read the file as data URL
406
+ reader.readAsDataURL(file);
407
+ });
408
+ }
409
  function downloadResultImage() {
410
  const resultImage = document.getElementById('resultContainer').querySelector('img');
411
  const link = document.createElement('a');