Created
October 8, 2016 09:07
-
-
Save engram-design/8a31f69ac31766dda48d96712726d503 to your computer and use it in GitHub Desktop.
Macro to prevent upscaling - something Craft doesn't support just yet...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# Usage #} | |
{% set transform = { width: 700, height: 247, mode: 'crop' } %} | |
<img class="banner" src="{{ _self.transform(entry.bannerImage.first, transform) }}"> | |
{# Macro to prevent upscaling - something Craft doesn't support just yet... #} | |
{% macro transform(image, transform) %} | |
{% spaceless %} | |
{% set originalWidth = image.getWidth(false) %} | |
{% set transformedWidth = image.getWidth(transform) %} | |
{% set originalHeight = image.getHeight(false) %} | |
{% set transformedHeight = image.getHeight(transform) %} | |
{% if transformedWidth > originalWidth %} | |
{% set transform = transform | merge({ width: originalWidth }) %} | |
{% endif %} | |
{% if transformedHeight > originalHeight %} | |
{% set transform = transform | merge({ height: originalHeight }) %} | |
{% endif %} | |
{{ image.url(transform) }} | |
{% endspaceless %} | |
{% endmacro %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment