Scan for first image in post and display it [duplicate]

Sorry to not help with your existing code but if you are open to using a plugin I can recommend: http://wordpress.org/extend/plugins/get-the-image/

It is great as it grabs the first image, or a featured image if you have one. You can also specify the dimensions to crop the image and an optional fallback image in case there is no image in the post.

You can just use <?php get_the_image(); ?> in a template and it will default to:

$defaults = array(
    'meta_key' => array( 'Thumbnail', 'thumbnail' ),
    'post_id' => $post->ID,
    'attachment' => true,
    'the_post_thumbnail' => true,
    'size' => 'thumbnail',
    'default_image' => false,
    'order_of_image' => 1,
    'link_to_post' => true,
    'image_class' => false,
    'image_scan' => false,
    'width' => false,
    'height' => false,
    'format' => 'img',
    'meta_key_save' => false,
    'callback' => null,
    'cache' => true,
    'echo' => true
);