Tu Nombre commited on
Commit
e89f413
·
1 Parent(s): f0924f1

comprimir imagenes antes de subir

Browse files
Files changed (1) hide show
  1. main.py +25 -5
main.py CHANGED
@@ -586,13 +586,33 @@ async function activar(){
586
  const files = Array.from(fileInput.files).slice(0, 20);
587
  for (const f of files) {
588
  try {
 
589
  const b64 = await new Promise((resolve, reject) => {
590
- const reader = new FileReader();
591
- reader.onload = () => resolve(reader.result.split(',')[1]);
592
- reader.onerror = reject;
593
- reader.readAsDataURL(f);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
594
  });
595
- archivos.push({nombre: f.name, contenido: b64});
596
  } catch(e) { console.log("Error archivo", f.name, e); }
597
  }
598
  document.getElementById('mediainfo1').textContent = archivos.length + " archivos listos";
 
586
  const files = Array.from(fileInput.files).slice(0, 20);
587
  for (const f of files) {
588
  try {
589
+ // Comprimir imagen antes de subir
590
  const b64 = await new Promise((resolve, reject) => {
591
+ if (!f.type.startsWith('image/')) {
592
+ const r = new FileReader();
593
+ r.onload = () => resolve(r.result.split(',')[1]);
594
+ r.onerror = reject;
595
+ r.readAsDataURL(f);
596
+ return;
597
+ }
598
+ const img = new Image();
599
+ const url = URL.createObjectURL(f);
600
+ img.onload = () => {
601
+ const maxW = 1080;
602
+ let w = img.width, h = img.height;
603
+ if (w > maxW) { h = h * (maxW/w); w = maxW; }
604
+ const canvas = document.createElement('canvas');
605
+ canvas.width = w; canvas.height = h;
606
+ const ctx = canvas.getContext('2d');
607
+ ctx.drawImage(img, 0, 0, w, h);
608
+ const dataUrl = canvas.toDataURL('image/jpeg', 0.8);
609
+ URL.revokeObjectURL(url);
610
+ resolve(dataUrl.split(',')[1]);
611
+ };
612
+ img.onerror = reject;
613
+ img.src = url;
614
  });
615
+ archivos.push({nombre: f.name.replace(/\.[^.]+$/, '.jpg'), contenido: b64});
616
  } catch(e) { console.log("Error archivo", f.name, e); }
617
  }
618
  document.getElementById('mediainfo1').textContent = archivos.length + " archivos listos";