Skip to content

meringue.thumbnail.drf_fields ¤

MImageField ¤

MImageField(size=None, job_chain=None, **kwargs)

Bases: BaseImageField

Optimize image to represent them

Source code in meringue/thumbnail/drf_fields.py
def __init__(self, size=None, job_chain=None, **kwargs):
    if bool(size) and bool(job_chain):
        msg = "Need to set `size` or `job_chain` attribute (not both)."
        raise Exception(msg)

    elif size:
        self.job_chain = [
            f"s:{size[0]}x{size[1]}",
            "rm:cover",
            "rs:no_increase",
            "resize",
        ]

    elif job_chain:
        self.job_chain = job_chain

    else:
        self.job_chain = []

    super().__init__(**kwargs)

to_representation ¤

to_representation(value)
TODO
  • Add params to get_image for optimize images
Source code in meringue/thumbnail/drf_fields.py
def to_representation(self, value):
    """
    TODO:
        * Add params to get_image for optimize images
    """

    if not value:
        return None

    if os.path.isfile(value.path):
        thumbnail = DefaultThumbnailer(value.path, job_chain=self.job_chain)
        optimized_image_url = thumbnail.get_image(get_format_from_path(value.path)).url
        result = optimized_image_url

    else:
        logger.error(f"File `{value.path}` not found")
        result = _dummyimage([])

    return result

MImageSetField ¤

MImageSetField(
    size=None,
    dimensions=None,
    base_job_chain=None,
    job_chains=None,
    **kwargs
)

Bases: ImageField

Image field - make multiple files in different formats.

Can set size (eg '100x100'), job_chain (arrays of jobs by dimensions) or nothing to optimize images only

Source code in meringue/thumbnail/drf_fields.py
def __init__(
    self,
    size=None,
    dimensions=None,
    base_job_chain=None,
    job_chains=None,
    **kwargs,
):
    if bool(size and dimensions) and bool(job_chains):
        msg = "Need to set `size` and `dimensions` or `job_chains` attribute (not both)."
        raise Exception(msg)

    elif size:
        self.job_chains = {}

        if 1 not in dimensions:
            dimensions.insert(0, 1)

        for dimension in dimensions:
            self.job_chains[dimension] = [
                f"s:{round(size[0]*dimension)}x{round(size[1]*dimension)}",
                *(base_job_chain or ["rm:cover", "resize", "crop"]),
            ]

    elif job_chains:
        self.job_chains = job_chains

    else:
        self.job_chains = {}

    self.spactacular_annotate()
    super().__init__(**kwargs)

to_representation ¤

to_representation(value)

Creates multiple images. Image with format as original image and webp.

TODO
  • Add params to get_image for optimize images
  • In future make av1 image
  • Check enabled webp
Source code in meringue/thumbnail/drf_fields.py
def to_representation(self, value):
    """
    Creates multiple images. Image with format as original image and webp.

    TODO:
        * Add params to get_image for optimize images
        * In future make av1 image
        * Check enabled webp
    """

    if not value:
        return None

    if not os.path.isfile(value.path):
        logger.error(f"File `{value.path}` not found")
        return [
            {
                "url": _dummyimage(self.job_chains[1]),
                "type": "image/png",
            },
        ]

    original_format = get_format_from_path(value.path)
    result = []

    for dimension, job_chain in self.job_chains.items():
        thumbnail = DefaultThumbnailer(value.path, job_chain=job_chain)
        original = thumbnail.get_image(original_format)
        webp_thumbnail = thumbnail.get_image("WEBP")

        if dimension == 1:
            result = [
                {"srcset": f"{webp_thumbnail.url} 1x", "type": webp_thumbnail.mimetype},
                {"srcset": f"{original.url} 1x", "type": original.mimetype},
            ]

        else:
            result[0]["srcset"] = f"{result[0]['srcset']}, {webp_thumbnail.url} {dimension}x"
            result[1]["srcset"] = f"{result[1]['srcset']}, {original.url} {dimension}x"

    return result