Get an HTML img element representing an image attachment

While $size will accept an array, it is better to register a size with add_image_size() so that a cropped version is generated. It’s much more efficient than having to find the closest-sized image and then having the browser scale down the image.

Signature

wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' )
attachment_id
 (int) Image attachment ID.
size
 (string) Optional, default is 'thumbnail'.
Default: 'thumbnail'
icon
 (boolean) Optional, default is false. Whether it is an icon.
Default: false
attr
 (mixed) Optional, attributes for the image markup.
Default: ''

Return

(string) HTML img element or empty string on failure.

Source

function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') {

	$html = '';
	$image = wp_get_attachment_image_src($attachment_id, $size, $icon);
	if ( $image ) {
		list($src, $width, $height) = $image;
		$hwstring = image_hwstring($width, $height);
		if ( is_array($size) )
			$size = join('x', $size);
		$attachment = get_post($attachment_id);
		$default_attr = array(
			'src'	=> $src,
			'class'	=> "attachment-$size",
			'alt'	=> trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first
		);
		if ( empty($default_attr['alt']) )
			$default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption
		if ( empty($default_attr['alt']) )
			$default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title

12 more lines...
WP Trac GitHub

Link here