WordPress.org

Make WordPress Core

Opened 12 years ago

Closed 8 years ago

Last modified 8 years ago

#12238 closed enhancement (worksforme)

Introduce a function to return image attributes of a specific attachment

Reported by: jredfern Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Media Keywords:
Focuses: Cc:

Description (last modified by nacin)

It's useful to get all the data relating to an image via one source:

/**
* returns an array containing attributes for an image stored in the database
 * $image_id : id of the image to query
 * $size : size of the image, can be 'thumbnail', 'medium', 'large' or 'full'
 */	
function get_image_by_id($image_id, $size) {
	$image = get_post($image_id);

	$imageObj = array();
	$imagearray = wp_get_attachment_image_src($image->ID, $size, false);
	$imageObj['src'] = $imagearray[0];
	$imageObj['width'] = $imagearray[1];
	$imageObj['height'] = $imagearray[2];
	$imageObj['id'] = $image->ID;
	$imageObj['title'] = $image->post_title;
	$imageObj['caption'] = $image->post_excerpt;
	$imageObj['description'] = $image->post_content;
	$imageObj['alt'] = get_post_meta($image->ID, '_wp_attachment_image_alt', true);

	return $imageObj;
}

Attachments (1)

post.php.diff (2.1 KB) - added by Amit_k 9 years ago.
Creates function get_attachment() to retrive image info

Download all attachments as: .zip

Change History (11)

#1 @nacin
12 years ago

  • Component changed from General to Media
  • Description modified (diff)
  • Milestone changed from Unassigned to 3.1

#2 @alexmansfield
11 years ago

I wish I would have found this earlier. It is so much simpler than the solution I was working on. However, think it would helpful to be able to access the data in an object format $object->variable. Also, setting a default value for $size would allow easier access to image details such as caption and description when the size doesn't matter. Here are the changes I made:

function get_image_by_id($image_id, $size = 'full') {
	$image = get_post($image_id);
	$imagearray = wp_get_attachment_image_src($image->ID, $size, false);
	$imageObj->src = $imagearray[0];
	$imageObj->width = $imagearray[1];
	$imageObj->height = $imagearray[2];
	$imageObj->id = $image->ID;
	$imageObj->title = $image->post_title;
	$imageObj->caption = $image->post_excerpt;
	$imageObj->description = $image->post_content;
	$imageObj->alt = get_post_meta($image->ID, '_wp_attachment_image_alt', true);

	return $imageObj;
}

Let me know if there's anything else I can do to help this process along.

#3 @nacin
11 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Triage to Future Release

Needs a formal patch.

#4 @ocean90
9 years ago

  • Summary changed from image helper functions that I think would be useful in the core to Introduce a function to return image attributes of a specific attachment

Duplicate: #21851

#5 @Amit_k
9 years ago

  • Keywords has-patch added; needs-patch removed

@Amit_k
9 years ago

Creates function get_attachment() to retrive image info

#6 @DrewAPicture
9 years ago

  • Cc xoodrew@… added

#7 @Amit_k
9 years ago

  • Cc akosti92@… added
  • Keywords dev-feedback added

#8 @ocean90
8 years ago

#21538 was marked as a duplicate.

#9 @ericlewis
8 years ago

  • Keywords has-patch dev-feedback removed
  • Resolution set to worksforme
  • Status changed from new to closed

#10 @SergeyBiryukov
8 years ago

  • Milestone Future Release deleted
Note: See TracTickets for help on using tickets.