diff options
| author | it-fixcomart <it@fixcomart.co.id> | 2025-01-15 15:43:42 +0700 |
|---|---|---|
| committer | it-fixcomart <it@fixcomart.co.id> | 2025-01-15 15:43:42 +0700 |
| commit | be1ee2092fef86e79932206cc48d5a146107ac32 (patch) | |
| tree | e54a8c6ffda996f212ab1c2d03b27a32bab8ba89 /indoteknik_api/controllers/controller.py | |
| parent | 8b2897d9c72eb67382221d320d488543aea08323 (diff) | |
| parent | e4ec9406cd0903db59cfed34781da55a2dba4ca3 (diff) | |
Merge branch 'odoo-production' into iman/switch-account
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 '' |
