Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
12/10/2012 12:52:16 PM (13 years ago)
Author:
johnjamesjacoby
Message:

Avatars:

  • Clean up avatar code.
  • When uploading a png, use the correct file extension.
  • Fixes #4705.
  • Enqueue Jcrop styling.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-avatars.php

    r6564 r6600  
    483483
    484484    $uploadErrors = array(
    485         0 => __("There is no error, the file uploaded with success", 'buddypress'),
    486         1 => __("Your image was bigger than the maximum allowed file size of: ", 'buddypress') . size_format( bp_core_avatar_original_max_filesize() ),
    487         2 => __("Your image was bigger than the maximum allowed file size of: ", 'buddypress') . size_format( bp_core_avatar_original_max_filesize() ),
    488         3 => __("The uploaded file was only partially uploaded", 'buddypress'),
    489         4 => __("No file was uploaded", 'buddypress'),
    490         6 => __("Missing a temporary folder", 'buddypress')
     485        0 => __( 'The image was uploaded successfully', 'buddypress' ),
     486        1 => __( 'The image exceeds the maximum allowed file size of: ', 'buddypress' ) . size_format( bp_core_avatar_original_max_filesize() ),
     487        2 => __( 'The image exceeds the maximum allowed file size of: ', 'buddypress' ) . size_format( bp_core_avatar_original_max_filesize() ),
     488        3 => __( 'The uploaded file was only partially uploaded.', 'buddypress' ),
     489        4 => __( 'The image was not uploaded.', 'buddypress' ),
     490        6 => __( 'Missing a temporary folder.', 'buddypress' )
    491491    );
    492492
    493     if ( !bp_core_check_avatar_upload( $file ) ) {
     493    if ( ! bp_core_check_avatar_upload( $file ) ) {
    494494        bp_core_add_message( sprintf( __( 'Your upload failed, please try again. Error was: %s', 'buddypress' ), $uploadErrors[$file['file']['error']] ), 'error' );
    495495        return false;
    496496    }
    497497
    498     if ( !bp_core_check_avatar_size( $file ) ) {
    499         bp_core_add_message( sprintf( __( 'The file you uploaded is too big. Please upload a file under %s', 'buddypress'), size_format( bp_core_avatar_original_max_filesize() ) ), 'error' );
    500         return false;
    501     }
    502 
    503     if ( !bp_core_check_avatar_type( $file ) ) {
     498    if ( ! bp_core_check_avatar_size( $file ) ) {
     499        bp_core_add_message( sprintf( __( 'The file you uploaded is too big. Please upload a file under %s', 'buddypress' ), size_format( bp_core_avatar_original_max_filesize() ) ), 'error' );
     500        return false;
     501    }
     502
     503    if ( ! bp_core_check_avatar_type( $file ) ) {
    504504        bp_core_add_message( __( 'Please upload only JPG, GIF or PNG photos.', 'buddypress' ), 'error' );
    505505        return false;
     
    531531
    532532            $resized = $editor->resize( bp_core_avatar_original_max_width(), bp_core_avatar_original_max_width(), false );
    533             if ( ! is_wp_error( $resized ) )
     533            if ( ! is_wp_error( $resized ) ) {
    534534                $thumb = $editor->save( $editor->generate_filename() );
    535             else
     535            } else {
    536536                $error = $resized;
     537            }
    537538
    538539            // Check for thumbnail creation errors
    539             if ( false === $error && is_wp_error( $thumb ) )
     540            if ( false === $error && is_wp_error( $thumb ) ) {
    540541                $error = $thumb;
    541 
     542            }
     543
     544            // Thumbnail is good so proceed
    542545            if ( false === $error ) {
    543                 // Thumbnail is good so proceed
    544546                $bp->avatar_admin->resized = $thumb;
    545547            }
     
    556558
    557559    // We only want to handle one image after resize.
    558     if ( empty( $bp->avatar_admin->resized ) )
     560    if ( empty( $bp->avatar_admin->resized ) ) {
    559561        $bp->avatar_admin->image->dir = str_replace( bp_core_avatar_upload_path(), '', $bp->avatar_admin->original['file'] );
    560     else {
     562    } else {
    561563        $bp->avatar_admin->image->dir = str_replace( bp_core_avatar_upload_path(), '', $bp->avatar_admin->resized['path'] );
    562564        @unlink( $bp->avatar_admin->original['file'] );
     
    593595function bp_core_avatar_handle_crop( $args = '' ) {
    594596
    595     $defaults = array(
     597    $r = wp_parse_args( $args, array(
    596598        'object'        => 'user',
    597599        'avatar_dir'    => 'avatars',
     
    602604        'crop_x'        => 0,
    603605        'crop_y'        => 0
    604     );
    605 
    606     $r = wp_parse_args( $args, $defaults );
     606    ) );
    607607
    608608    /***
     
    615615    extract( $r, EXTR_SKIP );
    616616
    617     if ( !$original_file )
     617    if ( empty( $original_file ) )
    618618        return false;
    619619
     
    623623        return false;
    624624
    625     if ( !$item_id )
     625    if ( empty( $item_id ) ) {
    626626        $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', dirname( $original_file ), $item_id, $object, $avatar_dir );
    627     else
     627    } else {
    628628        $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', bp_core_avatar_upload_path() . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir );
     629    }
    629630
    630631    if ( !file_exists( $avatar_folder_dir ) )
     
    638639
    639640    // Make sure we at least have a width and height for cropping
    640     if ( !(int) $crop_w )
     641    if ( empty( $crop_w ) ) {
    641642        $crop_w = bp_core_avatar_full_width();
    642 
    643     if ( !(int) $crop_h )
     643    }
     644
     645    if ( empty( $crop_h ) ) {
    644646        $crop_h = bp_core_avatar_full_height();
     647    }
     648
     649    // Get the file extension
     650    $data = @getimagesize( $original_file );
     651    $ext  = $data['mime'] == 'image/png' ? 'png' : 'jpg';
    645652
    646653    // Set the full and thumb filenames
    647     $full_filename  = wp_hash( $original_file . time() ) . '-bpfull.jpg';
    648     $thumb_filename = wp_hash( $original_file . time() ) . '-bpthumb.jpg';
     654    $full_filename  = wp_hash( $original_file . time() ) . '-bpfull.'  . $ext;
     655    $thumb_filename = wp_hash( $original_file . time() ) . '-bpthumb.' . $ext;
    649656
    650657    // Crop the image
    651     $full_cropped  = wp_crop_image( $original_file, (int) $crop_x, (int) $crop_y, (int) $crop_w, (int) $crop_h, bp_core_avatar_full_width(), bp_core_avatar_full_height(), false, $avatar_folder_dir . '/' . $full_filename );
     658    $full_cropped  = wp_crop_image( $original_file, (int) $crop_x, (int) $crop_y, (int) $crop_w, (int) $crop_h, bp_core_avatar_full_width(),  bp_core_avatar_full_height(),  false, $avatar_folder_dir . '/' . $full_filename );
    652659    $thumb_cropped = wp_crop_image( $original_file, (int) $crop_x, (int) $crop_y, (int) $crop_w, (int) $crop_h, bp_core_avatar_thumb_width(), bp_core_avatar_thumb_height(), false, $avatar_folder_dir . '/' . $thumb_filename );
    653660
    654661    // Check for errors
    655     if ( ! $full_cropped || ! $thumb_cropped || is_wp_error( $full_cropped ) || is_wp_error( $thumb_cropped ) )
     662    if ( empty( $full_cropped ) || empty( $thumb_cropped ) || is_wp_error( $full_cropped ) || is_wp_error( $thumb_cropped ) )
    656663        return false;
    657664
     
    684691
    685692    // If passed an object, assume $user->user_id
    686     if ( is_object( $user ) )
     693    if ( is_object( $user ) ) {
    687694        $id = $user->user_id;
    688695
    689696    // If passed a number, assume it was a $user_id
    690     else if ( is_numeric( $user ) )
     697    } else if ( is_numeric( $user ) ) {
    691698        $id = $user;
    692699
    693700    // If passed a string and that string returns a user, get the $id
    694     else if ( is_string( $user ) && ( $user_by_email = get_user_by( 'email', $user ) ) )
     701    } elseif ( is_string( $user ) && ( $user_by_email = get_user_by( 'email', $user ) ) ) {
    695702        $id = $user_by_email->ID;
     703    }
    696704
    697705    // If somehow $id hasn't been assigned, return the result of get_avatar
    698     if ( empty( $id ) )
     706    if ( empty( $id ) ) {
    699707        return !empty( $avatar ) ? $avatar : $default;
    700 
    701     if ( !$alt )
     708    }
     709
     710    // Image alt tag
     711    if ( empty( $alt ) ) {
    702712        $alt = sprintf( __( 'Avatar of %s', 'buddypress' ), bp_core_get_user_displayname( $id ) );
     713    }
    703714
    704715    // Let BuddyPress handle the fetching of the avatar
Note: See TracChangeset for help on using the changeset viewer.