Skip to content

Instantly share code, notes, and snippets.

@eoinkelly
Created April 13, 2012 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eoinkelly/2374454 to your computer and use it in GitHub Desktop.
Save eoinkelly/2374454 to your computer and use it in GitHub Desktop.
A simple wrapper function to make retrieving information about a WordPress attachment nicer.
<?php
/* Retrieve information about WordPress images without the chaos.
You have to remember a bunch of different functions to get info about images in the
Media Library so I collected their functionality under one wrapper with a (hopefully)
nice interface. This function tries to avoid fetching unnecessary info whenever it can.
I have limited the granularity of the API to not allow fetching different info
about each available image size. I think that would make using this function
more fiddly without any great benefit.
Doing this:
$image_info = allib_get_image_info(array(
// The Post ID of the image attachment
'id' => 687, // REQUIRED
// You can ask for any combination of the default image sizes (thumbnail, medium, large, full)
// or any sizes you have defined yourself using add_image_size()
'sizes' => array('thumbnail', 'medium', 'large', 'full'), // OPTIONAL, defaults to 'thumbnail'
// Only the info you ask for here will be returned
'info' => array( // OPTIONAL (but calling this function without it is pointless)
// url, width, height are retrieved for each size you specified in 'sizes'
'url', // The full URL to the file
'width', // Width of the image in pixels
'height', // Height of the image in pixels
// Attachment Metadata
'caption',
'description',
'title',
'alt_text',
// The URL of the attachment page - the format of this depends on
// your permalink structure
'attachment_page_url',
// This retrieves whatever metadata WordPress was able to extract from
// the image file itself. It can include info such as shutter speed etc.
// Confusingly wp_get_attachment_metadata() returns this info and not the
// caption, description, title, alt-text.
'image_meta'
)
));
print_r($image_info);
Returns this:
Array
(
[sizes] => Array
(
[thumbnail] => Array
(
[url] => http://localhost/sandbox/wordpress-stable/wp-content/uploads/2011/12/Kelly_20110127_11847-150x150.jpg
[width] => 150
[height] => 150
)
[medium] => Array
(
[url] => http://localhost/sandbox/wordpress-stable/wp-content/uploads/2011/12/Kelly_20110127_11847-300x188.jpg
[width] => 300
[height] => 188
)
[large] => Array
(
[url] => http://localhost/sandbox/wordpress-stable/wp-content/uploads/2011/12/Kelly_20110127_11847-1024x643.jpg
[width] => 500
[height] => 313
)
[full] => Array
(
[url] => http://localhost/sandbox/wordpress-stable/wp-content/uploads/2011/12/Kelly_20110127_11847.jpg
[width] => 4078
[height] => 2563
)
)
[title] => Kelly_20110127_11847
[caption] =>
[description] =>
[alt_text] =>
[image_meta] => Array
(
[aperture] => 3.5
[credit] =>
[camera] => NIKON D7000
[caption] =>
[created_timestamp] => 1296158616
[copyright] => Eoin Kelly
[focal_length] => 18
[iso] => 10000
[shutter_speed] => 0.01
[title] =>
)
[attachment_page_url] => http://localhost/sandbox/wordpress-stable/?attachment_id=687
)
@version 2012-04-13
TODO
* I just return false on errors - better way to do this? do any of the funcitons I call do different?
* research how WP passes variables and validates them in functions - can I do this better?
*/
function allib_get_image_info($args = array()) {
if (count($args) == 0) {
return false;
}
// We cannot continue if we don't get $id, $requested_info
$id = (array_key_exists('id', $args)) ? $args['id'] : false;
$requested_info = (array_key_exists('info', $args)) ? $args['info'] : false;
// Return false if we don't have the arguments we need
if (! ($id || $requested_info)) {
return false;
}
// If size not specified, it defaults to 'thumbnail'
$sizes = (array_key_exists('sizes', $args)) ? $args['sizes'] : array('thumbnail');
// Initialize a container for our results
$results = array();
/*
* If we are asked for any of url, width, height we fetch
* all of them as it is the same function call.
*/
if (in_array('url', $requested_info) ||
in_array('width', $requested_info) ||
in_array('height', $requested_info)) {
foreach ($sizes as $size) {
$a = wp_get_attachment_image_src($id, $size);
if (in_array('url', $requested_info)) {
$results['sizes'][$size]['url'] = $a[0];
}
if (in_array('width', $requested_info)) {
$results['sizes'][$size]['width'] = $a[1];
}
if (in_array('height', $requested_info)) {
$results['sizes'][$size]['height'] = $a[2];
}
}
}
// If we are asked for any of title, description, caption, alt-text, image_meta
// we need to query the DB
if (in_array('title', $requested_info) ||
in_array('description', $requested_info) ||
in_array('caption', $requested_info) ||
in_array('image_meta', $requested_info) ||
in_array('alt_text', $requested_info)) {
$query = new WP_Query(array(
'post_type' => 'attachment',
'p' => $id // Because we specify the id we *should* get 0 or 1 results
));
// If we didn't get exactly one result, we return false lest we do harm.
if (count($query->posts) != 1) {
return false;
}
foreach ($query->posts as $post) {
if (in_array('title', $requested_info)) {
$results['title'] = $post->post_title;
}
if (in_array('caption', $requested_info)) {
$results['caption'] = $post->post_excerpt;
}
if (in_array('description', $requested_info)) {
$results['description'] = $post->post_content;
}
if (in_array('alt_text', $requested_info)) {
// get_post_meta() touches the DB again so we only run this function if we have
// been specifically asked for alt_text
$results['alt_text'] = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
}
if (in_array('image_meta', $requested_info)) {
// get_post_meta() touches the DB again so we only run this function if we have
// been specifically asked for image_meta
$misc_attachment_metadata = get_post_meta( $post->ID, '_wp_attachment_metadata', true );
if (count($misc_attachment_metadata['image_meta']) > 0) {
$results['image_meta'] = $misc_attachment_metadata['image_meta'];
} else {
$results['image_meta'] = false;
}
}
}
}
if (in_array('attachment_page_url', $requested_info)) {
// get_attachment_link() returns the URI
// wp_get_attachment_link() returns an HTML hyperlink tag
$results['attachment_page_url'] = get_attachment_link($id);
}
// Return an array if we found any results, otherwise return false
// as this seems to be the WordPress way(TM).
if (count($results) > 0) {
return $results;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment