summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/controller.py
diff options
context:
space:
mode:
authorAzka Nathan <darizkyfaz@gmail.com>2024-01-25 16:04:12 +0700
committerAzka Nathan <darizkyfaz@gmail.com>2024-01-25 16:04:12 +0700
commit144d3e67017547437382187dc27cac480d209103 (patch)
tree14812be406c3d1a004b4a33d22d56cb2b92a6f8e /indoteknik_api/controllers/controller.py
parentbf84a6df71756afdd4b8846b8bba8fea4d243dee (diff)
parent19fa8e951da70347d822bb5b0f2acf5563afc704 (diff)
Merge branch 'production' of bitbucket.org:altafixco/indoteknik-addons into production
Diffstat (limited to 'indoteknik_api/controllers/controller.py')
-rw-r--r--indoteknik_api/controllers/controller.py29
1 files changed, 23 insertions, 6 deletions
diff --git a/indoteknik_api/controllers/controller.py b/indoteknik_api/controllers/controller.py
index 36f5aa9b..bd763e00 100644
--- a/indoteknik_api/controllers/controller.py
+++ b/indoteknik_api/controllers/controller.py
@@ -186,12 +186,16 @@ class Controller(http.Controller):
@http.route('/api/image/<model>/<field>/<id>', auth='public', methods=['GET'])
def get_image(self, model, field, id, **kw):
+ model_name = model
+
model = request.env[model].sudo().search([('id', '=', id)], limit=1)
image = model[field] if model[field] else ''
+ model_with_watermark = ['product.template', 'product.product']
watermark = kw.get('watermark', '')
- if watermark.lower() == 'true':
- image = self.add_watermark_to_image(image)
+ if watermark.lower() == 'true' or model_name in model_with_watermark:
+ ratio = kw.get('ratio', '')
+ image = self.add_watermark_to_image(image, ratio)
response_headers = [
('Content-Type', 'image/jpg'),
@@ -203,7 +207,7 @@ class Controller(http.Controller):
response_headers
)
- def add_watermark_to_image(self, image):
+ def add_watermark_to_image(self, image, ratio):
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')
@@ -213,11 +217,24 @@ class Controller(http.Controller):
img = img.convert('RGBA')
img_width, img_height = img.size
- logo_img.thumbnail((img_width // 3, img_height // 3))
+ logo_img.thumbnail((img_width // 2.2, img_height // 2.2))
+
+ new_img = img
+
+ if ratio == 'square':
+ longest_wh = max(img_height, img_width)
- img.paste(logo_img, (20, 15), logo_img)
+ new_img = Image.new('RGBA', (longest_wh, longest_wh), (255, 255, 255, 255))
+
+ paste_x = (longest_wh - img_width) // 2
+ paste_y = (longest_wh - img_height) // 2
+
+ new_img.paste(img, (paste_x, paste_y), img)
+
+ # Add watermark
+ new_img.paste(logo_img, (12, 10), logo_img)
buffered = io.BytesIO()
- img.save(buffered, format="PNG")
+ new_img.save(buffered, format="PNG")
return base64.b64encode(buffered.getvalue()).decode('utf-8')