<p>Here's a slight revision that checks widths and heights and ensures that the output has no white space:</p>

<pre><code>{%- macro imageURL(image, transform) -%}
    {% spaceless %}
        {% if image.getWidth &gt; image.getWidth(transform) or image.getHeight &gt; image.getHeight(transform) %}
            {{ image.getUrl(transform) }}
        {% else %}
            {{ image.getUrl }}
        {% endif %}
    {% endspaceless %}
{%- endmacro -%}
</code></pre>

<p>Template usage:</p>

<pre><code>{% import "_macros" as utils %}

&lt;img src="{{ utils.imageURL(asset, 'medium') }}"&gt;
</code></pre>

CC BY-SA 3.0
2017-02-09T18:05:42.723Z
2017-02-09T18:05:42.723Z
Dom Stubbs
<p>In Craft 3 there's another way of doing it.<br>
Brandon explained it <a href="https://github.com/craftcms/cms/issues/844" rel="nofollow noreferrer">here</a></p>

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

&lt;img src="{{ image.url(transform) }}" alt="" /&gt;
</code></pre>

CC BY-SA 4.0
2019-11-07T14:51:05.343Z
2019-11-07T14:51:05.343Z
outline4
<p>Yes.</p>

<p><a href="http://buildwithcraft.com/docs/templating/assetfilemodel#getWidth">getWidth</a> will give you an image's width: <code>{% set originalWidth = image.getWidth(false) %}</code>. You want to apply the transform if <code>desiredWidth</code> is less than <code>originalWidth</code>. I would write a <a href="http://twig.sensiolabs.org/doc/tags/macro.html">twig macro</a>:</p>

<pre><code>{% macro transform(image, desiredWidth) %}
  {% set originalWidth = image.getWidth(false) %}
  {% if desiredWidth &lt; originalWidth %}
    {{ image.getUrl({mode: 'fit', width: desiredWidth}) }}
  {% else %}
    {{ image.getUrl() }}
  {% endif %}
{% endmacro %}
</code></pre>

<p>You could also do the same test, and then conditionally <a href="http://buildwithcraft.com/docs/templating/assetfilemodel#setTransform">setTransform</a> on the image.</p>

<p>If you <a href="http://buildwithcraft.com/docs/image-transforms#defining-transforms-from-the-control-panel">define your transforms in the CP</a> the macro would be like this:</p>

<pre><code>{% macro transform(image, transformHandle) %}
  {% set originalWidth = image.getWidth(false) %}
  {% set transformedWidth = image.getWidth(transformHandle) %}
  {% if transformedWidth &lt; originalWidth %}
    {{ image.getUrl(transformHandle) }}
  {% else %}
    {{ image.getUrl() }}
  {% endif %}
{% endmacro %}
</code></pre>

CC BY-SA 3.0
2014-07-27T19:26:54.167Z
Good answer. Would be nice, though, if this was a setting built into Transforms.
cmal
CC BY-SA 3.0
2014-07-27T21:43:14.360Z
@cmal it looks like a setting to prevent upscaling [is already on "The List"](https://plus.google.com/110881688132908256910/posts/FAzrGAJ99W7).
carlcs
CC BY-SA 3.0
2014-07-27T18:53:04.030Z
2014-07-27T21:55:02.133Z
2014-07-27T21:55:02.133Z
Marion Newlevant
<p>There is almost never a reason why I would want an image to be scaled past 100% by a transform. This usually results in an image being looking worse than it would if scaled by the browser. Is there a way to tell Craft not to scale an image past 100% of its original size, even if the transform width is larger?</p>

CC BY-SA 3.0
2016-07-22T15:43:39.727Z
Please vote for this feature request to prevent transforms from scaling up! http://feedback.craftcms.com/forums/285221-feature-requests/suggestions/10007034-image-transforms-have-option-not-to-scale-up
Lindsey D
CC BY-SA 3.0
2017-04-24T15:55:31.933Z
This feature request has been moved to GitHub: https://github.com/craftcms/cms/issues/844
Katrin
<assets><image-transforms>
Prevent Asset Transforms from scaling Image past 100%
# Prevent Asset Transforms from scaling Image past 100%

(Asked by: username_0 on 2014-07-27)

There is almost never a reason why I would want an image to be scaled past 100% by a transform. This usually results in an image being looking worse than it would if scaled by the browser. Is there a way to tell Craft not to scale an image past 100% of its original size, even if the transform width is larger?

**Tags:** assets, image-transforms

> **Comments:**
> **username_1**: Please vote for this feature request to prevent transforms from scaling up! http://feedback.craftcms.com/forums/285221-feature-requests/suggestions/10007034-image-transforms-have-option-not-to-scale-up  
> **username_2**: This feature request has been moved to GitHub: https://github.com/craftcms/cms/issues/844  

---

## 3 answers

### Answer by username_5 on 2014-07-27. Score: 10 | Accepted answer

Yes.
getWidth will give you an image's width: {% set originalWidth = image.getWidth(false) %}. You want to apply the transform if desiredWidth is less than originalWidth. I would write a twig macro:
{% macro transform(image, desiredWidth) %}
  {% set originalWidth = image.getWidth(false) %}
  {% if desiredWidth < originalWidth %}
    {{ image.getUrl({mode: 'fit', width: desiredWidth}) }}
  {% else %}
    {{ image.getUrl() }}
  {% endif %}
{% endmacro %}

You could also do the same test, and then conditionally setTransform on the image.
If you define your transforms in the CP the macro would be like this:
{% macro transform(image, transformHandle) %}
  {% set originalWidth = image.getWidth(false) %}
  {% set transformedWidth = image.getWidth(transformHandle) %}
  {% if transformedWidth < originalWidth %}
    {{ image.getUrl(transformHandle) }}
  {% else %}
    {{ image.getUrl() }}
  {% endif %}
{% endmacro %}

> **Comments:**
> **username_0**: Good answer. Would be nice, though, if this was a setting built into Transforms.  
> **username_6**: @username_0 it looks like a setting to prevent upscaling [is already on "The List"](https://plus.google.com/110881688132908256910/posts/FAzrGAJ99W7).  

---

### Answer by username_3 on 2017-02-09. Score: 3

Here's a slight revision that checks widths and heights and ensures that the output has no white space:
{%- macro imageURL(image, transform) -%}
    {% spaceless %}
        {% if image.getWidth > image.getWidth(transform) or image.getHeight > image.getHeight(transform) %}
            {{ image.getUrl(transform) }}
        {% else %}
            {{ image.getUrl }}
        {% endif %}
    {% endspaceless %}
{%- endmacro -%}

Template usage:
{% import "_macros" as utils %}


---

### Answer by username_4 on 2019-11-07. Score: 2

In Craft 3 there's another way of doing it.
Brandon explained it here
{% set transformDimensions = craft.app.assetTransforms.getTransformByHandle('contentWidth') %}
{% set transform = {
    width: min(transformDimensions.width, image.width),
    mode: 'fit'
} %}


---

