-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.js
89 lines (70 loc) · 3.14 KB
/
decoder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
let imgElement; // Variable to store the generated image
// Function to validate Base64 string
function isValidBase64(base64String) {
const regex = /^data:image\/(png|jpeg|webp);base64,[a-zA-Z0-9+/]+={0,2}$/;
return regex.test(base64String);
}
// Function to convert Base64 to image
function convertBase64ToImage() {
const base64String = document.getElementById('base64Input').value.trim();
if (base64String === '') {
alert('Please enter a Base64 string.');
return;
}
// Ensure string has the required header, or validate it
let base64Data = base64String.startsWith('data:image/')
? base64String
: 'data:image/png;base64,' + base64String;
if (!isValidBase64(base64Data)) {
alert('Invalid Base64 string or format. Please use a valid image format.');
return;
}
// Create the image element
imgElement = document.createElement('img');
imgElement.src = base64Data;
// Clear the previous result and display the new image
const resultDiv = document.getElementById('result');
resultDiv.innerHTML = ''; // Clear previous content
resultDiv.appendChild(imgElement);
// Show the download button
document.getElementById('downloadButton').style.display = 'block';
// Update the preview for the current scale
updatePreview();
}
// Function to update the image preview based on the scale input
function updatePreview() {
const scale = parseFloat(document.getElementById('scaleInput').value);
const resultDiv = document.getElementById('result');
// Re-scale the image in the canvas for preview
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = imgElement.width * scale;
canvas.height = imgElement.height * scale;
ctx.drawImage(imgElement, 0, 0, canvas.width, canvas.height);
// Update the preview in the result div
resultDiv.innerHTML = ''; // Clear previous content
const previewImage = document.createElement('img');
previewImage.src = canvas.toDataURL();
resultDiv.appendChild(previewImage);
}
// Function to download the scaled image
function downloadImage() {
const format = document.getElementById('formatSelect').value; // Selected format (png, jpeg, webp)
const scale = parseFloat(document.getElementById('scaleInput').value); // Get the user-defined scale
// Create a canvas to draw the image
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
// Set canvas width and height according to the scale
canvas.width = imgElement.width * scale;
canvas.height = imgElement.height * scale;
// Draw the scaled image on the canvas
ctx.drawImage(imgElement, 0, 0, canvas.width, canvas.height);
// Generate the image URL in the selected format
const downloadLink = document.createElement('a');
downloadLink.href = canvas.toDataURL(`image/${format}`);
downloadLink.download = `image.${format}`;
// Trigger the download
downloadLink.click();
}
// Add event listener to scale input for live preview
document.getElementById('scaleInput').addEventListener('input', updatePreview);