File size: 8,085 Bytes
110d1d4
9131604
110d1d4
 
273d698
110d1d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273d698
9131604
110d1d4
 
 
273d698
110d1d4
273d698
110d1d4
 
 
 
 
9131604
110d1d4
 
 
 
 
 
 
 
 
 
 
 
 
9131604
110d1d4
9131604
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1eb5570
9131604
 
 
 
 
 
 
1eb5570
9131604
1eb5570
 
 
110d1d4
1eb5570
 
 
 
110d1d4
1eb5570
 
 
 
 
 
 
 
 
 
 
 
9131604
 
1eb5570
 
 
 
 
110d1d4
1eb5570
110d1d4
 
 
 
 
 
 
 
 
 
273d698
110d1d4
273d698
110d1d4
9131604
1eb5570
9131604
 
 
1eb5570
9131604
273d698
9131604
110d1d4
 
1eb5570
 
 
110d1d4
9131604
 
110d1d4
9131604
110d1d4
 
273d698
110d1d4
 
 
9131604
 
 
 
110d1d4
 
9131604
 
 
 
273d698
9131604
110d1d4
 
 
 
 
 
 
 
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import io
import os
import urllib.parse
from flask import Flask, request, send_file, render_template_string
from PIL import Image, ImageFont, ImageDraw, ImageFilter
from pilmoji import Pilmoji

app = Flask(__name__)

# ==========================================
# UI HTML (Mobile Friendly)
# ==========================================
HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Brat Generator</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            margin: 0;
            padding: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }
        .container {
            background: white;
            padding: 25px;
            border-radius: 12px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 400px;
            box-sizing: border-box;
        }
        h2 { text-align: center; margin-top: 0; color: #333; }
        textarea {
            width: 100%;
            height: 100px;
            padding: 12px;
            box-sizing: border-box;
            border: 2px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
            resize: none;
            margin-bottom: 15px;
        }
        button {
            width: 100%;
            background-color: #111;
            color: white;
            border: none;
            padding: 14px;
            font-size: 16px;
            font-weight: bold;
            border-radius: 8px;
            cursor: pointer;
            transition: background 0.2s;
        }
        button:hover { background-color: #333; }
        .result-container {
            margin-top: 20px;
            text-align: center;
        }
        img {
            max-width: 100%;
            border: 1px solid #eaeaea;
            border-radius: 4px;
            display: block;
            margin: 0 auto;
        }
        .download-btn {
            display: inline-block;
            margin-top: 15px;
            color: #111;
            text-decoration: none;
            font-weight: bold;
            font-size: 14px;
        }
    </style>
</head>
<body>

<div class="container">
    <h2>Brat Generator ⬜</h2>
    <textarea id="inputText" placeholder="Ini adalah\ncontoh\nbrat😭">Ini adalah&#10;contoh&#10;brat😭</textarea>
    <button onclick="generateImage()">Buat Gambar</button>
    
    <div class="result-container">
        <img id="resultImg" src="/generate?text=Ini%20adalah%0Acontoh%0Abrat%F0%9F%98%AD" alt="Hasil Render">
        <br>
        <a id="downloadLink" class="download-btn" href="/generate?text=Ini%20adalah%0Acontoh%0Abrat%F0%9F%98%AD" download="brat.png">⬇️ Download Gambar</a>
    </div>
</div>

<script>
    function generateImage() {
        const text = document.getElementById('inputText').value;
        const encodedText = encodeURIComponent(text);
        const url = `/generate?text=${encodedText}`;
        
        document.getElementById('resultImg').src = url;
        document.getElementById('downloadLink').href = url;
    }
</script>

</body>
</html>
"""

# ==========================================
# Fungsi Pencari Font Fleksibel
# ==========================================
def get_font(size):
    # Mencari Arial atau font mirip Arial di berbagai OS
    font_paths = [
        "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf",       # Ubuntu/Linux Arial
        "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf", # Linux Fallback
        "C:\\Windows\\Fonts\\arial.ttf",                           # Windows
        "/System/Library/Fonts/Supplemental/Arial.ttf",            # Mac OS
    ]
    for path in font_paths:
        if os.path.exists(path):
            try:
                return ImageFont.truetype(path, size)
            except:
                continue
    return ImageFont.load_default()

# ==========================================
# Algoritma Auto-Scaling yang Diperbaiki (Struktur Tetap Terjaga)
# ==========================================
def get_optimal_font_and_text(text, max_width, max_height, line_spacing=10):
    # BATASAN UKURAN FONT: 
    # Dimulai dari 75px (tidak 180px) agar kalimat seperti "Ini adalah" 
    # tidak terpecah menjadi 2 baris karena font terlalu besar.
    max_font_size = 75 
    min_font_size = 15
    
    # Dummy image untuk menghitung text bounding box
    temp_img = Image.new('RGB', (1, 1))
    draw = ImageDraw.Draw(temp_img)
    
    for size in range(max_font_size, min_font_size, -1):
        font = get_font(size)

        lines = []
        # Menghormati tombol "Enter" yang diketik user
        for paragraph in text.split('\n'):
            words = paragraph.split(' ')
            current_line = []
            
            for word in words:
                test_line = ' '.join(current_line + [word]) if current_line else word
                if font.getlength(test_line) <= max_width:
                    current_line.append(word)
                else:
                    if current_line:
                        lines.append(' '.join(current_line))
                        current_line = [word]
                    else:
                        lines.append(word)
                        current_line = []
                        
            if current_line:
                lines.append(' '.join(current_line))
                
        wrapped_text = '\n'.join(lines)
        
        # Cek apakah blok teks (termasuk spasi antar baris) muat di layar
        bbox = draw.multiline_textbbox((0, 0), wrapped_text, font=font, align="center", spacing=line_spacing)
        text_w = bbox[2] - bbox[0]
        text_h = bbox[3] - bbox[1]
        
        if text_w <= max_width and text_h <= max_height:
            return font, wrapped_text
            
    return font, wrapped_text

# ==========================================
# Routes
# ==========================================
@app.route('/')
def index():
    return render_template_string(HTML_TEMPLATE)

@app.route('/generate')
def generate():
    text = request.args.get('text', 'Ini adalah\ncontoh\nbrat😭').strip()
    if not text:
        text = "Ini adalah\ncontoh\nbrat😭"

    # Konfigurasi Canvas 1:1 (512x512)
    img_size = 512
    # Padding diperbesar (60px) agar area teks tidak terlalu menempel ke pinggir layar
    padding = 60 
    line_spacing = 15 # Jarak antar baris teks
    
    # Warna Canvas (Putih Murni) & Warna Teks (Abu-abu Gelap, mencegah kontras berlebih)
    bg_color = (255, 255, 255) 
    text_color = (40, 40, 40)    

    img = Image.new('RGB', (img_size, img_size), color=bg_color)
    
    max_text_width = img_size - (padding * 2)
    max_text_height = img_size - (padding * 2)
    
    # Dapatkan font size ideal yang menjaga struktur kalimat
    font, wrapped_text = get_optimal_font_and_text(text, max_text_width, max_text_height, line_spacing)

    # Rendering Teks dengan Pilmoji
    with Pilmoji(img) as pilmoji:
        pilmoji.text(
            (img_size // 2, img_size // 2),  
            wrapped_text, 
            fill=text_color, 
            font=font,
            anchor="mm",        # Fix posisi benar-benar di tengah absolut
            align="center",     # Rata tengah untuk tiap baris
            spacing=line_spacing, # Spasi antar baris
            emoji_scale_factor=1.05 # Skala emoji sedikit dikecilkan agar seimbang
        )

    # =============== EFEK BLUR (LOW QUALITY VIBE) =================
    # Radius 0.5 memberi efek 'soft' persis kompresi gambar WhatsApp/Meme
    img = img.filter(ImageFilter.GaussianBlur(radius=0.5))
    # ==============================================================

    # Output memori
    img_io = io.BytesIO()
    img.save(img_io, 'PNG')
    img_io.seek(0)
    
    return send_file(img_io, mimetype='image/png')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)