diff options
| author | Sébastien Theys <seb@odoo.com> | 2019-04-26 10:55:13 +0000 |
|---|---|---|
| committer | Sébastien Theys <seb@odoo.com> | 2019-04-29 13:45:03 +0000 |
| commit | 433d0070755b9917e46827c2ad1c258cfe31f804 (patch) | |
| tree | d3ed4fc444c3f6548ac9babf011e6174a9a4c76d /addons/web_editor/controllers | |
| parent | 8269f855136050c9139352ed71bc75f81899e255 (diff) | |
[IMP] tools,web_editor,web_unsplash: improve image optimize for web
This commit introduces the possibility to resize and to select the quality when
optimizing an image for web.
This will be used by the media dialog in a future commit.
The arbitrary resolution limit of 42e6 has been increased to 45e6 to fit some
of the biggest images we might get from Unsplash, and this value has been moved
into a variable so it can be customized.
The mimetype computation has been moved after the image processing because in a
next commit the operation could return the image in a different format than what
was originally given. Eg. BMP -> PNG, or unsupported format -> JPEG.
task-1958000
PR: #31811
Diffstat (limited to 'addons/web_editor/controllers')
| -rw-r--r-- | addons/web_editor/controllers/main.py | 24 |
1 files changed, 7 insertions, 17 deletions
diff --git a/addons/web_editor/controllers/main.py b/addons/web_editor/controllers/main.py index 8360161fb3d..d4542f8505d 100644 --- a/addons/web_editor/controllers/main.py +++ b/addons/web_editor/controllers/main.py @@ -158,27 +158,17 @@ class Web_Editor(http.Controller): #------------------------------------------------------ @http.route('/web_editor/add_image_base64', type='json', auth='user', methods=['POST'], website=True) def add_image_base64(self, res_model, res_id, image_base64, filename, disable_optimization=None, **kwargs): - data = base64.b64decode(image_base64) - attachment = self._image_to_attachment(res_model, res_id, data, filename, filename, disable_optimization=disable_optimization) + attachment = self._image_to_attachment(res_model, res_id, image_base64, filename, filename, disable_optimization=disable_optimization) return attachment.read(['name', 'mimetype', 'checksum', 'url', 'res_id', 'res_model', 'access_token'])[0] - def _image_to_attachment(self, res_model, res_id, data, name, datas_fname, disable_optimization=None): + def _image_to_attachment(self, res_model, res_id, image_base64, name, datas_fname, disable_optimization=None): Attachments = request.env['ir.attachment'] - try: - image = Image.open(io.BytesIO(data)) - w, h = image.size - if w*h > 42e6: # Nokia Lumia 1020 photo resolution - raise ValueError( - u"Image size excessive, uploaded images must be smaller " - u"than 42 million pixel") - if not disable_optimization and image.format in ('PNG', 'JPEG'): - data = tools.image_save_for_web(image) - except IOError: - pass + if not disable_optimization: + image_base64 = tools.image_optimize_for_web(image_base64) attachment = Attachments.create({ 'name': name, 'datas_fname': datas_fname, - 'datas': base64.b64encode(data), + 'datas': image_base64, 'public': res_model == 'ir.ui.view', 'res_id': res_id, 'res_model': res_model, @@ -224,12 +214,12 @@ class Web_Editor(http.Controller): try: attachments = request.env['ir.attachment'] for c_file in request.httprequest.files.getlist('upload'): - data = c_file.read() + image_base64 = base64.b64encode(c_file.read()) name = c_file.filename datas_fname = name if filters: datas_fname = filters + '_' + datas_fname - attachments += self._image_to_attachment(res_model, res_id, data, name, datas_fname, disable_optimization=disable_optimization) + attachments += self._image_to_attachment(res_model, res_id, image_base64, name, datas_fname, disable_optimization=disable_optimization) uploads += attachments.read(['name', 'mimetype', 'checksum', 'url', 'res_id', 'res_model', 'access_token']) except Exception as e: logger.exception("Failed to upload image to attachment") |
