bp_activity_thumbnail_content_images

Take content, remove images, and replace them with a single thumbnail image.

Description

(string) bp_activity_thumbnail_content_images( (string) $content, (constant) $link = false, (bool) $args = false ); 

The format of items in the activity stream is such that we do not want to allow an arbitrary number of arbitrarily large images to be rendered. However, the activity stream is built to elegantly display a single thumbnail corresponding to the activity comment. This function looks through the content, grabs the first image and converts it to a thumbnail, and removes the rest of the images from the string.

As of BuddyPress 2.3, this function is no longer in use.

Returns (string)

$content The content with images stripped and replaced with a single thumb.

Parameters (3)

0. $content (string)
The content of the activity item.
1. $link — Optional. (constant) => false
The unescaped URL that the image should link to. If absent, the image will not be a link.
2. $args — Optional. (bool) => false
The args passed to the activity creation function (eg bp_blogs_record_activity()).

Usage

  1. if ( !function_exists( 'bp_activity_thumbnail_content_images' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-activity/bp-activity-functions.php'; 
  3.  
  4. // The content of the activity item. 
  5. $content = ''; 
  6.  
  7. // Optional. The unescaped URL that the image should link 
  8. // to. If absent, the image will not be a link. 
  9. $link = false; 
  10.  
  11. // Optional. The args passed to the activity 
  12. // creation function (eg bp_blogs_record_activity()). 
  13. $args = false; 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = bp_activity_thumbnail_content_images($content, $link, $args); 
  17.  

Defined (1)

The function is defined in the following location(s).

/bp-activity/bp-activity-functions.php  
  1. function bp_activity_thumbnail_content_images( $content, $link = false, $args = false ) { 
  2.  
  3. preg_match_all( '/<img[^>]*>/Ui', $content, $matches ); 
  4.  
  5. // Remove <img> tags. Also remove caption shortcodes and caption text if present. 
  6. $content = preg_replace('|(\[caption(.*?)\])?<img[^>]*>([^\[\[]*\[\/caption\])?|', '', $content ); 
  7.  
  8. if ( !empty( $matches ) && !empty( $matches[0] ) ) { 
  9.  
  10. // Get the SRC value. 
  11. preg_match( '/<img.*?(src\=[\'|"]{0, 1}.*?[\'|"]{0, 1})[\s|>]{1}/i', $matches[0][0], $src ); 
  12.  
  13. // Get the width and height. 
  14. preg_match( '/<img.*?(height\=[\'|"]{0, 1}.*?[\'|"]{0, 1})[\s|>]{1}/i', $matches[0][0], $height ); 
  15. preg_match( '/<img.*?(width\=[\'|"]{0, 1}.*?[\'|"]{0, 1})[\s|>]{1}/i', $matches[0][0], $width ); 
  16.  
  17. if ( ! empty( $src ) ) { 
  18. $src = substr( substr( str_replace( 'src=', '', $src[1] ), 0, -1 ), 1 ); 
  19.  
  20. if ( isset( $width[1] ) ) { 
  21. $width = substr( substr( str_replace( 'width=', '', $width[1] ), 0, -1 ), 1 ); 
  22.  
  23. if ( isset( $height[1] ) ) { 
  24. $height = substr( substr( str_replace( 'height=', '', $height[1] ), 0, -1 ), 1 ); 
  25.  
  26. if ( empty( $width ) || empty( $height ) ) { 
  27. $width = 100; 
  28. $height = 100; 
  29.  
  30. $ratio = (int) $width / (int) $height; 
  31. $new_height = (int) $height >= 100 ? 100 : $height; 
  32. $new_width = $new_height * $ratio; 
  33. $image = '<img src="' . esc_url( $src ) . '" width="' . absint( $new_width ) . '" height="' . absint( $new_height ) . '" alt="' . __( 'Thumbnail', buddypress ) . '" class="align-left thumbnail" />'; 
  34.  
  35. if ( !empty( $link ) ) { 
  36. $image = '<a href="' . esc_url( $link ) . '">' . $image . '</a>'; 
  37.  
  38. $content = $image . $content; 
  39.  
  40. /** 
  41. * Filters the activity content that had a thumbnail replace images. 
  42. * @since 1.2.0 
  43. * @param string $content Activity content that had images replaced in. 
  44. * @param array $matches Array of all image tags found in the posted content. 
  45. * @param array $args Arguments passed into function creating the activity update. 
  46. */ 
  47. return apply_filters( 'bp_activity_thumbnail_content_images', $content, $matches, $args );