BP_Media_Extractor::extract_images( string $richtext, string $plaintext, array $extra_args = array() )
Extract images from <img src>
tags, [galleries], and featured images from a Post.
Description
If an image is in the Media Library, then its resolution is included in the results.
Parameters
- $richtext
-
(Required) Content to parse.
- $plaintext
-
(Required) Sanitized version of the content.
- $extra_args
-
(Optional) Bespoke data for a particular extractor (optional).
Default value: array()
Return
(array)
- 'has'
(array) Extracted media counts. { - 'images'
(int)
(array) Extracted images. { Array of extracted media.
(int) Gallery ID. Optional, not always set.
(int) Width of image. If unknown, set to 0.
(string) Media source. Either "html" or "galleries".
(string) Link to image.
(int) Width of image. If unknown, set to 0.
Source
File: bp-core/classes/class-bp-media-extractor.php
protected function extract_images( $richtext, $plaintext, $extra_args = array() ) { $media = array( 'has' => array( 'images' => 0 ), 'images' => array() ); $featured_image = $this->extract_images_from_featured_images( $richtext, $plaintext, $extra_args ); $galleries = $this->extract_images_from_galleries( $richtext, $plaintext, $extra_args ); // `<img src>` tags. if ( stripos( $richtext, 'src=' ) !== false ) { preg_match_all( '#src=(["\'])([^"\']+)\1#i', $richtext, $img_srcs ); // Matches src="text" and src='text'. // <img>. if ( ! empty( $img_srcs[2] ) ) { $img_srcs[2] = array_unique( $img_srcs[2] ); foreach ( $img_srcs[2] as $image_src ) { // Skip data URIs. if ( strtolower( substr( $image_src, 0, 5 ) ) === 'data:' ) { continue; } $image_src = esc_url_raw( $image_src ); if ( ! $image_src ) { continue; } $media['images'][] = array( 'source' => 'html', 'url' => $image_src, // The image resolution isn't available, but we need to set the keys anyway. 'height' => 0, 'width' => 0, ); } } } // Galleries. if ( ! empty( $galleries ) ) { foreach ( $galleries as $gallery ) { foreach ( $gallery as $image ) { $image_url = esc_url_raw( $image['url'] ); if ( ! $image_url ) { continue; } $media['images'][] = array( 'gallery_id' => $image['gallery_id'], 'source' => 'galleries', 'url' => $image_url, 'width' => $image['width'], 'height' => $image['height'], ); } } $media['has']['galleries'] = count( $galleries ); } // Featured images (aka thumbnails). if ( ! empty( $featured_image ) ) { $image_url = esc_url_raw( $featured_image[0] ); if ( $image_url ) { $media['images'][] = array( 'source' => 'featured_images', 'url' => $image_url, 'width' => $featured_image[1], 'height' => $featured_image[2], ); $media['has']['featured_images'] = 1; } } // Update image count. $media['has']['images'] = count( $media['images'] ); /** * Filters images extracted from text. * * @since BuddyPress 2.3.0 * * @param array $media Extracted images. See {@link BP_Media_Extractor::extract_images()} for format. * @param string $richtext Content to parse. * @param string $plaintext Copy of $richtext without any markup. * @param array $extra_args Bespoke data for a particular extractor. */ return apply_filters( 'bp_media_extractor_images', $media, $richtext, $plaintext, $extra_args ); }
Changelog
Version | Description |
---|---|
BuddyPress 2.3.0 | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.