Disabling post images for not logged in users

You can use PHP strip_tags and hook to the_content filter by checking if user is logged in.

add this to your functions.php file

add_filter( 'the_content', 'wpse_remove_img' );
function wpse_remove_img( $content ){

  if( ! is_user_logged_in() ){

    // You need to specify which tag you want to keep here it's p, a, h2,span, div.
    // Adjust accordingly (you might want to add other or remove all)
    $content = strip_tags($content, '<p><a><h2><span><div>'); 


  }

  return $content;

}

EDIT

If you want to replace the image instead, I would use PHP DOMDocument to do the search and replace.

Note that some issues with text encoding can be experienced or warnings for some HTML5 tags. Hakre explains in depth why this can happen and Gordon explains it well, both over at StackExchange.

In my solution below, I took care of that by defining the encoding before the $content is loaded. And temporary disabling libxml errors so we don’t get bugged with warnings by using libxml_use_internal_errors

again, this goes into your functions.php

add_filter( 'the_content', 'wpse_replace_img' );
function wpse_replace_img( $content ){

  if( ! is_user_logged_in() ){

    $dom = new DOMDocument();

    libxml_use_internal_errors(true); // deactivating errors
    // Load without added HTML and doctype, see https://stackoverflow.com/a/22490902/4643867
    $dom->loadHTML( '<?xml encoding="UTF-8">' . $content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); // adding encoding
    libxml_use_internal_errors(false); // reactivating errors

    $tags = $dom->getElementsByTagName( 'img' );

    foreach ($tags as $img) {
      $new_src_url="http://url/to/your/filler-image.jpg";
            $img->setAttribute( 'src', $new_src_url );
            $img->setAttribute( 'srcset', '' );

    }

    $content = $dom->saveHTML();

  }

  return $content;

}

EDIT

Ok, so here’s an edit that takes into consideration wrapping <a> tags around images.
This first solution will replace the link to the image with a #

add_filter( 'the_content', 'wpse_replace_img' );
function wpse_replace_img( $content ){

  if( ! is_user_logged_in() ){

    $dom = new DOMDocument();

    libxml_use_internal_errors(true);
    // Load without added HTML and doctype, see https://stackoverflow.com/a/22490902/4643867
    $dom->loadHTML( '<?xml encoding="UTF-8">' . $content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); // adding encoding
    libxml_use_internal_errors(false);

    $tags  = $dom->getElementsByTagName( 'img' );

    foreach ($tags as $img) {
      $new_src_url="http://url/to/your/filler-image.jpg";
      $img->setAttribute( 'src', $new_src_url );
      $img->setAttribute( 'srcset', '' );

      if( $img->parentNode->tagName == 'a' ){ // if the current image is wrapped by an <a> tag, replace href link with an #

        $img->parentNode->setAttribute( 'href', '#' );

      }

    }

    $content = $dom->saveHTML();

  }

  return $content;

}

Then you could also remove the <a> tag altogether. With some help from matb33 over at stackoverflow I managed to come up with this solution.

add_filter( 'the_content', 'wpse_replace_img' );
function wpse_replace_img( $content ){

  if( ! is_user_logged_in() ){

    $dom = new DOMDocument();

    libxml_use_internal_errors(true);
    // Load without added HTML and doctype, see https://stackoverflow.com/a/22490902/4643867
    $dom->loadHTML( '<?xml encoding="UTF-8">' . $content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); // adding encoding
    libxml_use_internal_errors(false);

    $tags  = $dom->getElementsByTagName( 'img' );
    $fragment = $dom->createDocumentFragment();  // Create our new document fragment to manipulate


    foreach ($tags as $img) {
      $new_src_url="http://url/to/your/filler-image.jpg";
      $img->setAttribute( 'src', $new_src_url );
      $img->setAttribute( 'srcset', '' );

      if( $img->parentNode->tagName == 'a' ){ // if the current image has an  <a> tag as a parent apply replace our <a> tag with our <img> tag

        $a = $img->parentNode;
        $fragment->appendChild( $img->parentNode->childNodes->item( 0 ) ) ; // store our image node into the fragment
        $a->parentNode->replaceChild( $fragment, $a ); // replace our <a> tag with our <img> tag

      }

    }

    $content = $dom->saveHTML();

  }

  return $content;

}

EDIT

And here’s how to apply only to specific categories

add_filter( 'the_content', 'wpse_replace_img' );
function wpse_replace_img( $content ){

  if( ! is_user_logged_in() ){

    $apply_restriction = false;
    $categories = get_the_category();
    $restricted_cat = array(
        'restricted_cat_slug' // Our restricted category slug, since it's an array you could provide more than one
    );


    // Performing our check, switch the flag if we find a match
    foreach( $categories as $cat ){
        foreach( $restricted_cat as $r_cat ){
            if( $cat->slug == $r_cat ){
                $apply_restriction = true;
            }
        }
    }

    // Bail out without applying any restriction if no category match is found
    if( ! $apply_restriction ){
        return $content;
    }

    $dom = new DOMDocument();

    libxml_use_internal_errors(true);
    // Load without added HTML and doctype, see https://stackoverflow.com/a/22490902/4643867
    $dom->loadHTML( '<?xml encoding="UTF-8">' . $content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); // adding encoding
    libxml_use_internal_errors(false);

    $tags  = $dom->getElementsByTagName( 'img' );
    $fragment = $dom->createDocumentFragment();  // Create our new document fragment to manipulate


    foreach ($tags as $img) {
      $new_src_url="http://url/to/your/filler-image.jpg";
      $img->setAttribute( 'src', $new_src_url );
      $img->setAttribute( 'srcset', '' );

      if( $img->parentNode->tagName == 'a' ){ // if the current image has an  <a> tag as a parent apply replace our <a> tag with our <img> tag

        $a = $img->parentNode;
        $fragment->appendChild( $img->parentNode->childNodes->item( 0 ) ) ; // store our image node into the fragment
        $a->parentNode->replaceChild( $fragment, $a ); // replace our <a> tag with our <img> tag

      }

    }

    $content = $dom->saveHTML();

  }

  return $content;

}