From d870877967b5debc11d9364ba1572e7aa6956c28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Theys?= Date: Fri, 26 Apr 2019 10:55:21 +0000 Subject: [PATCH] [IMP] tools,base,web,website_*: keep image ratio on resize * = website_partner, website_profile, website_sale, website_slides Before ====== Since commit: 2e3848b394748bf23eff0db0d6d92b9e8c9bf875 The images that are resized have additional borders if the target ratio is different than the image ratio. Those borders are transparent if the image format supports it, and are white otherwise. With that current solution, if the background where the image is displayed is another color than white, it is looking really bad. Moreover, most images are stored resized like this, so it is not even possible to decide if it should have borders or not depending on the context, the original image and ratio is forever lost. It is also inconsistent because if the image is already smaller than the target size then it doesn't include borders. In that case it keeps the original ratio instead of the target ratio. So it isn't even guaranteed that the target ratio is going to be respected. After ===== This commit will solve all of those problems by always keeping the ratio of the original image. This implies the views should be taking care of adding borders when necessary. task-1958000 PR: #31811 --- addons/product/models/product.py | 2 +- addons/web/controllers/main.py | 6 +- addons/web/models/ir_qweb.py | 3 +- addons/web/tests/test_image.py | 4 +- .../views/website_partner_templates.xml | 2 +- addons/website_profile/controllers/main.py | 4 +- .../tests/test_website_sale_image.py | 24 +-- addons/website_slides/controllers/main.py | 4 +- odoo/addons/base/models/image_mixin.py | 2 +- odoo/addons/base/models/res_partner.py | 4 +- odoo/addons/base/tests/test_image.py | 53 +++---- odoo/tools/image.py | 140 ++++-------------- 12 files changed, 75 insertions(+), 173 deletions(-) diff --git a/addons/product/models/product.py b/addons/product/models/product.py index 99a314662dd..4d566f2b100 100644 --- a/addons/product/models/product.py +++ b/addons/product/models/product.py @@ -168,7 +168,7 @@ class ProductProduct(models.Model): for record in self: images = tools.image_get_resized_images(record.image_raw_original, big_name=False) record.image_raw_big = tools.image_get_resized_images(record.image_raw_original, - large_name=False, medium_name=False, small_name=False, preserve_aspect_ratio=True)['image'] + large_name=False, medium_name=False, small_name=False)['image'] record.image_raw_large = images['image_large'] record.image_raw_medium = images['image_medium'] record.image_raw_small = images['image_small'] diff --git a/addons/web/controllers/main.py b/addons/web/controllers/main.py index dc345e047ed..226d5347e84 100644 --- a/addons/web/controllers/main.py +++ b/addons/web/controllers/main.py @@ -1023,8 +1023,8 @@ class Binary(http.Controller): '/web/image/-/x/'], type='http', auth="public") def content_image(self, xmlid=None, model='ir.attachment', id=None, field='datas', filename_field='datas_fname', unique=None, filename=None, mimetype=None, - download=None, width=0, height=0, crop=False, access_token=None, avoid_if_small=False, - upper_limit=False, placeholder='placeholder.png', **kw): + download=None, width=0, height=0, crop=False, access_token=None, + placeholder='placeholder.png', **kwargs): status, headers, image_base64 = request.env['ir.http'].binary_content( xmlid=xmlid, model=model, id=id, field=field, unique=unique, filename=filename, filename_field=filename_field, download=download, mimetype=mimetype, @@ -1041,7 +1041,7 @@ class Binary(http.Controller): image_base64 = getattr(odoo.tools, 'image_resize_image_%s' % suffix)(image_base64) image_base64 = limited_image_resize( - image_base64, width=width, height=height, crop=crop, upper_limit=upper_limit, avoid_if_small=avoid_if_small) + image_base64, width=width, height=height, crop=crop) content = base64.b64decode(image_base64) headers.append(('Content-Length', len(content))) diff --git a/addons/web/models/ir_qweb.py b/addons/web/models/ir_qweb.py index d1d792e8f54..e8a966647e3 100644 --- a/addons/web/models/ir_qweb.py +++ b/addons/web/models/ir_qweb.py @@ -45,7 +45,6 @@ class Image(models.AbstractModel): sha = hashlib.sha1(str(getattr(record, '__last_update')).encode('utf-8')).hexdigest()[0:7] max_size = '' if max_size is None else '/%s' % max_size - avoid_if_small = '&avoid_if_small=true' if options.get('avoid_if_small') else '' if options.get('filename-field') and getattr(record, options['filename-field'], None): filename = record[options['filename-field']] @@ -54,7 +53,7 @@ class Image(models.AbstractModel): else: filename = record.display_name - src = '/web/image/%s/%s/%s%s/%s?unique=%s%s' % (record._name, record.id, options.get('preview_image', field_name), max_size, url_quote(filename), sha, avoid_if_small) + src = '/web/image/%s/%s/%s%s/%s?unique=%s' % (record._name, record.id, options.get('preview_image', field_name), max_size, url_quote(filename), sha) if options.get('alt-field') and getattr(record, options['alt-field'], None): alt = escape(record[options['alt-field']]) diff --git a/addons/web/tests/test_image.py b/addons/web/tests/test_image.py index e17073d0fb7..1e4b0c3c5b5 100644 --- a/addons/web/tests/test_image.py +++ b/addons/web/tests/test_image.py @@ -13,10 +13,10 @@ class TestImage(HttpCase): """The goal of this test is to make sure the placeholder image is resized appropriately depending on the given URL parameters.""" - # CASE: resize placeholder to given size + # CASE: resize placeholder, given size but original ratio is always kept response = self.url_open('/web/image/0/200x150') image = Image.open(io.BytesIO(response.content)) - self.assertEqual(image.size, (200, 150)) + self.assertEqual(image.size, (150, 150)) # CASE: resize placeholder to small response = self.url_open('/web/image/fake/0/image_small') diff --git a/addons/website_partner/views/website_partner_templates.xml b/addons/website_partner/views/website_partner_templates.xml index c8682008619..2f5c58b1dee 100644 --- a/addons/website_partner/views/website_partner_templates.xml +++ b/addons/website_partner/views/website_partner_templates.xml @@ -17,7 +17,7 @@