Skip to Content
Menu
This question has been flagged
2 Replies
3985 Views

Hi all,

I have been looking for a block of code that helps me to "resize a dynamic image" on the fly for both create and write methos with no luck.

All the code I found is for later versions and actually not performing well on Odoo 12 (or at least I am not changing the code as I should).

I found this code block under "fleet" management app on Odoo 12 which seems to be a very simple code to understand.

class FleetVehicleModelBrand(models.Model):
    _name = 'fleet.vehicle.model.brand'
    _description = 'Brand of the vehicle'
    _order = 'name asc'
    name = fields.Char('Make', required=True)
    image = fields.Binary("Logo", attachment=True,
        help="This field holds the image used as logo for the brand, limited to 1024x1024px.")
    image_medium = fields.Binary("Medium-sized image", attachment=True,
        help="Medium-sized logo of the brand. It is automatically "
             "resized as a 128x128px image, with aspect ratio preserved. "
             "Use this field in form views or some kanban views.")
    image_small = fields.Binary("Small-sized image", attachment=True,
        help="Small-sized logo of the brand. It is automatically "
             "resized as a 64x64px image, with aspect ratio preserved. "
             "Use this field anywhere a small image is required.")

    @api.model_create_multi
    def create(self, vals_list):
        for vals in vals_list:
            tools.image_resize_images(vals)
        return super(FleetVehicleModelBrand, self).create(vals_list)

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


The code above seems to be very easy to undertand but:

On the class FleeetVehicleModelBrand, I have 3 image type fields, named image, image_medium and image_small.

On both create and write methods, I do not see any code that refers to those images itself. That is, I don't see any reference to the type of image that has to be resized.

Questions (UPDATED):

Note: I have updated the question(s) based on what I have done after the first post.

I found that the images have to be named exactly "image, image_medium and image_small) for the resizing work. Otherwise, fields on image type with different names will not be resized.

1. What if I have an image type field with a differente name? For instance, "student_picture"?

How can I resize this image?

I really hope you can help me with this issue because the code found on other modules (customer, products) are harder to understand and to adapt to my needs.

I need to resize images (to 1024x1024) using odoo standard tools on both create and write methods, for more than one field of type image.

Thank you in advance

Regards

Avatar
Discard
Author

Great. Received -1 as answer for this question that I believe to be legitimate.

Best Answer

Hi,

to understand what is happening you should go the Odoo tools folders – and look at the invoked method image_resize_images: https://github.com/odoo/odoo/blob/12.0/odoo/tools/image.py.

As you can see here the method relies upon the exact fields names (it is done to apply the method simply in all models). Thus, in case you want your own field with the resized image, you should introduce your own method. You can use another method from the same file – image_resize_image. An example (not tested, just for common understanding):


from odoo.tools import image
@api.multi
@api.depends('image')
def _compute_myfield(self):
    for record in self:
        record.myfield = image.image_resize_image(record.image, size=(1024, 1024)) 
myfield = fields.Binary(compute=_compute_myfield, store=True,)

It is probable that before converting you should encode the 'image' field to ascii as it is done in the method image_get_resized_images.

Avatar
Discard
Author

Thank you @Odoo Tools,

It was exactly what I needed. Perhaps using record.myfield = image.image_resize_image(base64.b64encode(record.image), size=(1024, 1024)) will allow me to encode the image. Will test it. Thank you very much once again

Related Posts Replies Views Activity
4
Feb 25
15475
3
Oct 23
7576
1
Sep 23
2720
1
May 23
1661
2
Apr 23
2128