static function from_attachment($post_id, $width = 200, $height = 200)
{
$images = array();
$post = get_post($post_id);
if (!empty($post->post_password)) {
return $images;
}
$post_images = get_posts(array('post_parent' => $post_id, 'numberposts' => 5, 'post_type' => 'attachment', 'post_mime_type' => 'image'));
if (!$post_images) {
return $images;
}
$permalink = get_permalink($post_id);
foreach ($post_images as $post_image) {
$meta = wp_get_attachment_metadata($post_image->ID);
// Must be larger than 200x200
if (!isset($meta['width']) || $meta['width'] < $width) {
continue;
}
if (!isset($meta['height']) || $meta['height'] < $height) {
continue;
}
$url = wp_get_attachment_url($post_image->ID);
$images[] = array('type' => 'image', 'from' => 'attachment', 'src' => $url, 'src_width' => $meta['width'], 'src_height' => $meta['height'], 'href' => $permalink);
}
/*
* We only want to pass back attached images that were actually inserted.
* We can load up all the images found in the HTML source and then
* compare URLs to see if an image is attached AND inserted.
*/
$html_images = self::from_html($post_id);
$inserted_images = array();
foreach ($html_images as $html_image) {
$src = parse_url($html_image['src']);
// strip off any query strings from src
if (!empty($src['scheme']) && !empty($src['host'])) {
$inserted_images[] = $src['scheme'] . '://' . $src['host'] . $src['path'];
} elseif (!empty($src['host'])) {
$inserted_images[] = set_url_scheme('http://' . $src['host'] . $src['path']);
} else {
$inserted_images[] = site_url('/') . $src['path'];
}
}
foreach ($images as $i => $image) {
if (!in_array($image['src'], $inserted_images)) {
unset($images[$i]);
}
}
return $images;
}