[buddypress-trac] [BuddyPress Trac] #9335: Crop fails when the uploaded image has "-bpfull" in the file name (was: Crop fails when the image is 260x260 pixels)
buddypress-trac
noreply at wordpress.org
Sat Apr 25 23:48:00 UTC 2026
#9335: Crop fails when the uploaded image has "-bpfull" in the file name
--------------------------+------------------------------
Reporter: joneiseman | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Core | Version: 14.4.0
Severity: normal | Resolution:
Keywords: |
--------------------------+------------------------------
Comment (by joneiseman):
Here are my suggested code changes to fhe file buddypress/bp-core/classes
/class-bp-attachment-avatar.php to fix this problem:
{{{
231,233d230
< $safe_copy = dirname( $absolute_path ) . '/safe_tmp.' .
pathinfo( $absolute_path, PATHINFO_EXTENSION );
< copy( $absolute_path, $safe_copy );
<
246d242
< @unlink( $safe_copy );
314c310
< $data = @getimagesize( $safe_copy );
---
> $data = @getimagesize( $absolute_path );
317c313
< $args['original_file'] = $safe_copy;
---
> $args['original_file'] = $absolute_path;
343d338
< @unlink( $safe_copy );
}}}
Here's the new version of the function crop.php:
{{{#!php
<?php
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;
}
$safe_copy = dirname( $absolute_path ) . '/safe_tmp.' .
pathinfo( $absolute_path, PATHINFO_EXTENSION );
copy( $absolute_path, $safe_copy );
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 ) ) {
@unlink( $safe_copy );
return false;
}
// Get 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 moving the previous one into history.
*/
if ( ! empty( $existing_avatar ) && $existing_avatar !==
$this->url . $relative_path ) {
// Avatar history is disabled, simply delete the
existing avatar files.
if ( bp_avatar_history_is_disabled() ) {
bp_core_delete_existing_avatar(
array(
'object' =>
$args['object'],
'item_id' =>
$args['item_id'],
'avatar_path' =>
$avatar_folder_dir,
)
);
} else {
// Add a new revision for the existing
avatar.
$avatars =
bp_attachments_list_directory_files( $avatar_folder_dir );
if ( $avatars ) {
foreach ( $avatars as $avatar_file
) {
if ( ! isset(
$avatar_file->name, $avatar_file->id, $avatar_file->path ) ) {
continue;
}
$is_full = preg_match(
'/-bpfull/', $avatar_file->name );
$is_thumb = preg_match(
'/-bpthumb/', $avatar_file->name );
if ( $is_full || $is_thumb
) {
$revision =
$this->add_revision(
'avatar',
array(
'file_abspath' => $avatar_file->path,
'file_id' => $avatar_file->id,
)
);
if ( is_wp_error(
$revision ) ) {
error_log(
$revision->get_error_message() );
}
}
}
}
}
}
// 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( $safe_copy );
$ext = $data['mime'] === 'image/png' ? 'png' : 'jpg';
$args['original_file'] = $safe_copy;
$args['src_abs'] = false;
$avatar_types = array(
'full' => '',
'thumb' => '',
);
$timestamp = bp_core_current_time( true, 'timestamp' );
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, $timestamp . "-bp{$key_type}.{$ext}" );
$args['dst_file'] = $avatar_folder_dir . '/' .
$filename;
$avatar_types[ $key_type ] = parent::crop( $args
);
}
// Remove the original.
@unlink( $absolute_path );
@unlink( $safe_copy );
// Return the full, thumb cropped avatars and the
timestamp.
return array_merge(
$avatar_types,
array(
'timestamp' => $timestamp,
)
);
}
}}}
--
Ticket URL: <https://buddypress.trac.wordpress.org/ticket/9335#comment:2>
BuddyPress Trac <http://buddypress.org/>
BuddyPress Trac
More information about the buddypress-trac
mailing list