示例#1
0
function bp_cover_group_handle_upload($activity_id)
{
    global $bp, $wpdb;
    $group_id = bp_get_current_group_id();
    $activity_table = $wpdb->prefix . "bp_activity";
    $activity_meta_table = $wpdb->prefix . "bp_activity_meta";
    $sql = "SELECT COUNT(*) as photo_count FROM {$activity_table} a INNER JOIN {$activity_meta_table} am ON a.id = am.activity_id WHERE a.item_id = %d AND meta_key = 'all_bp_cover_group'";
    $sql = $wpdb->prepare($sql, $group_id);
    $cnt = $wpdb->get_var($sql);
    $max_cnt = bp_cover_get_max_total();
    if ($cnt < $max_cnt) {
        if ($_POST['encodedimg']) {
            $file = $_POST['imgsize'];
            $max_upload_size = bp_cover_get_max_media_size();
            if ($max_upload_size > $file) {
                $group_id = $bp->groups->current_group->id;
                $imgresponse = array();
                $uploaddir = wp_upload_dir();
                /* let's decode the base64 encoded image sent */
                $img = $_POST['encodedimg'];
                $img = str_replace('data:' . $_POST['imgtype'] . ';base64,', '', $img);
                $img = str_replace(' ', '+', $img);
                $data = base64_decode($img);
                $imgname = wp_unique_filename($uploaddir['path'], $_POST['imgname']);
                $filepath = $uploaddir['path'] . '/' . $imgname;
                $fileurl = $uploaddir['url'] . '/' . $imgname;
                /* now we write the image in dir */
                $success = file_put_contents($filepath, $data);
                if ($success) {
                    $imgresponse[0] = "1";
                    $imgresponse[1] = $fileurl;
                    $size = @getimagesize($filepath);
                    $attachment = array('post_mime_type' => $_POST['imgtype'], 'guid' => $fileurl, 'post_title' => $imgname);
                    require_once ABSPATH . 'wp-admin/includes/image.php';
                    $attachment_id = wp_insert_attachment($attachment, $filepath);
                    $attach_data = wp_generate_attachment_metadata($attachment_id, $filepath);
                    wp_update_attachment_metadata($attachment_id, $attach_data);
                    groups_update_groupmeta($group_id, 'bp_cover_group', $fileurl);
                    groups_update_groupmeta($group_id, 'bp_cover_group_position', 0);
                    $group = groups_get_group(array("group_id" => $group_id));
                    $activity_id = groups_record_activity(array('action' => sprintf(__('%s uploaded a new cover picture to the group %s', 'bp-cover'), bp_core_get_userlink($bp->loggedin_user->id), '<a href="' . bp_get_group_permalink($group) . '">' . esc_attr($group->name) . '</a>'), 'type' => 'cover_added', 'item_id' => $group_id, 'content' => bp_cover_group_get_image_scr(), 'item_id' => $group_id));
                    bp_activity_update_meta($activity_id, 'all_bp_cover_group', $attachment_id);
                    update_post_meta($attachment_id, 'bp_cover_group_thumb', $imgresponse[2]);
                } else {
                    $imgresponse[0] = "0";
                    $imgresponse[1] = __('Upload Failed! Unable to write the image on server', 'bp-cover');
                }
            } else {
                $imgresponse[0] = "0";
                $imgresponse[1] = sprintf(__('The file you uploaded is too big. Please upload a file under %s', 'bp-cover'), size_format($max_upload_size));
            }
        } else {
            $imgresponse[0] = "0";
            $imgresponse[1] = __('Upload Failed! No image sent', 'bp-cover');
        }
    } else {
        $imgresponse[0] = "0";
        $imgresponse[1] = sprintf(__('Max total images allowed %d in a cover gallery', 'bp-cover'), $max_cnt);
    }
    /* if everything is ok, we send back url to thumbnail and to full image */
    echo json_encode($imgresponse);
    die;
}
示例#2
0
function bp_caver_avatar_handle_upload()
{
    global $bp;
    if ($_POST['encodedimg']) {
        $user_id = !empty($_POST['user_id']) ? $_POST['user_id'] : bp_displayed_user_id();
        $imgresponse = array();
        $uploaddir = bp_core_avatar_upload_path() . '/avatars';
        if (!file_exists($uploaddir)) {
            mkdir($uploaddir);
        }
        $img = $_POST['encodedimg'];
        $img = str_replace('data:' . $_POST['imgtype'] . ';base64,', '', $img);
        $img = str_replace(' ', '+', $img);
        $data = base64_decode($img);
        $filepath = $uploaddir . '/' . $user_id;
        if (!file_exists($filepath)) {
            mkdir($filepath);
        }
        $imgname = wp_unique_filename($uploaddir, $_POST['imgname']);
        $fileurl = $filepath . '/' . $imgname;
        $siteurl = trailingslashit(get_blog_option(1, 'siteurl'));
        $url = str_replace(ABSPATH, $siteurl, $fileurl);
        $success = file_put_contents($fileurl, $data);
        $file = $_POST['imgsize'];
        $max_upload_size = bp_cover_get_max_media_size();
        if ($max_upload_size > $file) {
            if ($success) {
                $imgresponse[0] = "1";
                $imgresponse[1] = $fileurl;
                $size = getimagesize($fileurl);
                /* Check image size and shrink if too large */
                if ($size[0] > 150) {
                    $original_file = image_resize($fileurl, 150, 150, true);
                    //$ava_file = image_resize( $fileurl, 250, 250, true );
                    /* Check for thumbnail creation errors */
                    if (is_wp_error($original_file)) {
                        $imgresponse[0] = "0";
                        $imgresponse[1] = sprintf(__('Upload Failed! Error was: %s', 'bp-cover'), $original_file->get_error_message());
                        die;
                    }
                    $avatar_to_crop = str_replace(bp_core_avatar_upload_path(), '', $original_file);
                    bp_core_delete_existing_avatar(array('item_id' => $user_id, 'avatar_path' => bp_core_avatar_upload_path() . '/avatars/' . $user_id));
                    $crop_args = array('item_id' => $user_id, 'original_file' => $avatar_to_crop, 'crop_w' => 0, 'crop_h' => 0);
                    bp_core_avatar_handle_crop($crop_args);
                    //$url = str_replace(ABSPATH,$siteurl,$ava_file);
                    update_user_meta(bp_loggedin_user_id(), 'profile_avatar', $url);
                    do_action('xprofile_avatar_uploaded');
                } else {
                    $imgresponse[0] = "0";
                    $imgresponse[1] = __('Upload Failed! Your photo must be larger than 150px', 'bp-cover');
                }
            } else {
                $imgresponse[0] = "0";
                $imgresponse[1] = __('Upload Failed! Unable to write the image on server', 'bp-cover');
            }
        } else {
            $imgresponse[0] = "0";
            $imgresponse[1] = sprintf(__('The file you uploaded is too big. Please upload a file under %s', 'bp-cover'), size_format($max_upload_size));
        }
    } else {
        $imgresponse[0] = "0";
        $imgresponse[1] = __('Upload Failed! No image sent', 'bp-cover');
    }
    /* if everything is ok, we send back url to thumbnail and to full image */
    echo json_encode($imgresponse);
    die;
}