diff --git a/backend/open_webui/models/users.py b/backend/open_webui/models/users.py index 8494c63..bb12ed8 100644 --- a/backend/open_webui/models/users.py +++ b/backend/open_webui/models/users.py @@ -9,8 +9,9 @@ from open_webui.models.groups import Groups from PIL import Image, ImageDraw, ImageFont import random -import base64 import io +import base64 +import requests import requests @@ -347,7 +348,7 @@ class UsersTable: with get_db() as db: users = db.query(User).filter(User.id.in_(user_ids)).all() return [user.id for user in users] - + @staticmethod def generate_image_base64(letter, size=200): if isinstance(size, int): @@ -356,55 +357,51 @@ class UsersTable: background_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) image = Image.new('RGB', size, color=background_color) draw = ImageDraw.Draw(image) - - # Ustawienie koloru czcionki na biały text_color = (255, 255, 255) - # Funkcja do obliczania rozmiaru czcionki - def get_font_size(image_size, text, font_path, target_percentage=0.8): + def get_font_size(image_size, text, font_source, target_percentage=0.8): font_size = 1 - font = ImageFont.truetype(font_path, font_size) - while font.getbbox(text)[2] < target_percentage * image_size[0] and \ - font.getbbox(text)[3] < target_percentage * image_size[1]: + while True: + if isinstance(font_source, bytes): + font_file = io.BytesIO(font_source) + font = ImageFont.truetype(font_file, font_size) + elif isinstance(font_source, io.BytesIO): + font_data = font_source.getvalue() + font_file = io.BytesIO(font_data) + font = ImageFont.truetype(font_file, font_size) + else: + font = ImageFont.truetype(font_source, font_size) + + bbox = font.getbbox(text) + text_width = bbox[2] - bbox[0] + text_height = bbox[3] - bbox[1] + if (text_width >= target_percentage * image_size[0] or + text_height >= target_percentage * image_size[1]): + break font_size += 1 - font = ImageFont.truetype(font_path, font_size) return font_size - 1 try: font_path = "arial.ttf" font_size = get_font_size(size, letter, font_path) - print(f"1) {font_size}") font = ImageFont.truetype(font_path, font_size) except IOError: - # Jeśli Arial nie jest dostępny, użyj czcionki domyślnej - #font = ImageFont.load_default() - # Dla czcionki domyślnej możemy potrzebować innej metody określania rozmiaru - #font_size = min(size) * 10 # To jest przybliżenie, może wymagać dostosowania - #print(f"2) {font_size}") - # Pobierz czcionkę z Google Fonts (np. Roboto) - font_url = "https://fonts.gstatic.com/s/roboto/v27/KFOmCnqEu92Fr1Mu4mxP.ttf" + font_url = "https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Mu4mxKKTU1Kg.woff2" response = requests.get(font_url) - font_file = io.BytesIO(response.content) - font_size = get_font_size(size, letter, font_file) + font_data = response.content + font_size = get_font_size(size, letter, font_data) + font_file = io.BytesIO(font_data) + font = ImageFont.truetype(font_file, font_size) - # Użyj pobranej czcionki - font = ImageFont.truetype(font_file, size=font_size) - #print(f"2) {font_size}") - - # Obliczanie pozycji tekstu bbox = font.getbbox(letter) text_width = bbox[2] - bbox[0] text_height = bbox[3] - bbox[1] - position = ((size[0] - text_width) / 2, (size[1] - text_height) / 2) - - # Rysowanie litery + position = ((size[0] - text_width) // 2, (size[1] - text_height) // 2) draw.text(position, letter, font=font, fill=text_color) - # Konwersja obrazu do base64 buffered = io.BytesIO() image.save(buffered, format="PNG") img_str = base64.b64encode(buffered.getvalue()).decode() - return f"data:image/png;base64,{img_str}" def update_user_profile(self, id: str, name: str, profile_image_url: str, role: str, permissions: Optional[dict] = None, subscription: Optional[List[str]] = None) -> bool: