Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-09T18:05:42.723Z
Answers.LastActivityDate: 2017-02-09T18:05:42.723Z
Answers.UserDisplayName: Dom Stubbs
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-07T14:51:05.343Z
Answers.LastActivityDate: 2019-11-07T14:51:05.343Z
Answers.UserDisplayName: outline4
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-27T19:26:54.167Z
Answers.Text: Good answer. Would be nice, though, if this was a setting built into Transforms.
Answers.UserDisplayName: cmal
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-27T21:43:14.360Z
Answers.Text: @cmal it looks like a setting to prevent upscaling [is already on "The List"](https://plus.google.com/110881688132908256910/posts/FAzrGAJ99W7).
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-27T18:53:04.030Z
Answers.LastActivityDate: 2014-07-27T21:55:02.133Z
Answers.LastEditDate: 2014-07-27T21:55:02.133Z
Answers.UserDisplayName: Marion Newlevant
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-07-22T15:43:39.727Z
Comments.Text: 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
Comments.UserDisplayName: Lindsey D
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-04-24T15:55:31.933Z
Comments.Text: This feature request has been moved to GitHub: https://github.com/craftcms/cms/issues/844
Comments.UserDisplayName: Katrin
Tags: <assets><image-transforms>
Title: Prevent Asset Transforms from scaling Image past 100%
ThreadText: # 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](http://buildwithcraft.com/docs/templating/assetfilemodel#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](http://twig.sensiolabs.org/doc/tags/macro.html):
  
  ```
  {% 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](http://buildwithcraft.com/docs/templating/assetfilemodel#setTransform) on the image.
  
  If you [define your transforms in the CP](http://buildwithcraft.com/docs/image-transforms#defining-transforms-from-the-control-panel) 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 %}
  
  <img src="{{ utils.imageURL(asset, 'medium') }}">
  
  ```
  
  
  ---
  
  ### Answer by username_4 on 2019-11-07. Score: 2
  
  In Craft 3 there's another way of doing it.  
  Brandon explained it [here](https://github.com/craftcms/cms/issues/844)
  
  ```
  {% set transformDimensions = craft.app.assetTransforms.getTransformByHandle('contentWidth') %}
  {% set transform = {
      width: min(transformDimensions.width, image.width),
      mode: 'fit'
  } %}
  
  <img src="{{ image.url(transform) }}" alt="" />
  
  ```
  
  
  ---
  
  