Advertisement
MizunoBrasil

Insere texto com quebra de linha na IMAGEM

Feb 20th, 2024
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.   <title>Enviar Imagem</title>
  7.   <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  8.   <style>
  9.     #imageContainer {
  10.       position: relative;
  11.       display: inline-block;
  12.     }
  13.     #textOverlay {
  14.       position: absolute;
  15.       bottom: 10px;
  16.       right: 10px;
  17.       color: white;
  18.       font-size: 18px;
  19.       text-shadow: 2px 2px 2px black;
  20.       background-color: rgba(0, 0, 0, 0.5);
  21.       padding: 10px;
  22.       border-radius: 5px;
  23.       white-space: pre-wrap; /* Para manter quebras de linha */
  24.     }
  25.   </style>
  26. </head>
  27. <body>
  28.   <div class="container mt-5">
  29.     <form id="imageForm" enctype="multipart/form-data">
  30.       <div class="form-group">
  31.         <label for="image">Selecione uma imagem:</label>
  32.         <input type="file" class="form-control-file" id="image" accept="image/*" required>
  33.       </div>
  34.       <div class="form-group">
  35.         <label for="caption">Insira o texto desejado:</label>
  36.         <textarea class="form-control" id="caption" rows="3"></textarea>
  37.       </div>
  38.       <button type="submit" class="btn btn-primary">Enviar</button>
  39.     </form>
  40.   </div>
  41.  
  42.   <script>
  43.     document.getElementById('imageForm').addEventListener('submit', function(event) {
  44.       event.preventDefault();
  45.       const fileInput = document.getElementById('image');
  46.       const file = fileInput.files[0];
  47.       const caption = document.getElementById('caption').value.trim(); // Obter texto do textarea e remover espaços em branco extras
  48.      
  49.       if (file) {
  50.         const reader = new FileReader();
  51.         reader.onload = function(e) {
  52.           const imageData = e.target.result;
  53.           const image = new Image();
  54.           image.src = imageData;
  55.          
  56.           image.onload = function() {
  57.             const canvas = document.createElement('canvas');
  58.             const context = canvas.getContext('2d');
  59.             canvas.width = image.width;
  60.             canvas.height = image.height;
  61.             context.drawImage(image, 0, 0);
  62.            
  63.             context.fillStyle = 'white';
  64.             context.font = '18px Arial';
  65.             context.textAlign = 'right';
  66.             context.textBaseline = 'bottom';
  67.  
  68.             const lines = caption.split('\n').reverse(); // Invertendo a ordem das linhas
  69.             const lineHeight = 25;
  70.             lines.forEach((line, index) => {
  71.               context.fillText(line, canvas.width - 10, canvas.height - 10 - (index * lineHeight));
  72.             });
  73.  
  74.             const imageWithText = canvas.toDataURL('image/png');
  75.             const link = document.createElement('a');
  76.             link.href = imageWithText;
  77.             link.download = 'image_with_text.png';
  78.             link.click();
  79.           };
  80.  
  81.         }
  82.         reader.readAsDataURL(file);
  83.       }
  84.     });
  85.   </script>
  86. </body>
  87. </html>
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement