meringue.thumbnail.drf_fields ¤
MImageField ¤
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 ¤
TODO
- Add params to get_thumbnail for optimize images
Source code in meringue/thumbnail/drf_fields.py
def to_representation(self, value):
"""
TODO:
* Add params to get_thumbnail for optimize images
"""
if not value:
return None
path = Path(value.path)
if path.exists():
thumbnail = DefaultThumbnailer(path, job_chain=self.job_chain)
optimized_image_url = thumbnail.get_thumbnail(get_format_from_path(path)).url
result = optimized_image_url
else:
logger.error(f"File `{path}` not found")
result = _dummyimage([])
return result
MImageSetField ¤
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=(1, 2),
base_job_chain=None,
job_chains=None,
**kwargs,
):
if size and job_chains:
msg = "Need to set 'size' or 'job_chains' attribute (not both)."
raise Exception(msg)
elif size:
if not dimensions:
msg = (
"If you specify the 'sizes' attribute, "
"then the 'dimensions' attribute cannot be empty."
)
raise Exception(msg)
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 ¤
Creates multiple images. Image with format as original image and webp.
TODO
- Add params to get_thumbnail 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_thumbnail for optimize images
* In future make av1 image
* Check enabled webp
"""
if not value:
return None
path = Path(value.path)
if not path.exists():
logger.error(f"File `{path}` not found")
return [
{
"url": _dummyimage(self.job_chains[1]),
"type": "image/png",
},
]
original_format = get_format_from_path(path)
result = []
for dimension, job_chain in self.job_chains.items():
thumbnail = DefaultThumbnailer(path, job_chain=job_chain)
original = thumbnail.get_thumbnail(original_format)
webp_thumbnail = thumbnail.get_thumbnail("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