Skip to main content

Generate and store thumbnail for images

Project description

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.

Table of contents

Use Cases / Context

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.

Usage

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')

Changelog

16.0.1.0.1 (2023-10-04)

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)

Bug Tracker

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.

Credits

Authors

  • ACSONE SA/NV

Contributors

Other credits

The development of this module has been financially supported by:

Maintainers

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

File details

Details for the file odoo_addon_fs_image_thumbnail-17.0.1.0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for odoo_addon_fs_image_thumbnail-17.0.1.0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 09078db31859837abf9b8019f4cae64e732c4da9969af9e58accd7f4a3a15bc6
MD5 fdb9e16665b4b4a86f44a963f5c9be09
BLAKE2b-256 2ec71d4f9c75e328da81f254da40dd753877b4c754e24cda02b20400a8c52807

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page