5

With Craft's image transforms used to resize images or generate thumbnails, can you prevent images from being upscaled? There's often cases where it makes no sense to increase the dimensions of an image.

It would be great if there was a setting for this, but I can't find one documented.

Otherwise, is the solution to fetch the image size in Twig and then conditionally apply the image transform?

8

As found here, this actually seems to be a requested feature that is not yet implemented. However, in the link given, Brandon also supplied a workaround for the time being:

{% set transform = {
width: min(150, image.width),
height: min(100, image.height)
} %}

<img src="{{ image.getUrl(transform) }}">
2

If you want to work with transforms you've created in the backend you can use this:

{% set transformDimensions = craft.app.assetTransforms.getTransformByHandle('contentWidth') %}
{% set transform = {
    width: min(transformDimensions.width, image.width),
    mode: 'fit'
} %}

<img src="{{ image.url(transform) }}" alt="" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.