Automatically add Link Images to Posts in WordPress if has not linked before or has specified class

I use this code to Automatically add Link Images to Posts in WordPress.

function wpguy_linked_image($  content){ if (is_single() === TRUE){ $  searchfor = '/(<img[^>]*\/>)/';   $  replacewith = '<a target="_blank" href="'.get_permalink().'">$  1</a>'; { $  content = preg_replace($  searchfor, $  replacewith, $  content); } } return $  content; } add_filter('the_content', 'wpguy_linked_image'); 

How can I add a condition that if the img tags are already linked or have class="no link me" , they will be excluded from linking?