Skip to:
Content
Pages
Categories
Search
Top
Bottom

Directly Updating A BuddyPress Avatar

  • Avatar of Dimensionmedia
    David Bisset
    Participant

    @dimensionmedia

    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()?
    
    		}
    
    	}
Viewing 11 replies - 1 through 11 (of 11 total)
  • Avatar of Boone Gorges
    Boone Gorges
    Keymaster

    @boonebgorges

    > After form gets submitted, both cropped versions of the image are put in the avatars / user_id directory.

    Are the old avatar images there too? Where are they being served from?

    Avatar of @modemlooper
    @modemlooper
    Moderator

    @modemlooper

    Is this for a user to do one at a time or you want to do a mass replace? Are the file names names created correctly?

    Avatar of Dimensionmedia
    David Bisset
    Participant

    @dimensionmedia

    The old avatar images, generated by the normal BP avatar upload process. This is for a single user (the logged in user) only. The $args array seems good before the core cropping function is called.

    Avatar of Boone Gorges
    Boone Gorges
    Keymaster

    @boonebgorges

    Where are the old avatars being served from? Where are they stored on disk? What’s their URL? I have a feeling the new ones are being copied into the wrong place.

    Avatar of @modemlooper
    @modemlooper
    Moderator

    @modemlooper

    I would use bp_core_avatar_handle_upload( $file ) because you are not doing a import from one folder to another you are just skipping the default bp cropper.

    Avatar of Dimensionmedia
    David Bisset
    Participant

    @dimensionmedia

    Here’s the before (current avatars created the normal cropped BP way):

    https://dl.dropboxusercontent.com/u/14006840/before.png

    Here’s the after (notice the additional graphic one level up):

    https://dl.dropboxusercontent.com/u/14006840/after.png

    I’m looking over the code again top to bottom. If i understood how in this process BP knows where and what to look for in an avatar (filename wise), i properly wouldn’t be asking this stupid of a question.

    I’ll also take a look at bp_core_avatar_handle_upload ($file) as well.

    Avatar of @modemlooper
    @modemlooper
    Moderator

    @modemlooper

    looks like bp_core_delete_existing_avatar isn’t firing because the file path to new image isn’t right. I’d use bp_core_avatar_handle_upload( $file ) as this will copy and resize the image for use by bp_core_avatar_handle_crop

    Avatar of @modemlooper
    @modemlooper
    Moderator

    @modemlooper

    Also, Boone’s importer assumes the avatar folder/image for the user doesn’t exist so there is no need to delete the previous image

    Avatar of Dimensionmedia
    David Bisset
    Participant

    @dimensionmedia

    Think i figured it out. There was some read/write permissions on the folders – i changed everything to full permissions (this is on my local server) and it SEEMS to be working. I’m also noticing that no additional files are added “one level up” either. Still, no past avatar photos (-bpfull.jpg, -bpthumb.jpg) deleted.

    Avatar of @modemlooper
    @modemlooper
    Moderator

    @modemlooper

    If you need more help post back

    Avatar of Dimensionmedia
    David Bisset
    Participant

    @dimensionmedia

    Thanks. I’m holding to make sure this is what the client wanted, but i do eventually want to remove the unused avatar photos.

Viewing 11 replies - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.