Skip to content

Instantly share code, notes, and snippets.

@engram-design
Created October 8, 2016 09:07
Show Gist options
  • Save engram-design/8a31f69ac31766dda48d96712726d503 to your computer and use it in GitHub Desktop.
Save engram-design/8a31f69ac31766dda48d96712726d503 to your computer and use it in GitHub Desktop.
Macro to prevent upscaling - something Craft doesn't support just yet...
{# 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