Modify media file markup output

WordPress has a filter image_send_to_editor which lets you modify the html that is generated by the media editor. Usage:

add_filter ('image_send_to_editor', 'wpse264886_filter_image', 10, 8);
function wpse264886_filter_image ($html, $id, $caption, $title, $align, $url, $size, $alt) {
  $html="<span> ... </span>" . $html;
  return $html;
  }

This will insert the spans always, however, not only when there is a link around the image. If you want the latter, you’d have to filter the content by doing a search and replace, finding all links on images and then insert the html. I’m not very good with regex and haven’t been able to test it, but it would be something like this:

add_filter ('the_content', 'wpse264886_content_insert_html', 10, 1);
function wpse264886_content_insert_html ($content) {
  $html="<div> ... </div>"
  $content = preg_replace('/(<a.*?)><img/', '$1>' . $html . '<img', $content);
  return $content;
  }