Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document Photo Upload</title>
- <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
- <style>
- .upload-box {
- border: 2px dashed #000;
- text-align: center;
- margin-top: 20px;
- cursor: pointer;
- width: 200px; /* Set the desired width */
- height: 200px; /* Set the desired height */
- background-size: cover; /* Ensure the background image covers the entire box */
- background-repeat: no-repeat; /* Prevent repeating the background image */
- }
- </style>
- </head>
- <body>
- <div class="container mt-5">
- <ul class="nav nav-tabs">
- <li class="nav-item">
- <a class="nav-link active" href="#">SEBELUM</a>
- </li>
- <li class="nav-item">
- <a class="nav-link" href="#">SEMENTARA</a>
- </li>
- <li class="nav-item">
- <a class="nav-link" href="#">SELESAI</a>
- </li>
- </ul>
- <div id="beforeWork" class="upload-box" onclick="handleFileUpload()">
- <h5>Sebelum Pekerjaan</h5>
- <!-- Hidden file input to handle file uploads -->
- <input type="file" id="fileInput" onchange="displayPhoto(event)" hidden>
- </div>
- <p>KLIK AREA KOTAK INI UNTUK MENGUPLOAD ATAU MENGAMBIL FOTO</p>
- <button onclick="savePhoto()">SIMPAN</button>
- </div>
- <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
- <script>
- function handleFileUpload() {
- const fileInput = document.getElementById('fileInput');
- fileInput.click();
- }
- function displayPhoto(event) {
- const selectedFile = event.target.files[0];
- if (selectedFile) {
- // Display the photo (set as background image)
- const uploadBox = document.getElementById('beforeWork');
- uploadBox.style.backgroundImage = `url(${URL.createObjectURL(selectedFile)})`;
- }
- }
- function savePhoto() {
- const selectedFile = document.getElementById('fileInput').files[0];
- if (selectedFile) {
- // Create a new FileReader
- const reader = new FileReader();
- // Define what to do when the file is read
- reader.onload = function(event) {
- // Create an image element to display the photo
- const image = new Image();
- image.src = event.target.result;
- // Draw the image on a canvas
- const canvas = document.createElement('canvas');
- const ctx = canvas.getContext('2d');
- canvas.width = image.width;
- canvas.height = image.height;
- ctx.drawImage(image, 0, 0);
- // Add text "SEBELUM PEKERJAAN" to the image
- ctx.font = "20px Arial";
- ctx.fillStyle = "white";
- ctx.fillText("SEBELUM PEKERJAAN", 10, 30);
- // Convert the canvas content to data URL and create a link to download
- const dataURL = canvas.toDataURL();
- const a = document.createElement('a');
- a.href = dataURL;
- a.download = selectedFile.name;
- a.click();
- console.log('Photo saved with text:', selectedFile.name);
- };
- // Read the file as a data URL
- reader.readAsDataURL(selectedFile);
- } else {
- console.log('No photo selected.');
- }
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement