get_images_from_html() public static method

public static get_images_from_html ( string $html, array $images_already_extracted ) : array
$html string Some markup, possibly containing image tags
$images_already_extracted array (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
コード例 #1
0
    /**
     * @author scotchfield
     * @covers Jetpack_Media_Meta_Extractor::get_images_from_html
     * @since 3.2
     */
    function test_extract_image_from_html()
    {
        $html = <<<EOT
<p><a href="http://paulbernal.files.wordpress.com/2013/05/mr-gove-cover.jpeg"><img class="aligncenter size-full wp-image-1027" alt="Mr Gove Cover" src="http://paulbernal.files.wordpress.com/2013/05/mr-gove-cover.jpeg?w=640" /></a></p>
<p>Mr Gove was extraordinarily arrogant.</p>
<p>Painfully arrogant.</p>
<p>He believed that he knew how everything should be done. He believed that everyone else in the world was stupid and ignorant.</p>
<p>The problem was, Mr Gove himself was the one who was ignorant.</p>
<p><a href="http://paulbernal.files.wordpress.com/2013/05/mr-gove-close-up.jpeg"><img class="aligncenter size-full wp-image-1030" alt="Mr Gove Close up" src="http://paulbernal.files.wordpress.com/2013/05/mr-gove-close-up.jpeg?w=640" /></a></p>
<p>He got most of his information from his own, misty, memory.</p>
<p>He thought he remembered what it had been like when he had been at school &#8211; and assumed that everyone else&#8217;s school should be the same.</p>
<p>He remembered the good things about his own school days, and thought that everyone should have the same.</p>
<p>He remembered the bad things about his own school days, and thought that it hadn&#8217;t done him any harm &#8211; and that other children should suffer the way that he had.</p>
EOT;
        $expected = array(0 => 'http://images-r-us.com/some-image.png', 1 => 'http://paulbernal.files.wordpress.com/2013/05/mr-gove-cover.jpeg', 2 => 'http://paulbernal.files.wordpress.com/2013/05/mr-gove-close-up.jpeg');
        $already_extracted_images = array('http://images-r-us.com/some-image.png');
        $result = Jetpack_Media_Meta_Extractor::get_images_from_html($html, $already_extracted_images);
        $this->assertEquals($expected, $result);
    }
コード例 #2
0
 public static function extract_images_from_content($content)
 {
     $image_list = Jetpack_Media_Meta_Extractor::get_images_from_html($post->post_content, $image_list);
     return Jetpack_Media_Meta_Extractor::build_image_struct($image_list);
 }
コード例 #3
0
 /**
  * @param $post A post object
  * @param $args (array) Optional args, see defaults list for details
  * @returns array Returns an array of all images meeting the specified criteria in $args
  *
  * Uses Jetpack Post Images
  */
 private static function get_image_fields($post, $args = array())
 {
     $defaults = array('width' => 200, 'height' => 200);
     $args = wp_parse_args($args, $defaults);
     $image_list = array();
     $image_booleans = array();
     $image_booleans['gallery'] = 0;
     $from_slideshow = Jetpack_PostImages::from_slideshow($post->ID, $args['width'], $args['height']);
     if (!empty($from_slideshow)) {
         $srcs = wp_list_pluck($from_slideshow, 'src');
         $image_list = array_merge($image_list, $srcs);
     }
     $from_gallery = Jetpack_PostImages::from_gallery($post->ID);
     if (!empty($from_gallery)) {
         $srcs = wp_list_pluck($from_gallery, 'src');
         $image_list = array_merge($image_list, $srcs);
         $image_booleans['gallery']++;
         // @todo This count isn't correct, will only every count 1
     }
     // @todo Can we check width/height of these efficiently?  Could maybe use query args at least, before we strip them out
     $image_list = Jetpack_Media_Meta_Extractor::get_images_from_html($post->post_content, $image_list);
     if (!empty($image_list)) {
         $retval = array('image' => array());
         $unique_imgs = array_unique($image_list);
         foreach ($image_list as $img) {
             $retval['image'][] = array('url' => $img);
         }
         $image_booleans['image'] = count($retval['image']);
         if (!empty($image_booleans)) {
             $retval['has'] = $image_booleans;
         }
         return $retval;
     } else {
         return array();
     }
 }