File size: 3,057 Bytes
693c081
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import uuid
import requests
from flask import Flask, request, send_file
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO

app = Flask(__name__)

# Ensure temp directory exists
TEMP_DIR = "temp"
os.makedirs(TEMP_DIR, exist_ok=True)

@app.route('/api')
def generate_profile():
    # Get query parameters
    name = request.args.get('name', 'Unknown')
    rank = request.args.get('rank', 'Unranked')
    balance = request.args.get('balance', '0')
    next_rank = request.args.get('next_rank', 'Unknown')
    pfp_url = request.args.get('pfp', None)  # Profile picture URL

    # Load background
    bg = Image.open("bg.png").convert("RGBA")
    draw = ImageDraw.Draw(bg)
    
    # Load font
    font_large = ImageFont.truetype("font.ttf", 50)  # Larger font for name
    font_small = ImageFont.truetype("font.ttf", 40)  # Smaller font for details

    # Get image size
    W, H = bg.size  

    # Profile picture settings
    pfp_size = 150  # Size of the profile picture
    pfp_y = 50  # Position from top
    pfp_x = (W - pfp_size) // 2  # Centering horizontally

    # Load profile picture or fallback
    try:
        if pfp_url:
            response = requests.get(pfp_url, timeout=5)
            pfp = Image.open(BytesIO(response.content)).convert("RGBA")
        else:
            pfp = Image.open("fallback.png").convert("RGBA")  # Fallback image

        pfp = pfp.resize((pfp_size, pfp_size), Image.LANCZOS)

        # Create circular mask for profile picture
        mask = Image.new("L", (pfp_size, pfp_size), 0)
        draw_mask = ImageDraw.Draw(mask)
        draw_mask.ellipse((0, 0, pfp_size, pfp_size), fill=255)

        # Apply mask and paste profile picture
        pfp = Image.composite(pfp, Image.new("RGBA", pfp.size, (0, 0, 0, 0)), mask)
        bg.paste(pfp, (pfp_x, pfp_y), pfp)
    except Exception as e:
        print(f"Error loading profile picture: {e}")

    # Define text positions
    y_start = pfp_y + pfp_size + 30  # Start below the profile picture
    spacing = 80  # Vertical gap between lines
    text_color = (255, 255, 255)  # White color

    # Centered text positions
    def draw_centered_text(text, y, font):
        text_width, _ = draw.textsize(text, font=font)
        x = (W - text_width) // 2  # Center the text
        draw.text((x, y), text, font=font, fill=text_color)

    # Draw text
    draw_centered_text(f"Name: {name}", y_start, font_large)
    draw_centered_text(f"Rank: {rank}", y_start + spacing, font_small)
    draw_centered_text(f"Balance: {balance}", y_start + 2 * spacing, font_small)
    draw_centered_text(f"Next Rank: {next_rank}", y_start + 3 * spacing, font_small)

    # Generate a random filename
    output_path = os.path.join(TEMP_DIR, f"profile_{uuid.uuid4().hex}.png")
    bg.save(output_path)

    # Send and delete image after sending
    response = send_file(output_path, mimetype='image/png')
    os.remove(output_path)  # Clean up file after sending
    return response

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)