Jetpack_PostImages::from_thumbnail PHP Method

from_thumbnail() static public method

Check if a Featured Image is set for this post, and return it in a similar format to the other images?_from_*() methods.
static public from_thumbnail ( integer $post_id, $width = 200, $height = 200 ) : Array
$post_id integer The post ID to check
return Array containing details of the Featured Image, or empty array if none.
    static function from_thumbnail($post_id, $width = 200, $height = 200)
    {
        $images = array();
        $post = get_post($post_id);
        if (!empty($post->post_password)) {
            return $images;
        }
        if (!function_exists('get_post_thumbnail_id')) {
            return $images;
        }
        $thumb = get_post_thumbnail_id($post_id);
        if ($thumb) {
            $meta = wp_get_attachment_metadata($thumb);
            // Must be larger than requested minimums
            if (!isset($meta['width']) || $meta['width'] < $width) {
                return $images;
            }
            if (!isset($meta['height']) || $meta['height'] < $height) {
                return $images;
            }
            $too_big = !empty($meta['width']) && $meta['width'] > 1200 || !empty($meta['height']) && $meta['height'] > 1200;
            if ($too_big && (method_exists('Jetpack', 'is_module_active') && Jetpack::is_module_active('photon') || defined('WPCOM') && IS_WPCOM)) {
                $img_src = wp_get_attachment_image_src($thumb, array(1200, 1200));
            } else {
                $img_src = wp_get_attachment_image_src($thumb, 'full');
            }
            $url = $img_src[0];
            $images = array(array('type' => 'image', 'from' => 'thumbnail', 'src' => $url, 'src_width' => $img_src[1], 'src_height' => $img_src[2], 'href' => get_permalink($thumb)));
        }
        if (empty($images) && (defined('IS_WPCOM') && IS_WPCOM)) {
            $meta_thumbnail = get_post_meta($post_id, '_jetpack_post_thumbnail', true);
            if (!empty($meta_thumbnail)) {
                if (!isset($meta_thumbnail['width']) || $meta_thumbnail['width'] < $width) {
                    return $images;
                }
                if (!isset($meta_thumbnail['height']) || $meta_thumbnail['height'] < $height) {
                    return $images;
                }
                $images = array(array('type' => 'image', 'from' => 'thumbnail', 'src' => $meta_thumbnail['URL'], 'src_width' => $meta_thumbnail['width'], 'src_height' => $meta_thumbnail['height'], 'href' => $meta_thumbnail['URL']));
            }
        }
        return $images;
    }

Usage Example

function enhanced_og_has_featured_image($post_id)
{
    $featured = Jetpack_PostImages::from_thumbnail($post_id, 200, 200);
    if (!empty($featured) && count($featured) > 0) {
        return true;
    }
    return false;
}
All Usage Examples Of Jetpack_PostImages::from_thumbnail