-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
227 lines (207 loc) · 9.41 KB
/
index.html
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text to Image Generator</title>
<style>
body, html {
height: 100%;
margin: 0;
}
#imagesContainer img {
max-width: 100%;
height: auto;
}
#textInput {
min-height: 10rem;
}
.bordered {
border: 2px solid #000; /* Example: solid black border */
}
.flex-container {
flex-shrink: 0;
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
}
</style>
</head>
<body>
<div class="flex-container">
<div id="instructionsContainer" style="margin-bottom: 20px; padding: 10px; border: 1px solid #ccc; border-radius: 5px; background-color: #f9f9f9;">
<h2>Welcome to the Text to Image Generator!</h2>
<p>This tool allows you to generate images from your text. Simply enter your text, adjust the settings below, and click "Generate Images".</p>
<p>You can also select a custom font file (.ttf or .otf) to personalize your images further.</p>
<p><strong>Each line of text will produce 1 image.</strong></p>
</div>
</div>
<div class="flex-container">
<textarea id="textInput" rows="10" cols="50" placeholder="Enter your text here..." title="Enter text to generate images"></textarea><br>
<label for="canvasWidth" title="Set the width of the canvas">Canvas Width:</label>
<input type="number" id="canvasWidth" value="1920"><br>
<label for="canvasHeight" title="Set the height of the canvas">Canvas Height:</label>
<input type="number" id="canvasHeight" value="200"><br>
<label for="fontSize" title="Set the font size for the text">Font Size:</label>
<input type="number" id="fontSize" value="48"><br>
<label for="fontColor" title="Choose the font color">Font Color:</label>
<input type="color" id="fontColor" value="#000000"><br>
<label for="fontFile" title="Select a custom font file (.ttf or .otf)">Select a Font File:</label>
<input type="file" id="fontFile" accept=".ttf,.otf"><br>
<button onclick="generateImages()" title="Click to generate images from text">Generate Images</button><br>
<!--<button id="downloadAllConsent" title="Download all generated images">Download All Images</button>-->
<button id="removeAllImages" title="Remove all displayed images">Remove All Images</button>
<button id="toggleBorder" title="Toggle border on images">Toggle Border</button>
</div>
<div id="imagesContainer"></div>
<script>
// Event listener for the Toggle Border button
document.getElementById('toggleBorder').addEventListener('click', function() {
const images = document.querySelectorAll('#imagesContainer img');
images.forEach(img => {
img.classList.toggle('bordered');
});
});
/**
* Generates images from input text, each line of text becomes a separate image.
* The images are displayed with a transparent background.
*/
function generateImages() {
const textInput = document.getElementById('textInput').value;
const lines = textInput.split('\n').filter(line => line.trim() !== '');
const imagesContainer = document.getElementById('imagesContainer');
imagesContainer.innerHTML = '';
const canvasWidth = parseInt(document.getElementById('canvasWidth').value, 10);
const canvasHeight = parseInt(document.getElementById('canvasHeight').value, 10);
const fontSize = parseInt(document.getElementById('fontSize').value, 10);
const fontColor = document.getElementById('fontColor').value; // Get the selected font color
lines.forEach((line, index) => {
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.width = canvasWidth;
canvas.height = canvasHeight;
// Clear the canvas to ensure the background is transparent
context.clearRect(0, 0, canvas.width, canvas.height);
if (window.customFontName) {
context.font = `${fontSize}px ${window.customFontName}`;
}
else {
context.font = `${fontSize}px Arial`;
}
context.textAlign = 'center';
context.textBaseline = 'middle';
context.fillStyle = fontColor; // Use the selected font color
console.log(fontColor);
const textX = canvas.width / 2;
const textY = canvas.height / 2;
context.fillText(line, textX, textY);
const img = document.createElement('img');
img.src = canvas.toDataURL('image/png');
imagesContainer.appendChild(img);
const sanitizedLine = line.replace(/[^a-zA-Z0-9]/g, '_').substring(0, 15);
const downloadLink = document.createElement('a');
downloadLink.href = img.src;
downloadLink.download = `${sanitizedLine}.png`;
downloadLink.innerText = `Download Image: ${sanitizedLine}.png`;
imagesContainer.appendChild(downloadLink);
const removeButton = document.createElement('button');
removeButton.innerText = 'Remove';
removeButton.onclick = function() {
img.remove();
downloadLink.remove();
removeButton.remove();
};
imagesContainer.appendChild(removeButton);
// Create a Save As button for each image
const saveAsButton = document.createElement('button');
saveAsButton.innerText = 'Save As';
saveAsButton.onclick = function() {
const customName = prompt('Enter a filename:', `${sanitizedLine}.png`);
if (customName) {
const link = document.createElement('a');
link.href = img.src;
link.download = customName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
};
imagesContainer.appendChild(saveAsButton);
});
}
document.getElementById('removeAllImages').addEventListener('click', function() {
document.getElementById('imagesContainer').innerHTML = '';
});
document.getElementById('fontFile').addEventListener('change', function(e) {
const file = e.target.files[0];
if (!file) {
updateFontFamily('Arial');
return;
}
const reader = new FileReader();
reader.onload = function(event) {
const fontFace = new FontFace('CustomFont', event.target.result);
fontFace.load().then(function(loadedFace) {
document.fonts.add(loadedFace);
// Update the font in the generateImages function or elsewhere as needed
// For example, set a global variable or directly modify the generateImages function to use 'CustomFont'
updateFontFamily('CustomFont');
}).catch(function(error) {
console.error('Font loading failed:', error);
});
};
reader.readAsArrayBuffer(file);
});
function updateFontFamily(fontName) {
// Example: Update the font used in the generateImages function
// This assumes generateImages or a similar function will use this variable for the font name
window.customFontName = fontName;
}
// //not implemented as generated code is not working.
//
// document.getElementById('downloadAllConsent').addEventListener('click', function() {
// const userConsent = confirm('This feature requires loading a third-party library (JSZip). Do you agree to proceed?');
// if (userConsent) {
// loadJSZipLibrary().then(() => {
// downloadAllImagesAsZip();
// }).catch(error => {
// console.error('Failed to load JSZip library:', error);
// });
// }
// });
//
// function loadJSZipLibrary() {
// return new Promise((resolve, reject) => {
// const script = document.createElement('script');
// script.src = 'https://cdnjs.cloudflare.com/ajax/libs/jszip/3.7.1/jszip.min.js';
// script.onload = () => resolve();
// script.onerror = () => reject(new Error('JSZip library failed to load'));
// document.head.appendChild(script);
// });
// }
//
// function downloadAllImagesAsZip() {
// const zip = new JSZip();
// const images = document.querySelectorAll('#imagesContainer img');
// let count = 0;
//
// images.forEach((img, index) => {
// const filename = `image${index + 1}.png`;
// fetch(img.src)
// .then(res => res.blob())
// .then(blob => {
// zip.file(filename, blob);
// count++;
// if (count === images.length) {
// zip.generateAsync({type: 'blob'}).then(function(content) {
// saveAs(content, 'images.zip'); // Assumes FileSaver.js is also loaded or available
// });
// }
// });
// });
// }
</script>
</body>
</html>