How to do it...

In Odoo, all image utilities are placed in the odoo/tools/image.py file. You need to add an import statement to use these image utilities:

from odoo import tools

The image_resize_images() method of the image utility helps you manage three different image sizes. To do this, you need to add three binary fields to store the different-sized images:

image = fields.Binary(attachment=True) # 1024x1024px
image_medium = fields.Binary(attachment=True) # 128x128px
image_small = fields.Binary(attachment=True) #64x64px

When a user uploads an image from the form view, it is uploaded to full-size. We need to override the create and write methods to convert the image into three different sizes:

@api.model
def create(self, vals):
tools.image_resize_images(vals)
return super().create(vals)

@api.multi
def write(self, vals):
tools.image_resize_images(vals)
return super().write(vals)

This will resize uploaded images and save them in the image, image_medium, and image_small fields.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset