Skip to content

Latest commit

 

History

History
196 lines (141 loc) · 7.14 KB

README.rst

File metadata and controls

196 lines (141 loc) · 7.14 KB

Fs Image Thumbnail

Alpha License: AGPL-3 OCA/storage Translate me on Weblate Try me on Runboat

This module extends the fs_image addon to support the creation and the storage of thumbnails for images. This module is a technical module and is not meant to be installed by end-users. It only provides a mixin to be used by other modules and a model to store the thumbnails.

Important

This is an alpha version, the data model and design can change at any time without warning. Only for development or testing purpose, do not use in production. More details on development status

Table of contents

In some specific cases you may need to generate and store thumbnails of images in Odoo. This is the case for example when you want to provide image in specific sizes for a website or a mobile application.

This module provides a generic way to generate thumbnails of images and store them in a specific filesystem storage. Indeed, you could need to store the thumbnails in a different storage than the original image (eg: store the thumbnails in a CDN) to make sure the thumbnails are served quickly when requested by an external application and to avoid to expose the original image storage.

This module uses the fs_image module to store the thumbnails in a filesystem storage.

The shopinvader_product_image addon uses this module to generate and store the thumbnails of the images of the products and categories to be accessible by the website.

This addon provides a convenient way to get and create if not exists image thumbnails. All the logic is implemented by the abstract model fs.image.thumbnail.mixin. The main method is get_or_create_thumbnails which accepts a FSImageValue instance, a list of thumbnail sizes and a base name.

When the method is called, it will check if the thumbnail exists for the given sizes and base name. If not, it will create it.

The fs.thumbnail model provided by this addon is a concrete implementation of the abstract model fs.image.thumbnail.mixin. The motivation to implement all the logic in an abstract model is to allow developers to create their own thumbnail models. This could be useful if you want to store the thumbnails in a different storage since you can specify the storage to use by model on the fs.storage form view.

Creating / retrieving thumbnails is as simple as:

from odoo.addons.fs_image.fields import FSImageValue

# create an attachment with a image file
attachment = self.env['ir.attachment'].create({
    'name': 'test',
    'datas': base64.b64encode(open('test.png', 'rb').read()),
    'datas_fname': 'test.png',
})

# create a FSImageValue instance for the attachment
image_value = FSImageValue(attachment)

# get or create the thumbnails
thumbnails = self.env['fs.thumbnail'].get_or_create_thumbnails(
    image_value, [(800,600), (400, 200)], 'my base name')

If you've a model with a FSImage field, the call to get_or_create_thumbnails is even simpler:

from odoo import models
from odoo.addons.fs_image.fields import FSImage

class MyModel(models.Model):
    _name = 'my.model'

    image = FSImage('Image')

my_record = cls.env['my.model'].create({
    'image': open('test.png', 'rb'),
})

# get or create the thumbnails
thumbnails = record.image.get_or_create_thumbnails(my_record.image,
    [(800,600), (400, 200)], 'my base name')

Bugfixes

  • The call to the method get_or_create_thumbnails on the fs.image.thumbnail.mixin class returns now an ordered dictionary where the key is the original image and the value is a recordset of thumbnail images. The order of the dict is the order of the images passed to the method. This ensures that when you process the result of the method you can be sure that the order of the images is the same as the order of the images passed to the method. (#282)

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed feedback.

Do not contact contributors directly about support or help with technical issues.

  • ACSONE SA/NV

The development of this module has been financially supported by:

This module is maintained by the OCA.

Odoo Community Association

OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.

Current maintainer:

lmignon

This module is part of the OCA/storage project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.