bp_avatar_handle_capture( string $data = '', int $item_id )

Handle avatar webcam capture.

Description

Parameters

$data

(Optional) Base64 encoded image.

Default value: ''

$item_id

(Required) Item to associate.

Return

(bool) True on success, false on failure.

Source

File: bp-core/bp-core-avatars.php

function bp_avatar_handle_capture( $data = '', $item_id = 0 ) {
	if ( empty( $data ) || empty( $item_id ) ) {
		return false;
	}

	/**
	 * Filters whether or not to handle avatar webcam capture.
	 *
	 * If you want to override this function, make sure you return false.
	 *
	 * @since BuddyPress 2.5.1
	 *
	 * @param bool   $value   Whether or not to crop.
	 * @param string $data    Base64 encoded image.
	 * @param int    $item_id Item to associate.
	 */
	if ( ! apply_filters( 'bp_avatar_pre_handle_capture', true, $data, $item_id ) ) {
		return true;
	}

	$avatar_dir = bp_core_avatar_upload_path() . '/avatars';

	// It's not a regular upload, we may need to create this folder.
	if ( ! file_exists( $avatar_dir ) ) {
		if ( ! wp_mkdir_p( $avatar_dir ) ) {
			return false;
		}
	}

	/**
	 * Filters the Avatar folder directory.
	 *
	 * @since BuddyPress 2.3.0
	 *
	 * @param string $avatar_dir Directory for storing avatars.
	 * @param int    $item_id    ID of the item being acted on.
	 * @param string $value      Avatar type.
	 * @param string $value      Avatars word.
	 */
	$avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', $avatar_dir . '/' . $item_id, $item_id, 'user', 'avatars' );

	// It's not a regular upload, we may need to create this folder.
	if( ! is_dir( $avatar_folder_dir ) ) {
		if ( ! wp_mkdir_p( $avatar_folder_dir ) ) {
			return false;
		}
	}

	$original_file = $avatar_folder_dir . '/webcam-capture-' . $item_id . '.png';

	if ( file_put_contents( $original_file, $data ) ) {
		$avatar_to_crop = str_replace( bp_core_avatar_upload_path(), '', $original_file );

		// Crop to default values.
		$crop_args = array( 'item_id' => $item_id, 'original_file' => $avatar_to_crop, 'crop_x' => 0, 'crop_y' => 0 );

		return bp_core_avatar_handle_crop( $crop_args );
	} else {
		return false;
	}
}

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.