Can I get the ID of an inserted / linked image?

I am using this code to get the url of first inserted image in a post. Is there any way I can get the ID of that image.

      global $post, $posts;
      $first_img = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/<img.+src=['"]([^'"]+)['"].*>/i', $post->post_content, $matches);
      $first_image = $matches [1] [0];

Related posts

Leave a Reply

1 comment

  1. No, an image placed inside a post/page content don’t reference in your database other than inside your content only.

    That’s why an image attached is better: you have full control over your images since they are in your server and referenced in your database. An attachment is a child post, once you attach an file to a post/page you can use get_children for retrieve file description, title etc.