| | 1239 | * Use the absolute path to an image to set an object's avatar |
| | 1240 | * |
| | 1241 | * @since BuddyPress (?) |
| | 1242 | * |
| | 1243 | * @param array $args { |
| | 1244 | * @type int $item_id The ID of the object |
| | 1245 | * @type string $object The object type (eg: group, user, blog) |
| | 1246 | * @type string $component The component for the object (eg: groups, xprofile, blogs) |
| | 1247 | * @type string $image The absolute path to the image |
| | 1248 | * @type int $crop_w Crop width. Default: the global 'full' avatar width, |
| | 1249 | * as retrieved by bp_core_avatar_full_width(). |
| | 1250 | * @type int $crop_h Crop height. Default: the global 'full' avatar height, |
| | 1251 | * as retrieved by bp_core_avatar_full_height(). |
| | 1252 | * @type int $crop_x The horizontal starting point of the crop. Default: 0. |
| | 1253 | * @type int $crop_y The vertical starting point of the crop. Default: 0. |
| | 1254 | * } |
| | 1255 | * @return bool true on success, false otherwise |
| | 1256 | */ |
| | 1257 | function bp_avatar_create_item_avatar( $args = array() ) { |
| | 1258 | $r = bp_parse_args( $args, array( |
| | 1259 | 'item_id' => 0, |
| | 1260 | 'object' => 'user', |
| | 1261 | 'component' => 'xprofile', |
| | 1262 | 'image' => '', |
| | 1263 | 'crop_w' => bp_core_avatar_full_width(), |
| | 1264 | 'crop_h' => bp_core_avatar_full_height(), |
| | 1265 | 'crop_x' => 0, |
| | 1266 | 'crop_y' => 0 |
| | 1267 | ), 'create_item_avatar' ); |
| | 1268 | |
| | 1269 | if ( empty( $r['item_id'] ) || ! file_exists( $r['image'] ) || ! @getimagesize( $r['image'] ) ) { |
| | 1270 | return false; |
| | 1271 | } |
| | 1272 | |
| | 1273 | if ( is_callable( $r['component'] . '_avatar_upload_dir' ) ) { |
| | 1274 | $dir_args = array( $r['item_id'] ); |
| | 1275 | |
| | 1276 | // In case of xprofile, we need an extra argument |
| | 1277 | if ( 'xprofile' === $r['component'] ) { |
| | 1278 | $dir_args = array( false, $r['item_id'] ); |
| | 1279 | } |
| | 1280 | |
| | 1281 | $avatar_data = call_user_func_array( $r['component'] . '_avatar_upload_dir', $dir_args ); |
| | 1282 | } |
| | 1283 | |
| | 1284 | if ( ! isset( $avatar_data['path'] ) || ! isset( $avatar_data['subdir'] ) ) { |
| | 1285 | return false; |
| | 1286 | } |
| | 1287 | |
| | 1288 | // It's not a regular upload, we may need to create this folder |
| | 1289 | if( ! is_dir( $avatar_data['path'] ) ) { |
| | 1290 | if ( ! wp_mkdir_p( $avatar_data['path'] ) ) { |
| | 1291 | return false; |
| | 1292 | } |
| | 1293 | } |
| | 1294 | |
| | 1295 | $image_file_name = wp_unique_filename( $avatar_data['path'], basename( $r['image'] ) ); |
| | 1296 | |
| | 1297 | // Copy the image file into the avatar dir |
| | 1298 | copy( $r['image'], $avatar_data['path'] . '/' . $image_file_name ); |
| | 1299 | |
| | 1300 | // Check the original file is copied |
| | 1301 | if ( ! file_exists( $avatar_data['path'] . '/' . $image_file_name ) ) { |
| | 1302 | return false; |
| | 1303 | } |
| | 1304 | |
| | 1305 | if ( ! bp_core_avatar_handle_crop( array( |
| | 1306 | 'object' => $r['object'], |
| | 1307 | 'avatar_dir' => trim( dirname( $avatar_data['subdir'] ), '/' ), |
| | 1308 | 'item_id' => (int) $r['item_id'], |
| | 1309 | 'original_file' => trailingslashit( $avatar_data['subdir'] ) . $image_file_name, |
| | 1310 | 'crop_w' => $r['crop_w'], |
| | 1311 | 'crop_h' => $r['crop_h'], |
| | 1312 | 'crop_x' => $r['crop_x'], |
| | 1313 | 'crop_y' => $r['crop_y'] |
| | 1314 | ) ) ) { |
| | 1315 | return false; |
| | 1316 | } else { |
| | 1317 | return true; |
| | 1318 | } |
| | 1319 | } |
| | 1320 | |
| | 1321 | /** |