summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/controller.py
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2024-01-22 14:46:13 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2024-01-22 14:46:13 +0700
commit24b383b580ae039d66e06a9b20b000b19f833033 (patch)
tree8ea39c035d744c659aeddb745bf2d47699a9ba68 /indoteknik_api/controllers/controller.py
parent931955161ec87ef152f9c76bee9f6c0abd81a2b5 (diff)
Add watermark in api get image
Diffstat (limited to 'indoteknik_api/controllers/controller.py')
-rw-r--r--indoteknik_api/controllers/controller.py21
1 files changed, 19 insertions, 2 deletions
diff --git a/indoteknik_api/controllers/controller.py b/indoteknik_api/controllers/controller.py
index 85eda235..7278ec23 100644
--- a/indoteknik_api/controllers/controller.py
+++ b/indoteknik_api/controllers/controller.py
@@ -7,8 +7,10 @@ from odoo import http, tools
from odoo.http import request
from odoo.tools.config import config
from pytz import timezone
+from PIL import Image, ImageDraw, ImageFont
import jwt
import functools
+import io
class Controller(http.Controller):
@@ -182,11 +184,26 @@ class Controller(http.Controller):
return self.response(address)
@http.route('/api/image/<model>/<field>/<id>', auth='public', methods=['GET'])
- def get_image(self, model, field, id):
+ def get_image(self, model, field, id, **kw):
+ watermark = kw.get('watermark', '')
model = request.env[model].sudo().search([('id', '=', id)], limit=1)
image = model[field] if model[field] else ''
- request.env['user.activity.log'].record_activity()
+ if watermark.lower() == 'true':
+ img_data = io.BytesIO(base64.b64decode(image))
+ img = Image.open(img_data)
+
+ img_width, img_height = img.size
+ font_size = int(min(img_width, img_height) * 0.04)
+ font = ImageFont.truetype("arial.ttf", font_size)
+
+ img = img.convert('RGBA')
+ draw = ImageDraw.Draw(img)
+ draw.text((10, 10), 'Indoteknik.com', fill=(0, 0, 0, 100), font=font)
+
+ buffered = io.BytesIO()
+ img.save(buffered, format="PNG")
+ image = base64.b64encode(buffered.getvalue()).decode('utf-8')
response_headers = [('Content-Type', 'image/jpg'), ('Cache-Control', 'public, max-age=3600')]