From e782d7294cad480bc7ac0b66696e4fef917ad88e Mon Sep 17 00:00:00 2001 From: norman-andrians Date: Sat, 9 Sep 2023 19:45:59 +0700 Subject: [PATCH] separate text with confirmation code --- README.md | 2 +- main.py | 33 +++++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a889b6b..798367e 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Setup: Usage: - Run `main.py` -- Send a request to http://localhost:5000/send-otp with your `email` query as email recipient. E.g: http://localhost:5000/send-top?email=your@email.com. +- Send a request to http://localhost:5000/send-otp with your `email` query as email recipient. E.g: http://localhost:5000/send-otp?email=your@email.com. - If successful then a response appears like this: ```json { diff --git a/main.py b/main.py index 010a7b0..ec3039f 100644 --- a/main.py +++ b/main.py @@ -12,7 +12,7 @@ from email.mime.image import MIMEImage app = Flask(__name__) -def add_textimg(text): +def add_textimg(text, code): def generatePath(): some_id = random.randint(2000, 9000) return f"images/output/pos-{str(some_id)}.png" @@ -22,11 +22,19 @@ def add_textimg(text): try: img = Image.open("images/main/black-suit.png") draw = ImageDraw.Draw(img) - font = ImageFont.truetype("fonts/impact.ttf", 64) - text_pos_center = (img.width - draw.textlength(text, font)) // 2 - pos = (text_pos_center, 720) + font = ImageFont.truetype("fonts/impact.ttf", 52) + + text_pos1_center = (img.width - draw.textlength(text, font)) // 2 + text_pos2_center = (img.width - draw.textlength(code, font)) // 2 + + text_pos_bottom = img.height + + pos1 = (text_pos1_center, text_pos_bottom - 200) + pos2 = (text_pos2_center, text_pos_bottom - 130) text_color = (255, 255, 255) - draw.text(pos, text, fill=text_color, font=font) + + draw.text(pos1, text, fill=text_color, font=font) + draw.text(pos2, code, fill=text_color, font=font) if not os.path.exists("images/output"): os.mkdir("images/output") @@ -44,6 +52,19 @@ def add_textimg(text): def hello(): return "Hello" +@app.route('/test/otp', methods=['POST']) +def generate_otp(): + try: + randis = random.randint(1000, 9999) + add_textimg(f"Your confirmation code is", str(randis)) + + return jsonify({"status": "ok"}) + except Exception as e: + print("Something went wrong") + print(str(e)) + + return jsonify({"error": str(e)}) + @app.route('/send-otp', methods=['POST']) def send_email(): # Create an .env file at the root of this project @@ -75,7 +96,7 @@ def send_email(): zmail.attach(MIMEText(msg, 'plain')) # attach picure - add_textimg("Your otp code is " + str(randis)) + add_textimg(f"Your confirmation code is", str(randis)) pict = open("images/output/pos.png", "rb").read() zmail.attach(MIMEImage(pict, name="black-man-wearing-suit.png"))