Jetpack_PostImages::from_attachment PHP Method

from_attachment() static public method

Get attachment images for a specified post and return them. Also make sure their dimensions are at or above a required minimum.
static public from_attachment ( $post_id, $width = 200, $height = 200 )
    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;
    }

Usage Example

 /**
  * @author scotchfield
  * @covers Jetpack_PostImages::from_attachment
  * @since 3.2
  */
 public function test_from_attachment_is_correct_array()
 {
     $img_name = 'image.jpg';
     $img_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $img_name;
     $img_html = '<img src="' . $img_url . '"/>';
     $img_dimensions = array('width' => 250, 'height' => 250);
     $post_id = $this->factory->post->create(array('post_content' => $img_html));
     $attachment_id = $this->factory->attachment->create_object($img_name, $post_id, array('post_mime_type' => 'image/jpeg', 'post_type' => 'attachment'));
     wp_update_attachment_metadata($attachment_id, $img_dimensions);
     $images = Jetpack_PostImages::from_attachment($post_id);
     $this->assertEquals(count($images), 1);
     $this->assertEquals($images[0]['src'], $img_url);
 }