-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_generator.py
188 lines (176 loc) · 5.76 KB
/
html_generator.py
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
import base64
import config
import time
from PIL import Image
import os, io
def generate_html_response(gif_files, port):
protocol = "https" if port == config.HTTPS_PORT else "http"
html = f'''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{config.HTML_TITLE}</title>
<style>
* {{
box-sizing: border-box;
margin: 0;
padding: 0;
}}
html, body {{
height: 100%;
width: 100%;
overflow-x: hidden;
}}
body {{
font-family: Arial, sans-serif;
background-color: {config.HTML_BACKGROUND_COLOR};
color: #333;
padding-top: 60px; /* Height of the header */
}}
header {{
background-color: {config.HTML_HEADER_COLOR};
color: white;
text-align: center;
padding: 1rem;
width: 100%;
position: fixed;-*
top: 0;
left: 0;
height: 60px; /* Fixed height for header */
z-index: 1000;
}}
h1 {{
margin: 0;
font-size: 24px;
line-height: 28px; /* Center text vertically in header */
}}
.content {{
max-width: 800px;
margin: 0 auto;
padding: 20px;
}}
.gif-column {{
display: flex;
flex-direction: column;
align-items: center;
gap: 20px;
}}
.gif-item {{
width: 100%;
max-width: 600px;
}}
.gif-item img {{
width: 100%;
height: auto;
display: block;
border: 1px solid #ddd;
border-radius: 4px;
}}
.back-link {{
display: block;
text-align: center;
margin-top: 20px;
color: {config.HTML_HEADER_COLOR};
text-decoration: none;
font-weight: bold;
}}
.download-link {{
display: block;
margin-top: 10px;
text-align: center;
color: #007bff;
text-decoration: none;
}}
</style>
</head>
<body>
<header>
<h1>{config.HTML_TITLE}</h1>
</header>
<div class="content">
<div class="gif-column">
'''
base_image_path = config.BASE_IMAGE_PATH
if os.path.exists(base_image_path):
with Image.open(base_image_path) as img:
if gif_files:
with Image.open(gif_files[0]) as first_gif:
img = img.resize(first_gif.size, Image.LANCZOS)
buffered = io.BytesIO()
img.save(buffered, format="PNG")
encoded_string = base64.b64encode(buffered.getvalue()).decode()
html += f'''
<div class="gif-item">
<img src="data:image/png;base64,{encoded_string}" alt="Base Image">
<a href="data:image/png;base64,{encoded_string}" download="base_image.png" class="download-link">Download Base Image</a>
</div>
'''
for i, gif_file in enumerate(gif_files):
with open(gif_file, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode()
html += f'''
<div class="gif-item">
<img src="data:image/gif;base64,{encoded_string}" alt="Generated GIF">
<a href="data:image/gif;base64,{encoded_string}" download="generated_gif_{i+1}.gif" class="download-link">Download GIF {i+1}</a>
</div>
'''
html += f'''
</div>
<a href="/" class="back-link">Back to Upload</a>
</div>
</body>
</html>
'''
return html
def get_gif_base64(gif_file):
with open(gif_file, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
def generate_no_gifs_html():
return '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>No GIFs Generated - GROUP GOES GIF</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.message-container {
background-color: white;
padding: 2rem;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
text-align: center;
}
h1 {
color: #4CAF50;
}
.back-link {
display: inline-block;
margin-top: 1rem;
color: #4CAF50;
text-decoration: none;
font-weight: bold;
}
</style>
</head>
<body>
<div class="message-container">
<h1>No GIFs Generated</h1>
<p>We couldn't detect available profiles in the uploaded image.</p>
<p>Please try uploading a different image with clear profiles.</p>
<a href="/" class="back-link">Back to Upload</a>
</div>
</body>
</html>
'''