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)
  • 'images'
    (array) Extracted images. { Array of extracted media.
  • 'gallery_id'
    (int) Gallery ID. Optional, not always set.
  • 'height'
    (int) Width of image. If unknown, set to 0.
  • 'source'
    (string) Media source. Either "html" or "galleries".
  • 'url'
    (string) Link to image.
  • 'width'
    (int) Width of image. If unknown, set to 0.
  • Source

    File: bp-core/classes/class-bp-media-extractor.php

    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    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

    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.