Ejemplo n.º 1
0
/**
 * Crop an uploaded avatar
 *
 * $args has the following parameters:
 *  object - What component the avatar is for, e.g. "user"
 *  avatar_dir  The absolute path to the avatar
 *  item_id - Item ID
 *  original_file - The absolute path to the original avatar file
 *  crop_w - Crop width
 *  crop_h - Crop height
 *  crop_x - The horizontal starting point of the crop
 *  crop_y - The vertical starting point of the crop
 *
 * @global object $bp BuddyPress global settings
 * @param mixed $args
 * @return bool Success/failure
 */
function bp_core_avatar_handle_crop($args = '')
{
    global $bp;
    $defaults = array('object' => 'user', 'avatar_dir' => 'avatars', 'item_id' => false, 'original_file' => false, 'crop_w' => bp_core_avatar_full_width(), 'crop_h' => bp_core_avatar_full_height(), 'crop_x' => 0, 'crop_y' => 0);
    $r = nxt_parse_args($args, $defaults);
    /***
     * 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_crop', true, $r)) {
        return true;
    }
    extract($r, EXTR_SKIP);
    if (!$original_file) {
        return false;
    }
    $original_file = bp_core_avatar_upload_path() . $original_file;
    if (!file_exists($original_file)) {
        return false;
    }
    if (!$item_id) {
        $avatar_folder_dir = apply_filters('bp_core_avatar_folder_dir', dirname($original_file), $item_id, $object, $avatar_dir);
    } else {
        $avatar_folder_dir = apply_filters('bp_core_avatar_folder_dir', bp_core_avatar_upload_path() . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir);
    }
    if (!file_exists($avatar_folder_dir)) {
        return false;
    }
    require_once ABSPATH . '/nxt-admin/includes/image.php';
    require_once ABSPATH . '/nxt-admin/includes/file.php';
    // Delete the existing avatar files for the object
    bp_core_delete_existing_avatar(array('object' => $object, 'avatar_path' => $avatar_folder_dir));
    // Make sure we at least have a width and height for cropping
    if (!(int) $crop_w) {
        $crop_w = bp_core_avatar_full_width();
    }
    if (!(int) $crop_h) {
        $crop_h = bp_core_avatar_full_height();
    }
    // Set the full and thumb filenames
    $full_filename = nxt_hash($original_file . time()) . '-bpfull.jpg';
    $thumb_filename = nxt_hash($original_file . time()) . '-bpthumb.jpg';
    // Crop the image
    $full_cropped = nxt_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);
    $thumb_cropped = nxt_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);
    // Remove the original
    @unlink($original_file);
    return true;
}
Ejemplo n.º 2
0
 /**
  * Display third step of custom header image page.
  *
  * @since 2.1.0
  */
 function step_3()
 {
     check_admin_referer('custom-header-crop-image');
     if (!current_theme_supports('custom-header-uploads')) {
         nxt_die(__('Cheatin’ uh?'));
     }
     if ($_POST['oitar'] > 1) {
         $_POST['x1'] = $_POST['x1'] * $_POST['oitar'];
         $_POST['y1'] = $_POST['y1'] * $_POST['oitar'];
         $_POST['width'] = $_POST['width'] * $_POST['oitar'];
         $_POST['height'] = $_POST['height'] * $_POST['oitar'];
     }
     $attachment_id = absint($_POST['attachment_id']);
     $original = get_attached_file($attachment_id);
     $cropped = nxt_crop_image($attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT);
     if (is_nxt_error($cropped)) {
         nxt_die(__('Image could not be processed.  Please go back and try again.'), __('Image Processing Error'));
     }
     $cropped = apply_filters('nxt_create_file_in_uploads', $cropped, $attachment_id);
     // For replication
     $parent = get_post($attachment_id);
     $parent_url = $parent->guid;
     $url = str_replace(basename($parent_url), basename($cropped), $parent_url);
     // Construct the object array
     $object = array('ID' => $attachment_id, 'post_title' => basename($cropped), 'post_content' => $url, 'post_mime_type' => 'image/jpeg', 'guid' => $url, 'context' => 'custom-header');
     // Update the attachment
     nxt_insert_attachment($object, $cropped);
     nxt_update_attachment_metadata($attachment_id, nxt_generate_attachment_metadata($attachment_id, $cropped));
     update_post_meta($attachment_id, '_nxt_attachment_is_custom_header', get_option('stylesheet'));
     set_theme_mod('header_image', $url);
     // cleanup
     $medium = str_replace(basename($original), 'midsize-' . basename($original), $original);
     @unlink(apply_filters('nxt_delete_file', $medium));
     @unlink(apply_filters('nxt_delete_file', $original));
     return $this->finished();
 }