Skip to:
Content

BuddyPress.org

Changeset 6600


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

Avatars:

  • Clean up avatar code.
  • When uploading a png, use the correct file extension.
  • Fixes #4705.
  • Enqueue Jcrop styling.
Location:
trunk
Files:
3 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
  • trunk/bp-core/bp-core-cssjs.php

    r6597 r6600  
    11<?php
     2
    23// Exit if accessed directly
    34if ( !defined( 'ABSPATH' ) ) exit;
     
    2627        });
    2728    </script>
     29
    2830<?php
    2931}
     
    3941 */
    4042function bp_core_add_jquery_cropper() {
     43    wp_enqueue_style( 'jcrop' );
    4144    wp_enqueue_script( 'jcrop', array( 'jquery' ) );
    4245    add_action( 'wp_head', 'bp_core_add_cropper_inline_js' );
     
    5255 */
    5356function bp_core_add_cropper_inline_js() {
    54     global $bp;
    5557
    56     $image = apply_filters( 'bp_inline_cropper_image', getimagesize( bp_core_avatar_upload_path() . $bp->avatar_admin->image->dir ) );
    57     $aspect_ratio = 1;
     58    // Bail if no image was uploaded
     59    $image = apply_filters( 'bp_inline_cropper_image', getimagesize( bp_core_avatar_upload_path() . buddypress()->avatar_admin->image->dir ) );
     60    if ( empty( $image ) )
     61        return;
    5862
     63    //
    5964    $full_height = bp_core_avatar_full_height();
    6065    $full_width  = bp_core_avatar_full_width();
    6166
    6267    // Calculate Aspect Ratio
    63     if ( $full_height && ( $full_width != $full_height ) )
     68    if ( !empty( $full_height ) && ( $full_width != $full_height ) ) {
    6469        $aspect_ratio = $full_width / $full_height;
     70    } else {
     71        $aspect_ratio = 1;
     72    }
    6573
    66         $width  = $image[0] / 2;
    67         $height = $image[1] / 2;
    68 ?>
     74    // Default cropper coordinates
     75    $crop_left   = $image[0] / 4;
     76    $crop_top    = $image[1] / 4;
     77    $crop_right  = $image[0] - $crop_left;
     78    $crop_bottom = $image[1] - $crop_top; ?>
    6979
    7080    <script type="text/javascript">
     
    7484                onSelect: showPreview,
    7585                onSelect: updateCoords,
    76                 aspectRatio: <?php echo $aspect_ratio ?>,
    77                 setSelect: [ 50, 50, <?php echo $width ?>, <?php echo $height ?> ]
     86                aspectRatio: <?php echo $aspect_ratio; ?>,
     87                setSelect: [ <?php echo $crop_left; ?>, <?php echo $crop_top; ?>, <?php echo $crop_right; ?>, <?php echo $crop_bottom; ?> ]
    7888            });
    79             updateCoords({x: 50, y: 50, w: <?php echo $width ?>, h: <?php echo $height ?>});
     89            updateCoords({x: <?php echo $crop_left; ?>, y: <?php echo $crop_top; ?>, w: <?php echo $crop_right; ?>, h: <?php echo $crop_bottom; ?>});
    8090        });
    8191
     
    8999        function showPreview(coords) {
    90100            if ( parseInt(coords.w) > 0 ) {
    91                 var rx = <?php echo $full_width; ?> / coords.w;
    92                 var ry = <?php echo $full_height; ?> / coords.h;
     101                var fw = <?php echo $full_width; ?>;
     102                var fh = <?php echo $full_height; ?>;
     103                var rx = fw / coords.w;
     104                var ry = fh / coords.h;
    93105
    94                 jQuery('#avatar-crop-preview').css({
    95                 <?php if ( $image ) : ?>
     106                jQuery( '#avatar-crop-preview' ).css({
    96107                    width: Math.round(rx * <?php echo $image[0]; ?>) + 'px',
    97108                    height: Math.round(ry * <?php echo $image[1]; ?>) + 'px',
    98                 <?php endif; ?>
    99109                    marginLeft: '-' + Math.round(rx * coords.x) + 'px',
    100110                    marginTop: '-' + Math.round(ry * coords.y) + 'px'
     
    119129    <style type="text/css">
    120130        .jcrop-holder { float: left; margin: 0 20px 20px 0; text-align: left; }
    121         .jcrop-vline, .jcrop-hline { font-size: 0; position: absolute; background: white top left repeat url('<?php echo BP_PLUGIN_URL ?>/bp-core/images/Jcrop.gif'); }
    122         .jcrop-vline { height: 100%; width: 1px !important; }
    123         .jcrop-hline { width: 100%; height: 1px !important; }
    124         .jcrop-handle { font-size: 1px; width: 7px !important; height: 7px !important; border: 1px #eee solid; background-color: #333; *width: 9px; *height: 9px; }
    125         .jcrop-tracker { width: 100%; height: 100%; }
    126         .custom .jcrop-vline, .custom .jcrop-hline { background: yellow; }
    127         .custom .jcrop-handle { border-color: black; background-color: #C7BB00; -moz-border-radius: 3px; -webkit-border-radius: 3px; }
    128131        #avatar-crop-pane { width: <?php echo bp_core_avatar_full_width() ?>px; height: <?php echo bp_core_avatar_full_height() ?>px; overflow: hidden; }
    129132        #avatar-crop-submit { margin: 20px 0; }
    130         #avatar-upload-form img, #create-group-form img, #group-settings-form img { border: none !important; }
     133        #avatar-upload-form img,
     134        #create-group-form img,
     135        #group-settings-form img { border: none !important; max-width: none !important; }
    131136    </style>
    132137
  • trunk/bp-xprofile/bp-xprofile-screens.php

    r6395 r6600  
    3737 */
    3838function xprofile_screen_edit_profile() {
    39     global $bp;
    4039
    4140    if ( !bp_is_my_profile() && !bp_current_user_can( 'bp_moderate' ) )
    4241        return false;
     42
     43    $bp = buddypress();
    4344
    4445    // Make sure a group is set.
     
    105106
    106107                // Certain types of fields (checkboxes, multiselects) may come through empty. Save them as an empty array so that they don't get overwritten by the default on the next edit.
    107                 if ( empty( $_POST['field_' . $field_id] ) )
     108                if ( empty( $_POST['field_' . $field_id] ) ) {
    108109                    $value = array();
    109                 else
     110                } else {
    110111                    $value = $_POST['field_' . $field_id];
    111 
    112                 if ( !xprofile_set_field_data( $field_id, bp_displayed_user_id(), $value, $is_required[$field_id] ) )
     112                }
     113
     114                if ( !xprofile_set_field_data( $field_id, bp_displayed_user_id(), $value, $is_required[$field_id] ) ) {
    113115                    $errors = true;
    114                 else
     116                } else {
    115117                    do_action( 'xprofile_profile_field_data_updated', $field_id, $value );
     118                }
    116119
    117120                // Save the visibility level
     
    123126
    124127            // Set the feedback messages
    125             if ( !empty( $errors ) )
     128            if ( !empty( $errors ) ) {
    126129                bp_core_add_message( __( 'There was a problem updating some of your profile information, please try again.', 'buddypress' ), 'error' );
    127             else
     130            } else {
    128131                bp_core_add_message( __( 'Changes saved.', 'buddypress' ) );
     132            }
    129133
    130134            // Redirect back to the edit screen to display the updates and message
     
    145149 */
    146150function xprofile_screen_change_avatar() {
    147     global $bp;
    148 
     151
     152    // Bail if not the correct screen
    149153    if ( !bp_is_my_profile() && !bp_current_user_can( 'bp_moderate' ) )
    150154        return false;
    151155
     156    // Bail if there are action variables
    152157    if ( bp_action_variables() ) {
    153158        bp_do_404();
     
    155160    }
    156161
     162    $bp = buddypress();
     163
    157164    if ( ! isset( $bp->avatar_admin ) )
    158165        $bp->avatar_admin = new stdClass();
     
    180187        check_admin_referer( 'bp_avatar_cropstore' );
    181188
    182         if ( !bp_core_avatar_handle_crop( array( 'item_id' => bp_displayed_user_id(), 'original_file' => $_POST['image_src'], 'crop_x' => $_POST['x'], 'crop_y' => $_POST['y'], 'crop_w' => $_POST['w'], 'crop_h' => $_POST['h'] ) ) )
    183             bp_core_add_message( __( 'There was a problem cropping your avatar, please try uploading it again', 'buddypress' ), 'error' );
    184         else {
    185             bp_core_add_message( __( 'Your new avatar was uploaded successfully!', 'buddypress' ) );
     189        $args = array(
     190            'item_id'       => bp_displayed_user_id(),
     191            'original_file' => $_POST['image_src'],
     192            'crop_x'        => $_POST['x'],
     193            'crop_y'        => $_POST['y'],
     194            'crop_w'        => $_POST['w'],
     195            'crop_h'        => $_POST['h']
     196        );
     197
     198        if ( ! bp_core_avatar_handle_crop( $args ) ) {
     199            bp_core_add_message( __( 'There was a problem cropping your avatar.', 'buddypress' ), 'error' );
     200        } else {
     201            bp_core_add_message( __( 'Your new avatar was uploaded successfully.', 'buddypress' ) );
    186202            do_action( 'xprofile_avatar_uploaded' );
    187203        }
Note: See TracChangeset for help on using the changeset viewer.