from_html() static public method

Very raw -- just parse the HTML and pull out any/all img tags and return their src
static public from_html ( mixed $html_or_id ) : Array
$html_or_id mixed The HTML string to parse for images, or a post id
return Array containing images
 /**
  * @author blobaugh
  * @covers Jetpack_PostImages::from_html
  * @since 2.7
  */
 public function test_from_html_double_quotes()
 {
     $s = "<imgANYTHINGATALLHEREsrc='bob.jpg'MOREANYTHINGHERE/>";
     $result = Jetpack_PostImages::from_html($s);
     $this->assertInternalType('array', $result);
     $this->assertFalse(empty($result));
 }
示例#2
0
 /**
  *
  * @param string $html Some markup, possibly containing image tags
  * @param array $images_already_extracted (just an array of image URLs without query strings, no special structure), used for de-duplication
  * @return array Image URLs extracted from the HTML, stripped of query params and de-duped
  */
 public static function get_images_from_html($html, $images_already_extracted)
 {
     $image_list = $images_already_extracted;
     $from_html = Jetpack_PostImages::from_html($html);
     if (!empty($from_html)) {
         $srcs = wp_list_pluck($from_html, 'src');
         foreach ($srcs as $image_url) {
             if (($src = parse_url($image_url)) && isset($src['scheme'], $src['host'], $src['path'])) {
                 // Rebuild the URL without the query string
                 $queryless = $src['scheme'] . '://' . $src['host'] . $src['path'];
             } elseif ($length = strpos($image_url, '?')) {
                 // If parse_url() didn't work, strip off theh query string the old fashioned way
                 $queryless = substr($image_url, 0, $length);
             } else {
                 // Failing that, there was no spoon! Err ... query string!
                 $queryless = $image_url;
             }
             if (!in_array($queryless, $image_list)) {
                 $image_list[] = $queryless;
             }
         }
     }
     return $image_list;
 }
示例#3
0
/**
 * Get one image from a specified post in the following order:
 * Featured Image, first attached image, first image from the_content HTML
 * @param int $id, The post ID to check
 * @param string $size, The image size to return, defaults to 'post-thumbnail'
 * @return string $thumb, Thumbnail image with markup
 */
function spun_get_image($id, $size = 'home-post')
{
    $thumb = '';
    if ('' != get_the_post_thumbnail($id)) {
        $thumb = get_the_post_thumbnail($id, $size, array('title' => esc_attr(strip_tags(get_the_title()))));
    } else {
        $args = array('post_type' => 'attachment', 'fields' => 'ids', 'numberposts' => 1, 'post_status' => null, 'post_mime_type' => 'image', 'post_parent' => $id);
        $first_attachment = get_posts($args);
        if ($first_attachment) {
            /* Get the first image attachment */
            foreach ($first_attachment as $attachment) {
                $thumb = wp_get_attachment_image($attachment, $size, false, array('title' => esc_attr(strip_tags(get_the_title()))));
            }
        } elseif (class_exists('Jetpack_PostImages')) {
            /* Get the first image directly from HTML content */
            $getimage = new Jetpack_PostImages();
            $image = $getimage->from_html($id);
            if ($image) {
                $thumb = '<img src="' . $image[0]['src'] . '" title="' . esc_attr(strip_tags(get_the_title())) . '" class="attachment-' . $size . ' wp-post-image" />';
            }
        }
    }
    return $thumb;
}
示例#4
0
 public function get_post_image($content)
 {
     $image = '';
     if (class_exists('Jetpack_PostImages')) {
         global $post;
         $img = Jetpack_PostImages::from_html($post->ID);
         if (!empty($img['src'])) {
             return $img['src'];
         }
     }
     if (function_exists('has_post_thumbnail') && has_post_thumbnail()) {
         $thumb_id = get_post_thumbnail_id();
         $thumb = wp_get_attachment_image_src($thumb_id, 'full');
         // This shouldn't be necessary, since has_post_thumbnail() is true,
         // but... see http://wordpress.org/support/topic/jetpack-youtube-embeds
         if (!$thumb) {
             return '';
         }
         $image = remove_query_arg(array('w', 'h'), $thumb[0]);
     } else {
         if (preg_match_all('/<img (.+?)>/', $content, $matches)) {
             foreach ($matches[1] as $attrs) {
                 $media = $img = array();
                 foreach (wp_kses_hair($attrs, array('http', 'https')) as $attr) {
                     $img[$attr['name']] = $attr['value'];
                 }
                 if (!isset($img['src']) || 0 !== strpos($img['src'], 'http')) {
                     continue;
                 } else {
                     $image = htmlspecialchars_decode($img['src']);
                     break;
                 }
             }
         }
     }
     return $image;
 }