Check if page/post has any anchors that link to an image jpg/gif/png
This method has worked in the past for checking if a shortcode exists on a page. You will have to test it and see if it fires in time to enqueue the javascript>
add_filter('the_posts', 'c3_image_check');
function c3_image_check($posts){
if (empty($posts)) return $posts;
$img_found = false;
foreach ($posts as $post) {
if (stripos($post->post_content, '<img src="https://wordpress.stackexchange.com/questions/53585/)) {
$img_found = true; // bingo!
break;
}
}
if ($img_found) {
wp_enqueue_script("jquery.fancybox', LIB .'/jquery.fancybox.min.js', array( 'jquery' ) );
}
return $posts;
}