Get post attachment images dimension and use in embed code

WordPress has a predefined function, wp_get_attachment_image_src that returns the following information as an array:

[0] => url
[1] => width
[2] => height

But, the function requires that you know the attachment’s ID, not the post’s ID. In order to get that, you’ll need to use get_post_thumbnail_id.

Here’s the two functions together in action:

$image_id = get_post_thumbnail_id($post->ID);
$image = wp_get_attachment_image_src($image_id);

After that, you can easily do what you’re talking about in your question.

<img src="https://wordpress.stackexchange.com/questions/51796/$image[0]" width="$image[1]" height="$image[2]" />

If you want more information about the wp_get_attachment_image_src, you can read more about it here: http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src.