Skip to content

Commit

Permalink
[FIX] base: fix autoresize and exclude gif
Browse files Browse the repository at this point in the history
Odoo may resize attachment image with side larger than 1920 pixels.

But for animated gifs, this resizement seems to in general increase size
file which is not what we want (in some case making it grow from 3MB to
60 MB).

So with this change, we only resize and optimize images that are not
gifs.

Reasoning: pillow doesn't seem to resize GIF (and seems to only increase
their size, especially animated GIF, because each frame is not
optimized) so we should just not touch them.

Note:

- currently tiff were not resized (because of a mimetype typo)
- currently image dimensions were not resized (from our test, resizing
  on the dimension does not change the size much, quality is most
  important).

both of these issue have been solved in this commit.

opw-2897291

closes odoo#96307

Signed-off-by: Nicolas Lempereur (nle) <nle@odoo.com>
  • Loading branch information
nle-odoo committed Jul 27, 2022
1 parent 03bb18d commit 743ab67
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
7 changes: 3 additions & 4 deletions odoo/addons/base/models/ir_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def _compute_mimetype(self, values):

def _postprocess_contents(self, values):
ICP = self.env['ir.config_parameter'].sudo().get_param
supported_subtype = ICP('base.image_autoresize_extensions', 'png,jpeg,gif,bmp,tif').split(',')
supported_subtype = ICP('base.image_autoresize_extensions', 'png,jpeg,bmp,tiff').split(',')

mimetype = values['mimetype'] = self._compute_mimetype(values)
_type, _subtype = mimetype.split('/')
Expand All @@ -306,16 +306,15 @@ def _postprocess_contents(self, values):
img = ImageProcess(False, verify_resolution=False)
img.image = Image.open(io.BytesIO(values['raw']))
img.original_format = (img.image.format or '').upper()
fn_quality = img.image_quality
else: # datas
img = ImageProcess(values['datas'], verify_resolution=False)
fn_quality = img.image_base64

w, h = img.image.size
nw, nh = map(int, max_resolution.split('x'))
if w > nw or h > nh:
img.resize(nw, nh)
img = img.resize(nw, nh)
quality = int(ICP('base.image_autoresize_quality', 80))
fn_quality = img.image_quality if is_raw else img.image_base64
values[is_raw and 'raw' or 'datas'] = fn_quality(quality=quality)
except UserError as e:
# Catch error during test where we provide fake image
Expand Down
6 changes: 6 additions & 0 deletions odoo/addons/base/tests/test_ir_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ def test_10_image_autoresize(self):
attach.raw = img_bin
self.assertApproximately(attach.raw, fullsize)

# no resize of gif
self.env['ir.config_parameter'].set_param('base.image_autoresize_max_px', '0x0')
gif_bin = b'GIF89a\x01\x00\x01\x00\x00\xff\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x00;'
attach.raw = gif_bin
self.assertEqual(attach.raw, gif_bin)

def test_11_copy(self):
"""
Copying an attachment preserves the data
Expand Down

0 comments on commit 743ab67

Please sign in to comment.