<p>I do all images through a macro, that substitutes in our logo if an image happens to be missing.</p>

<pre><code>{% macro imageTransform(image, transform, caption, extraHTML) %}
  {# Fallback to our logo if the image is null... #}
  {% if not image|length %}
    {% set image = craft.assets.id('84').first() %}
  {% endif %}
  &lt;img src="{{ image.getUrl( transform ) }}" width="{{ image.getWidth( transform ) }}" height="{{ image.getHeight( transform ) }}" title="{{ caption|default('') }}" alt="{{ caption|default('') }}"{% if extraHTML %} {{ extraHTML }}{% endif %}&gt;    
{% endmacro %}
</code></pre>

<p>We have a more complicated version for responsive images but that's the basic idea...</p>

<p>Also, to find things using assets, although I have never used it, I hear this is good:
<a href="https://github.com/iainurquhart/craft_introvert">https://github.com/iainurquhart/craft_introvert</a></p>

CC BY-SA 3.0
2015-10-15T10:23:48.047Z
Yep, never use `{{ entry.myAsset.first().getUrl() }}`, as you can never know if `first()` actually returns an asset model.
carlcs
CC BY-SA 3.0
2015-10-15T17:06:14.320Z
{{ entry.myAsset.first().getUrl() }} isn't a problem if you wrap it in an if that detects length... except that it will still fail if the image is missing but the database says it should be there.
Matt Wilcox
CC BY-SA 3.0
2015-10-15T17:49:58.760Z
Matt is spot on, the problem is really when "the database says it should be there". Jeremy's solution is great for checking whether the database thinks an image exists, but Craft still fails catastrophically **when the DB thinks the image exists, but the actual file does not exist.** I bump into this frequently when I pull a copy of a production DB into my local environment.
Lindsey D
CC BY-SA 3.0
2017-05-01T12:22:24.580Z
Indeed, like lIndsey I too run into this, any way this could be worked around?
erwinheiser
CC BY-SA 3.0
2015-10-15T10:15:15.960Z
2015-10-15T10:15:15.960Z
Jeremy Daalder
<p>Craft 3.5 introduced a new config setting - <code>brokenImagePath</code> to deal with exactly this scenario.</p>
<p><a href="https://craftcms.com/docs/3.x/config/config-settings.html#brokenimagepath" rel="nofollow noreferrer">https://craftcms.com/docs/3.x/config/config-settings.html#brokenimagepath</a></p>

CC BY-SA 4.0
2020-09-11T10:14:56.270Z
2020-09-11T10:14:56.270Z
James Smith
<p>If it's any help I made a quick plugin to handle this by way of a Twig extension. <a href="https://github.com/moresoda/existential" rel="nofollow noreferrer">https://github.com/moresoda/existential</a></p>

<p>You can use it to test for the existence of a file with</p>

<pre><code>{% if asset.getUrl() | fileExists %}
</code></pre>

CC BY-SA 4.0
2018-09-18T17:36:40.403Z
2018-09-18T17:36:40.403Z
foamcow
<p>How Craft reacts to missing images is one of the very few places where I find Craft less than ideal. </p>

<p>TL:DR; </p>

<ul>
<li><strong>Is there a way to make missing images <em>not</em> become a fatal error?</strong></li>
<li><strong>How do you find elements that are referencing the missing image?</strong></li>
</ul>

<p>Long version:</p>

<p>There are a number of ways images can be missing, for example:</p>

<ul>
<li>Our workflow involves developing locally, then putting up to staging for clients to add content. Sometimes we need to then do more local development, local and stage both share the same database... but not images.

<ul>
<li>An image is simply corrupt, due to transfer or disk failure (it happens)</li>
<li>A filesystem is reverted to a previous state but the database is not</li>
<li>A live server dies and a project is restored to a new machine from SVN/GIT, but the assets are not in version control and therefore not available</li>
<li>Getting hold of the images on local isn't possible temporarily because you're working on a restricted internet connection; like on a plane.</li>
</ul></li>
</ul>

<p>Any of these result in instances of 'blahblah.jpg could not be found' in Dev mode or Internal Server Error in production. Sometimes those images actually get lost; they just don't exist any more; but they're in the database. So...</p>

<p>How do you find what element is referencing these lost files? Besides manually trawling through entries, related entries, etc? Crafts error gives no useful information about that, and rebuilding asset indexes doesn't seem to make Craft aware that the image isn't available anymore.</p>

<p>And...</p>

<p>Is there an option to just ignore these types of error - it's not helpful in production to have a whole page 'internal server error' just because a single image referenced on the page is missing from the file system for whatever reason. A missing image is not a throw-the-baby-out-with-the-bathwater problem.</p>

<assets>
Dealing with missing images
# Dealing with missing images

(Asked by: username_0 on 2015-10-15)

How Craft reacts to missing images is one of the very few places where I find Craft less than ideal.

TL:DR;

* **Is there a way to make missing images *not* become a fatal error?**
* **How do you find elements that are referencing the missing image?**

Long version:

There are a number of ways images can be missing, for example:

* Our workflow involves developing locally, then putting up to staging for clients to add content. Sometimes we need to then do more local development, local and stage both share the same database... but not images.
  + An image is simply corrupt, due to transfer or disk failure (it happens)
  + A filesystem is reverted to a previous state but the database is not
  + A live server dies and a project is restored to a new machine from SVN/GIT, but the assets are not in version control and therefore not available
  + Getting hold of the images on local isn't possible temporarily because you're working on a restricted internet connection; like on a plane.

Any of these result in instances of 'blahblah.jpg could not be found' in Dev mode or Internal Server Error in production. Sometimes those images actually get lost; they just don't exist any more; but they're in the database. So...

How do you find what element is referencing these lost files? Besides manually trawling through entries, related entries, etc? Crafts error gives no useful information about that, and rebuilding asset indexes doesn't seem to make Craft aware that the image isn't available anymore.

And...

Is there an option to just ignore these types of error - it's not helpful in production to have a whole page 'internal server error' just because a single image referenced on the page is missing from the file system for whatever reason. A missing image is not a throw-the-baby-out-with-the-bathwater problem.

**Tags:** assets


---

## 3 answers

### Answer by username_1 on 2015-10-15. Score: 6

I do all images through a macro, that substitutes in our logo if an image happens to be missing.
{% macro imageTransform(image, transform, caption, extraHTML) %}
  {# Fallback to our logo if the image is null... #}
  {% if not image|length %}
    {% set image = craft.assets.id('84').first() %}
  {% endif %}
      
{% endmacro %}

We have a more complicated version for responsive images but that's the basic idea...
Also, to find things using assets, although I have never used it, I hear this is good:
https://github.com/iainurquhart/craft_introvert

> **Comments:**
> **username_2**: Yep, never use `{{ entry.myAsset.first().getUrl() }}`, as you can never know if `first()` actually returns an asset model.  
> **username_0**: {{ entry.myAsset.first().getUrl() }} isn't a problem if you wrap it in an if that detects length... except that it will still fail if the image is missing but the database says it should be there.  
> **username_3**: Matt is spot on, the problem is really when "the database says it should be there". Jeremy's solution is great for checking whether the database thinks an image exists, but Craft still fails catastrophically **when the DB thinks the image exists, but the actual file does not exist.** I bump into this frequently when I pull a copy of a production DB into my local environment.  
> **username_4**: Indeed, like lIndsey I too run into this, any way this could be worked around?  

---

### Answer by username_5 on 2020-09-11. Score: 3

Craft 3.5 introduced a new config setting - `brokenImagePath` to deal with exactly this scenario.

<https://craftcms.com/docs/3.x/config/config-settings.html#brokenimagepath>


---

### Answer by username_6 on 2018-09-18. Score: 1

If it's any help I made a quick plugin to handle this by way of a Twig extension. https://github.com/moresoda/existential
You can use it to test for the existence of a file with
{% if asset.getUrl() | fileExists %}


---

