[IMP] base: resize partner image with actual aspect ratio

Purpose of this commit is to finally stop adding extra padding to
website logo. Indeed not giving square logos lead to padding added
to images, leading to not so beautiful logo that makes people sad.

Before this commit
 * partner's image was resized to square 1024x1024. Extra padding was added
   if user provided rectangle image;
 * due to this website logo resized to square when rectangle logo provided;

After this commit,
 * sizes dict can be passed in image resizer of tools;
 * e.g. tools.image_resize_images(vals, sizes={'image': (512, 512),
   'image_small': (128, 128)})
 * image field in partner will resize with max width 1024 and height based
   on given image ratio (still image_medium, image_small will be default
   square images);
 * now website logo will respect actual aspect ratio

This commit is related to task ID 31994 . Future commits should probably
clean all that mess, but at least it seems we have something that works.
This commit is contained in:
Sagar Ahirrao
2018-03-15 11:31:06 +01:00
committed by Thibault Delavallée
parent 964e16a7f5
commit 05e02059f0
4 changed files with 16 additions and 13 deletions
@@ -89,8 +89,8 @@
</t>
<div class="media">
<a class="pull-left" t-attf-href="/partners/#{slug(partner)}?#{current_grade and 'grade_id=%s&amp;' % current_grade.id}#{current_country and 'country_id=%s' % current_country.id}"
t-field="partner.image"
t-options='{"widget": "image", "class": "media-object", "max_width": 128}'
t-field="partner.image_medium"
t-options='{"widget": "image", "class": "media-object"}'
></a>
<div class="media-body o_partner_body" style="min-height: 64px;">
<a class="media-heading" t-attf-href="/partners/#{slug(partner)}?#{current_grade and 'grade_id=%s&amp;' % current_grade.id}#{current_country and 'country_id=%s' % current_country.id}">
@@ -17,7 +17,7 @@
<template id="partner_detail" name="Partner Details">
<h1 class="col-md-12 text-center" id="partner_name" t-field="partner.display_name"/>
<div class="col-md-4">
<div t-field="partner.image" t-options='{"widget": "image", "class": "center-block mb16"}'/>
<div t-field="partner.image" t-options='{"widget": "image", "class": "center-block mb16", "max_width": 512, "avoid_if_small": True}'/>
<address class="well">
<div t-field="partner.self" t-options='{
"widget": "contact",
+2 -2
View File
@@ -501,7 +501,7 @@ class Partner(models.Model):
companies = set(user.company_id for user in partner.user_ids)
if len(companies) > 1 or company not in companies:
raise UserError(_("You can not change the company as the partner/user has multiple user linked with different companies."))
tools.image_resize_images(vals)
tools.image_resize_images(vals, sizes={'image': (1024, None)})
result = True
# To write in SUPERUSER on field is_company and avoid access rights problems.
@@ -525,7 +525,7 @@ class Partner(models.Model):
# cannot be easily performed if default images are in the way
if not vals.get('image'):
vals['image'] = self._get_default_image(vals.get('type'), vals.get('is_company'), vals.get('parent_id'))
tools.image_resize_images(vals)
tools.image_resize_images(vals, sizes={'image': (1024, None)})
partner = super(Partner, self).create(vals)
partner._fields_sync(vals)
partner._handle_first_contact_creation()
+11 -8
View File
@@ -234,7 +234,7 @@ def image_colorize(original, randomize=True, color=(255, 255, 255)):
def image_get_resized_images(base64_source, return_big=False, return_medium=True, return_small=True,
big_name='image', medium_name='image_medium', small_name='image_small',
avoid_resize_big=True, avoid_resize_medium=False, avoid_resize_small=False):
avoid_resize_big=True, avoid_resize_medium=False, avoid_resize_small=False, sizes={}):
""" Standard tool function that returns a dictionary containing the
big, medium and small versions of the source image. This function
is meant to be used for the methods of functional fields for
@@ -255,33 +255,36 @@ def image_get_resized_images(base64_source, return_big=False, return_medium=True
previous parameters.
"""
return_dict = dict()
size_big = sizes.get(big_name, (1024, 1024))
size_medium = sizes.get(medium_name, (128, 128))
size_small = sizes.get(small_name, (64, 64))
if isinstance(base64_source, pycompat.text_type):
base64_source = base64_source.encode('ascii')
if return_big:
return_dict[big_name] = image_resize_image_big(base64_source, avoid_if_small=avoid_resize_big)
return_dict[big_name] = image_resize_image_big(base64_source, avoid_if_small=avoid_resize_big, size=size_big)
if return_medium:
return_dict[medium_name] = image_resize_image_medium(base64_source, avoid_if_small=avoid_resize_medium)
return_dict[medium_name] = image_resize_image_medium(base64_source, avoid_if_small=avoid_resize_medium, size=size_medium)
if return_small:
return_dict[small_name] = image_resize_image_small(base64_source, avoid_if_small=avoid_resize_small)
return_dict[small_name] = image_resize_image_small(base64_source, avoid_if_small=avoid_resize_small, size=size_small)
return return_dict
def image_resize_images(vals, big_name='image', medium_name='image_medium', small_name='image_small'):
def image_resize_images(vals, big_name='image', medium_name='image_medium', small_name='image_small', sizes={}):
""" Update ``vals`` with image fields resized as expected. """
if vals.get(big_name):
vals.update(image_get_resized_images(vals[big_name],
return_big=True, return_medium=True, return_small=True,
big_name=big_name, medium_name=medium_name, small_name=small_name,
avoid_resize_big=True, avoid_resize_medium=False, avoid_resize_small=False))
avoid_resize_big=True, avoid_resize_medium=False, avoid_resize_small=False, sizes=sizes))
elif vals.get(medium_name):
vals.update(image_get_resized_images(vals[medium_name],
return_big=True, return_medium=True, return_small=True,
big_name=big_name, medium_name=medium_name, small_name=small_name,
avoid_resize_big=True, avoid_resize_medium=True, avoid_resize_small=False))
avoid_resize_big=True, avoid_resize_medium=True, avoid_resize_small=False, sizes=sizes))
elif vals.get(small_name):
vals.update(image_get_resized_images(vals[small_name],
return_big=True, return_medium=True, return_small=True,
big_name=big_name, medium_name=medium_name, small_name=small_name,
avoid_resize_big=True, avoid_resize_medium=True, avoid_resize_small=True))
avoid_resize_big=True, avoid_resize_medium=True, avoid_resize_small=True, sizes=sizes))
elif big_name in vals or medium_name in vals or small_name in vals:
vals[big_name] = vals[medium_name] = vals[small_name] = False