Update gallery.js

Avoid 'Can\'t set headers after they are sent.' crash on damaged image files.