BP_Attachment_Avatar::crop( array $args = array() )
Crop the avatar.
Description
See also
- BP_Attachment::crop: for the list of parameters
Parameters
- $args
-
(Optional) Array of arguments for the cropping.
Default value: array()
Return
(array) The cropped avatars (full and thumb).
Source
File: bp-core/classes/class-bp-attachment-avatar.php
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | public function crop( $args = array () ) { // Bail if the original file is missing. if ( empty ( $args [ 'original_file' ] ) ) { return false; } if ( ! bp_attachments_current_user_can( 'edit_avatar' , $args ) ) { return false; } if ( 'user' === $args [ 'object' ] ) { $avatar_dir = 'avatars' ; } else { $avatar_dir = sanitize_key( $args [ 'object' ] ) . '-avatars' ; } $args [ 'item_id' ] = (int) $args [ 'item_id' ]; /** * Original file is a relative path to the image * eg: /avatars/1/avatar.jpg */ $relative_path = sprintf( '/%s/%s/%s' , $avatar_dir , $args [ 'item_id' ], basename ( $args [ 'original_file' ] ) ); $absolute_path = $this ->upload_path . $relative_path ; // Bail if the avatar is not available. if ( ! file_exists ( $absolute_path ) ) { return false; } if ( empty ( $args [ 'item_id' ] ) ) { /** This filter is documented in bp-core/bp-core-avatars.php */ $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir' , dirname( $absolute_path ), $args [ 'item_id' ], $args [ 'object' ], $args [ 'avatar_dir' ] ); } else { /** This filter is documented in bp-core/bp-core-avatars.php */ $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir' , $this ->upload_path . '/' . $args [ 'avatar_dir' ] . '/' . $args [ 'item_id' ], $args [ 'item_id' ], $args [ 'object' ], $args [ 'avatar_dir' ] ); } // Bail if the avatar folder is missing for this item_id. if ( ! file_exists ( $avatar_folder_dir ) ) { return false; } // Delete the existing avatar files for the object. $existing_avatar = bp_core_fetch_avatar( array ( 'object' => $args [ 'object' ], 'item_id' => $args [ 'item_id' ], 'html' => false, ) ); /** * Check that the new avatar doesn't have the same name as the * old one before deleting */ if ( ! empty ( $existing_avatar ) && $existing_avatar !== $this ->url . $relative_path ) { bp_core_delete_existing_avatar( array ( 'object' => $args [ 'object' ], 'item_id' => $args [ 'item_id' ], 'avatar_path' => $avatar_folder_dir ) ); } // Make sure we at least have minimal data for cropping. if ( empty ( $args [ 'crop_w' ] ) ) { $args [ 'crop_w' ] = bp_core_avatar_full_width(); } if ( empty ( $args [ 'crop_h' ] ) ) { $args [ 'crop_h' ] = bp_core_avatar_full_height(); } // Get the file extension. $data = @ getimagesize ( $absolute_path ); $ext = $data [ 'mime' ] == 'image/png' ? 'png' : 'jpg' ; $args [ 'original_file' ] = $absolute_path ; $args [ 'src_abs' ] = false; $avatar_types = array ( 'full' => '' , 'thumb' => '' ); foreach ( $avatar_types as $key_type => $type ) { if ( 'thumb' === $key_type ) { $args [ 'dst_w' ] = bp_core_avatar_thumb_width(); $args [ 'dst_h' ] = bp_core_avatar_thumb_height(); } else { $args [ 'dst_w' ] = bp_core_avatar_full_width(); $args [ 'dst_h' ] = bp_core_avatar_full_height(); } $filename = wp_unique_filename( $avatar_folder_dir , uniqid() . "-bp{$key_type}.{$ext}" ); $args [ 'dst_file' ] = $avatar_folder_dir . '/' . $filename ; $avatar_types [ $key_type ] = parent::crop( $args ); } // Remove the original. @unlink( $absolute_path ); // Return the full and thumb cropped avatars. return $avatar_types ; } |
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.