Directly Updating A BuddyPress Avatar
-
I am using a Gravity Form (think: regular form, no difference in this context) to upload an image and directly overwrite the logged in user’s current avatar.
Thanks to Boone’s Ning-to-BP plugin, i managed to get MOST of this completed. Code is below. Avatar seems to be created and added to the proper directory… but i’m not understanding how BuddyPress now associates that new image with the user. I’m not seeing a “ok, this is the filename of the user’s avatar” now in bp_core_avatar_handle_crop(). BuddyPress and avatars always make my head hurt… and i’m so close. Just wondering what i’m missing.
To clarify what DOES happen: After form gets submitted, both cropped versions of the image are put in the avatars / user_id directory. The only problem is that when i go to the user’s profile page, the old avatar is still there. DA DA DAAAAAAAA.
if ( $form['id'] == 2 ) { // upload avatar $filename = $lead[1]; if ( strpos( $filename, '/' ) ) { $fn = explode( "/", $filename ); $filename = array_pop( $fn ); } $oldfilepath = $lead[1]; if ( $filename ) { if ( !preg_match( '/png|gif|jpg|jpeg|bmp|PNG|GIF|JPG|JPEG|BMP/', $filename ) ) return; $newfilepath = BP_AVATAR_UPLOAD_PATH . '/' . $filename; if ( !file_exists( BP_AVATAR_UPLOAD_PATH . '/avatars/' ) ) mkdir( BP_AVATAR_UPLOAD_PATH . '/avatars/' ); if ( !file_exists( BP_AVATAR_UPLOAD_PATH . '/avatars/' . bp_loggedin_user_id() ) ) mkdir( BP_AVATAR_UPLOAD_PATH . '/avatars/' . bp_loggedin_user_id() ); copy( $oldfilepath, $newfilepath ); // Rudimentary squaring algorithm $size = getimagesize( $newfilepath ); $args = array( 'item_id' => bp_loggedin_user_id(), 'original_file' => '/' . $filename ); if ( $size[0] > $size[1] ) { $diff = $size[0] - $size[1]; $cropx = $diff/2; $args['crop_w'] = $size[1]; $args['crop_h'] = $size[1]; $args['crop_x'] = $cropx; } else { $diff = $size[1] - $size[0]; $cropy = $diff/2; $args['crop_w'] = $size[0]; $args['crop_h'] = $size[0]; $args['crop_y'] = $cropy; } bp_core_avatar_handle_crop( $args ); // todo - find a good way to check for avatar import. bp_core_get_avatar()? } }
- The topic ‘Directly Updating A BuddyPress Avatar’ is closed to new replies.