diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2024-01-23 10:08:26 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2024-01-23 10:08:26 +0700 |
| commit | 50dfe6d8fa94b784c9cf62fcc6611131170c17e2 (patch) | |
| tree | ab9908dc9870e8dda491821faafa776aa8dc3005 | |
| parent | f7fe2253a8c79ff5172cf74c1f1aa341c9bf4f07 (diff) | |
Add watermark on image api
| -rw-r--r-- | indoteknik_api/controllers/controller.py | 42 | ||||
| -rw-r--r-- | indoteknik_api/static/src/images/logo-indoteknik.png | bin | 0 -> 29443 bytes |
2 files changed, 25 insertions, 17 deletions
diff --git a/indoteknik_api/controllers/controller.py b/indoteknik_api/controllers/controller.py index 95b7d838..36f5aa9b 100644 --- a/indoteknik_api/controllers/controller.py +++ b/indoteknik_api/controllers/controller.py @@ -186,30 +186,38 @@ class Controller(http.Controller): @http.route('/api/image/<model>/<field>/<id>', auth='public', methods=['GET']) 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 '' + watermark = kw.get('watermark', '') 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_path = get_module_resource('indoteknik_api', 'static', 'src', 'fonts', 'Inter.ttf') - font = ImageFont.truetype(font_path, 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') + image = self.add_watermark_to_image(image) - response_headers = [('Content-Type', 'image/jpg'), ('Cache-Control', 'public, max-age=3600')] + response_headers = [ + ('Content-Type', 'image/jpg'), + ('Cache-Control', 'public, max-age=3600') + ] return request.make_response( base64.b64decode(image), response_headers ) + + def add_watermark_to_image(self, image): + logo_path = get_module_resource('indoteknik_api', 'static', 'src', 'images', 'logo-indoteknik.png') + logo_img = Image.open(logo_path) + logo_img = logo_img.convert('RGBA') + + img_data = io.BytesIO(base64.b64decode(image)) + img = Image.open(img_data) + img = img.convert('RGBA') + + img_width, img_height = img.size + logo_img.thumbnail((img_width // 3, img_height // 3)) + + img.paste(logo_img, (20, 15), logo_img) + + buffered = io.BytesIO() + img.save(buffered, format="PNG") + return base64.b64encode(buffered.getvalue()).decode('utf-8') + diff --git a/indoteknik_api/static/src/images/logo-indoteknik.png b/indoteknik_api/static/src/images/logo-indoteknik.png Binary files differnew file mode 100644 index 00000000..0770c265 --- /dev/null +++ b/indoteknik_api/static/src/images/logo-indoteknik.png |
