/**
  * Maybe shrink the attachment to fit maximum allowed width.
  *
  * @since 2.3.0
  * @since 2.4.0 Add the $ui_available_width parameter, to inform about the Avatar UI width.
  *
  * @uses  bp_core_avatar_original_max_width()
  *
  * @param string $file               The absolute path to the file.
  * @param int    $ui_available_width Available width for the UI.
  * @return mixed
  */
 public static function shrink($file = '', $ui_available_width = 0)
 {
     // Get image size.
     $avatar_data = parent::get_image_data($file);
     // Init the edit args.
     $edit_args = array();
     // Defaults to the Avatar original max width constant.
     $original_max_width = bp_core_avatar_original_max_width();
     // The ui_available_width is defined and it's smaller than the Avatar original max width.
     if (!empty($ui_available_width) && $ui_available_width < $original_max_width) {
         /**
          * In this case, to make sure the content of the image will be fully displayed
          * during the cropping step, let's use the Avatar UI Available width.
          */
         $original_max_width = $ui_available_width;
         // $original_max_width has to be larger than the avatar's full width
         if ($original_max_width < bp_core_avatar_full_width()) {
             $original_max_width = bp_core_avatar_full_width();
         }
     }
     // Do we need to resize the image?
     if (isset($avatar_data['width']) && $avatar_data['width'] > $original_max_width) {
         $edit_args = array('max_w' => $original_max_width, 'max_h' => $original_max_width);
     }
     // Do we need to rotate the image?
     $angles = array(3 => 180, 6 => -90, 8 => 90);
     if (isset($avatar_data['meta']['orientation']) && isset($angles[$avatar_data['meta']['orientation']])) {
         $edit_args['rotate'] = $angles[$avatar_data['meta']['orientation']];
     }
     // No need to edit the avatar, original file will be used.
     if (empty($edit_args)) {
         return false;
         // Add the file to the edit arguments.
     } else {
         $edit_args['file'] = $file;
     }
     return parent::edit_image('avatar', $edit_args);
 }
Example #2
0
/**
 * Handles avatar uploading.
 *
 * The functions starts off by checking that the file has been uploaded properly using bp_core_check_avatar_upload().
 * It then checks that the file size is within limits, and that it has an accepted file extension (jpg, gif, png).
 * If everything checks out, crop the image and move it to its real location.
 *
 * @global object $bp BuddyPress global settings
 * @param array $file The appropriate entry the from $_FILES superglobal.
 * @param string $upload_dir_filter A filter to be applied to upload_dir
 * @return bool Success/failure
 * @see bp_core_check_avatar_upload()
 * @see bp_core_check_avatar_type()
 */
function bp_core_avatar_handle_upload($file, $upload_dir_filter)
{
    global $bp;
    /***
     * You may want to hook into this filter if you want to override this function.
     * Make sure you return false.
     */
    if (!apply_filters('bp_core_pre_avatar_handle_upload', true, $file, $upload_dir_filter)) {
        return true;
    }
    require_once ABSPATH . '/nxt-admin/includes/image.php';
    require_once ABSPATH . '/nxt-admin/includes/file.php';
    $uploadErrors = array(0 => __("There is no error, the file uploaded with success", 'buddypress'), 1 => __("Your image was bigger than the maximum allowed file size of: ", 'buddypress') . size_format(bp_core_avatar_original_max_filesize()), 2 => __("Your image was bigger than the maximum allowed file size of: ", 'buddypress') . size_format(bp_core_avatar_original_max_filesize()), 3 => __("The uploaded file was only partially uploaded", 'buddypress'), 4 => __("No file was uploaded", 'buddypress'), 6 => __("Missing a temporary folder", 'buddypress'));
    if (!bp_core_check_avatar_upload($file)) {
        bp_core_add_message(sprintf(__('Your upload failed, please try again. Error was: %s', 'buddypress'), $uploadErrors[$file['file']['error']]), 'error');
        return false;
    }
    if (!bp_core_check_avatar_size($file)) {
        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');
        return false;
    }
    if (!bp_core_check_avatar_type($file)) {
        bp_core_add_message(__('Please upload only JPG, GIF or PNG photos.', 'buddypress'), 'error');
        return false;
    }
    // Filter the upload location
    add_filter('upload_dir', $upload_dir_filter, 10, 0);
    $bp->avatar_admin->original = nxt_handle_upload($file['file'], array('action' => 'bp_avatar_upload'));
    // Move the file to the correct upload location.
    if (!empty($bp->avatar_admin->original['error'])) {
        bp_core_add_message(sprintf(__('Upload Failed! Error was: %s', 'buddypress'), $bp->avatar_admin->original['error']), 'error');
        return false;
    }
    // Get image size
    $size = @getimagesize($bp->avatar_admin->original['file']);
    // Check image size and shrink if too large
    if ($size[0] > bp_core_avatar_original_max_width()) {
        $thumb = nxt_create_thumbnail($bp->avatar_admin->original['file'], bp_core_avatar_original_max_width());
        // Check for thumbnail creation errors
        if (is_nxt_error($thumb)) {
            bp_core_add_message(sprintf(__('Upload Failed! Error was: %s', 'buddypress'), $thumb->get_error_message()), 'error');
            return false;
        }
        // Thumbnail is good so proceed
        $bp->avatar_admin->resized = $thumb;
    }
    // We only want to handle one image after resize.
    if (empty($bp->avatar_admin->resized)) {
        $bp->avatar_admin->image->dir = str_replace(bp_core_avatar_upload_path(), '', $bp->avatar_admin->original['file']);
    } else {
        $bp->avatar_admin->image->dir = str_replace(bp_core_avatar_upload_path(), '', $bp->avatar_admin->resized);
        @unlink($bp->avatar_admin->original['file']);
    }
    // Check for nxt_Error on what should be an image
    if (is_nxt_error($bp->avatar_admin->image->dir)) {
        bp_core_add_message(sprintf(__('Upload failed! Error was: %s', 'buddypress'), $bp->avatar_admin->image->dir->get_error_message()), 'error');
        return false;
    }
    // Set the url value for the image
    $bp->avatar_admin->image->url = bp_core_avatar_url() . $bp->avatar_admin->image->dir;
    return true;
}
Example #3
0
/**
 * Handle avatar uploading.
 *
 * The functions starts off by checking that the file has been uploaded
 * properly using bp_core_check_avatar_upload(). It then checks that the file
 * size is within limits, and that it has an accepted file extension (jpg, gif,
 * png). If everything checks out, crop the image and move it to its real
 * location.
 *
 * @see bp_core_check_avatar_upload()
 * @see bp_core_check_avatar_type()
 *
 * @param array $file The appropriate entry the from $_FILES superglobal.
 * @param string $upload_dir_filter A filter to be applied to 'upload_dir'.
 * @return bool True on success, false on failure.
 */
function bp_core_avatar_handle_upload($file, $upload_dir_filter)
{
    /***
     * You may want to hook into this filter if you want to override this function.
     * Make sure you return false.
     */
    if (!apply_filters('bp_core_pre_avatar_handle_upload', true, $file, $upload_dir_filter)) {
        return true;
    }
    require_once ABSPATH . '/wp-admin/includes/file.php';
    $uploadErrors = array(0 => __('The image was uploaded successfully', 'buddypress'), 1 => __('The image exceeds the maximum allowed file size of: ', 'buddypress') . size_format(bp_core_avatar_original_max_filesize()), 2 => __('The image exceeds the maximum allowed file size of: ', 'buddypress') . size_format(bp_core_avatar_original_max_filesize()), 3 => __('The uploaded file was only partially uploaded.', 'buddypress'), 4 => __('The image was not uploaded.', 'buddypress'), 6 => __('Missing a temporary folder.', 'buddypress'));
    if (!bp_core_check_avatar_upload($file)) {
        bp_core_add_message(sprintf(__('Your upload failed, please try again. Error was: %s', 'buddypress'), $uploadErrors[$file['file']['error']]), 'error');
        return false;
    }
    if (!bp_core_check_avatar_size($file)) {
        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');
        return false;
    }
    if (!bp_core_check_avatar_type($file)) {
        bp_core_add_message(__('Please upload only JPG, GIF or PNG photos.', 'buddypress'), 'error');
        return false;
    }
    // Filter the upload location
    add_filter('upload_dir', $upload_dir_filter, 10, 0);
    $bp = buddypress();
    $bp->avatar_admin->original = wp_handle_upload($file['file'], array('action' => 'bp_avatar_upload'));
    // Remove the upload_dir filter, so that other upload URLs on the page
    // don't break
    remove_filter('upload_dir', $upload_dir_filter, 10, 0);
    // Move the file to the correct upload location.
    if (!empty($bp->avatar_admin->original['error'])) {
        bp_core_add_message(sprintf(__('Upload Failed! Error was: %s', 'buddypress'), $bp->avatar_admin->original['error']), 'error');
        return false;
    }
    // Get image size
    $size = @getimagesize($bp->avatar_admin->original['file']);
    $error = false;
    // Check image size and shrink if too large
    if ($size[0] > bp_core_avatar_original_max_width()) {
        $editor = wp_get_image_editor($bp->avatar_admin->original['file']);
        if (!is_wp_error($editor)) {
            $editor->set_quality(100);
            $resized = $editor->resize(bp_core_avatar_original_max_width(), bp_core_avatar_original_max_width(), false);
            if (!is_wp_error($resized)) {
                $thumb = $editor->save($editor->generate_filename());
            } else {
                $error = $resized;
            }
            // Check for thumbnail creation errors
            if (false === $error && is_wp_error($thumb)) {
                $error = $thumb;
            }
            // Thumbnail is good so proceed
            if (false === $error) {
                $bp->avatar_admin->resized = $thumb;
            }
        } else {
            $error = $editor;
        }
        if (false !== $error) {
            bp_core_add_message(sprintf(__('Upload Failed! Error was: %s', 'buddypress'), $error->get_error_message()), 'error');
            return false;
        }
    }
    if (!isset($bp->avatar_admin->image)) {
        $bp->avatar_admin->image = new stdClass();
    }
    // We only want to handle one image after resize.
    if (empty($bp->avatar_admin->resized)) {
        $bp->avatar_admin->image->dir = str_replace(bp_core_avatar_upload_path(), '', $bp->avatar_admin->original['file']);
    } else {
        $bp->avatar_admin->image->dir = str_replace(bp_core_avatar_upload_path(), '', $bp->avatar_admin->resized['path']);
        @unlink($bp->avatar_admin->original['file']);
    }
    // Check for WP_Error on what should be an image
    if (is_wp_error($bp->avatar_admin->image->dir)) {
        bp_core_add_message(sprintf(__('Upload failed! Error was: %s', 'buddypress'), $bp->avatar_admin->image->dir->get_error_message()), 'error');
        return false;
    }
    // If the uploaded image is smaller than the "full" dimensions, throw
    // a warning
    $uploaded_image = @getimagesize(bp_core_avatar_upload_path() . buddypress()->avatar_admin->image->dir);
    $full_width = bp_core_avatar_full_width();
    $full_height = bp_core_avatar_full_height();
    if (isset($uploaded_image[0]) && $uploaded_image[0] < $full_width || $uploaded_image[1] < $full_height) {
        bp_core_add_message(sprintf(__('You have selected an image that is smaller than recommended. For best results, upload a picture larger than %d x %d pixels.', 'buddypress'), $full_width, $full_height), 'error');
    }
    // Set the url value for the image
    $bp->avatar_admin->image->url = bp_core_avatar_url() . $bp->avatar_admin->image->dir;
    return true;
}
 /**
  * Maybe shrink the attachment to fit maximum allowed width.
  *
  * @since 2.3.0
  *
  * @uses  bp_core_avatar_original_max_width()
  * @uses  wp_get_image_editor()
  *
  * @param string $file the absolute path to the file.
  *
  * @return mixed
  */
 public static function shrink($file = '')
 {
     // Get image size
     $size = @getimagesize($file);
     $retval = false;
     // Check image size and shrink if too large
     if ($size[0] > bp_core_avatar_original_max_width()) {
         $editor = wp_get_image_editor($file);
         if (!is_wp_error($editor)) {
             $editor->set_quality(100);
             $resized = $editor->resize(bp_core_avatar_original_max_width(), bp_core_avatar_original_max_width(), false);
             if (!is_wp_error($resized)) {
                 $thumb = $editor->save($editor->generate_filename());
             } else {
                 $retval = $resized;
             }
             // Check for thumbnail creation errors
             if (false === $retval && is_wp_error($thumb)) {
                 $retval = $thumb;
             }
             // Thumbnail is good so proceed
             if (false === $retval) {
                 $retval = $thumb;
             }
         } else {
             $retval = $editor;
         }
     }
     return $retval;
 }