Skip to content

Instantly share code, notes, and snippets.

@voodooGQ
Last active August 29, 2015 14:02
Show Gist options
  • Save voodooGQ/3333c3695842537ec278 to your computer and use it in GitHub Desktop.
Save voodooGQ/3333c3695842537ec278 to your computer and use it in GitHub Desktop.
getImageMeta
<?php
/**
* Returns image meta data based on the image ID supplied
*
* @param int $imageID The image post id
* @return array
*/
public static function getImageMeta($imageID)
{
$meta = array();
if(!is_int($imageID) || wp_attachment_is_image($imageID)) {
return $meta;
}
$imageSlugs = get_intermediate_image_sizes();
$image = get_post($imageID);
$meta['title'] = $image->post_title;
$meta['caption'] = $image->post_excerpt;
$meta['description'] = $image->post_content;
$meta['alt'] = get_post_meta($image->ID, '_wp_attachment_image_alt', true);
foreach($imageSlugs as $slug) {
$meta['sizes'][$slug] = array(
'width' => wp_get_attachment_image_src($image->ID, $slug)[1],
'height' => wp_get_attachment_image_src($image->ID, $slug)[2],
);
$meta['urls'][$slug] = wp_get_attachment_image_src($image->ID, $slug)[0];
}
return $meta;
}
array
'title' => 'Test Image'
'caption' => 'This is a Test Image caption'
'description' => 'This is a Test Image Description'
'alt' => 'Alternative Text for the Test Image'
'href' => 'http://london.sierrabravo.net/~ssmith/somesite/test-image/'
'src' => 'http://london.sierrabravo.net/~ssmith/somesite/content/uploads/2014/05/test.jpg'
'sizes' => array
'thumbnail' => array
'width' => 150
'height' => 150
'medium' => array
'width' => 265
'height' => 300
'large' => array
'width' => 906
'height' => 1024
'spotlight' => array
'width' => 1068
'height' => 575
'urls' => array
'thumbnail' => 'http://london.sierrabravo.net/~ssmith/somesite/content/uploads/2014/05/test-150x150.jpg'
'medium' => 'http://london.sierrabravo.net/~ssmith/somesite/content/uploads/2014/05/test-265x300.jpg'
'large' => 'http://london.sierrabravo.net/~ssmith/somesite/content/uploads/2014/05/test-906x1024.jpg'
'spotlight' => 'http://london.sierrabravo.net/~ssmith/somesite/content/uploads/2014/05/test-1068x575.jpg'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment