diff options
| author | it-fixcomart <it@fixcomart.co.id> | 2024-12-31 09:44:11 +0700 |
|---|---|---|
| committer | it-fixcomart <it@fixcomart.co.id> | 2024-12-31 09:44:11 +0700 |
| commit | 8d00df73e76162d624d2f32eefdd47ca68ca154c (patch) | |
| tree | ef2b9706de3bbe895709502bf60506bca8f20a91 /indoteknik_api/controllers/controller.py | |
| parent | 6a7b2e28c9c1612ac3e91ac321b72e3400fdb5a3 (diff) | |
| parent | d35c2dce88a87bc05d30c4935d51d7d58aa5d37d (diff) | |
Merge branch 'production' into iman/telegram
# Conflicts:
# indoteknik_custom/models/stock_picking.py
Diffstat (limited to 'indoteknik_api/controllers/controller.py')
| -rw-r--r-- | indoteknik_api/controllers/controller.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/indoteknik_api/controllers/controller.py b/indoteknik_api/controllers/controller.py index a34a2688..80f45074 100644 --- a/indoteknik_api/controllers/controller.py +++ b/indoteknik_api/controllers/controller.py @@ -4,6 +4,7 @@ import functools import io import json from array import array +from io import BytesIO import jwt from odoo import http @@ -11,6 +12,8 @@ from odoo.http import request from odoo.modules import get_module_resource from odoo.tools.config import config from PIL import Image +from PIL.WebPImagePlugin import Image +from PIL import features from pytz import timezone @@ -204,6 +207,8 @@ class Controller(http.Controller): if not variant: image = self.add_watermark_to_image(image, ratio, version) + # image = self.convert_to_webp(image) + response_headers = [ ('Content-Type', 'image/jpg'), ('Cache-Control', 'public, max-age=3600') @@ -214,6 +219,31 @@ class Controller(http.Controller): response_headers ) + def convert_to_webp(self, image_base64): + """Convert image from base64 to WebP format and return base64 WebP.""" + try: + print(f"Image base64 length: {len(image_base64)}") + + # Decode Base64 to Bytes + image_data = base64.b64decode(image_base64) + image = Image.open(BytesIO(image_data)) + + if image.format == "PNG" and image.mode != "RGBA": + image = image.convert("RGBA") + + # Convert to WebP + with BytesIO() as output: + image.save(output, format="WEBP", quality=85) + webp_data = output.getvalue() + + # Encode back to Base64 + return base64.b64encode(webp_data).decode('utf-8') + except Exception as e: + print(f"Error details: {e}") + # If conversion fails, return the original image + request.env.cr.rollback() # Rollback any transactions + return image_base64 + def add_watermark_to_image(self, image, ratio, version = '1'): if not image: return '' |
