7

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.

3 Answers 3

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 %}
  <img src="{{ image.getUrl( transform ) }}" width="{{ image.getWidth( transform ) }}" height="{{ image.getHeight( transform ) }}" title="{{ caption|default('') }}" alt="{{ caption|default('') }}"{% if extraHTML %} {{ extraHTML }}{% 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

4
  • Yep, never use {{ entry.myAsset.first().getUrl() }}, as you can never know if first() actually returns an asset model.
    – carlcs
    Oct 15, 2015 at 10:23
  • {{ 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. Oct 15, 2015 at 17:06
  • 4
    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
    Oct 15, 2015 at 17:49
  • Indeed, like lIndsey I too run into this, any way this could be worked around? May 1, 2017 at 12:22
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

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 %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.