Beispiel #1
0
 function process()
 {
     if (isset($_POST['upload'])) {
         check_admin_referer('thesis-header-upload', '_wpnonce-thesis-header-upload');
         #wp
         $overrides = array('test_form' => false);
         $file = wp_handle_upload($_FILES['import'], $overrides);
         #wp
         if (isset($file['error'])) {
             wp_die($file['error'], __('Image Upload Error', 'thesis'));
         }
         #wp
         if ($file['type'] == 'image/jpeg' || $file['type'] == 'image/pjpeg' || $file['type'] == 'image/png' || $file['type'] == 'image/gif') {
             $this->url = $file['url'];
             $image = $file['file'];
             list($this->width, $this->height) = getimagesize($image);
             if ($this->width <= $this->optimal_width) {
                 $this->save($image);
             } elseif ($this->width > $this->optimal_width) {
                 if (apply_filters('thesis_crop_header', true)) {
                     #filter
                     $this->ratio = $this->width / $this->optimal_width;
                     $cropped = wp_crop_image($image, 0, 0, $this->width, $this->height, $this->optimal_width, $this->height / $this->ratio, false, str_replace(basename($image), 'cropped-' . basename($image), $image));
                     #wp
                     if (is_wp_error($cropped)) {
                         #wp
                         wp_die(__('Your image could not be processed. Please go back and try again.', 'thesis'), __('Image Processing Error', 'thesis'));
                     }
                     #wp
                     $this->url = str_replace(basename($this->url), basename($cropped), $this->url);
                     $this->width = round($this->width / $this->ratio);
                     $this->height = round($this->height / $this->ratio);
                     $this->save($cropped);
                     @unlink($image);
                 } else {
                     $this->save($image);
                 }
             }
         } else {
             $this->error = true;
         }
     } elseif ($_GET['remove']) {
         check_admin_referer('thesis-remove-header');
         #wp
         unset($this->header);
         delete_option('thesis_header');
         #wp
         global $thesis_design;
         if (!$thesis_design->display['header']['tagline'] && apply_filters('thesis_header_auto_tagline', true)) {
             #filter
             $thesis_design->display['header']['tagline'] = true;
             update_option('thesis_design_options', $thesis_design);
             #wp
         }
         thesis_generate_css();
         $this->removed = true;
     }
 }
Beispiel #2
0
 /**
  * Gets attachment uploaded by Media Manager, crops it, then saves it as a
  * new object. Returns JSON-encoded object details.
  */
 public function ajax_header_crop()
 {
     check_ajax_referer('image_editor-' . $_POST['id'], 'nonce');
     if (!current_user_can('edit_theme_options')) {
         wp_send_json_error();
     }
     if (!current_theme_supports('custom-header', 'uploads')) {
         wp_send_json_error();
     }
     $crop_details = $_POST['cropDetails'];
     $dimensions = $this->get_header_dimensions(array('height' => $crop_details['height'], 'width' => $crop_details['width']));
     $attachment_id = absint($_POST['id']);
     $cropped = wp_crop_image($attachment_id, (int) $crop_details['x1'], (int) $crop_details['y1'], (int) $crop_details['width'], (int) $crop_details['height'], (int) $dimensions['dst_width'], (int) $dimensions['dst_height']);
     if (!$cropped || is_wp_error($cropped)) {
         wp_send_json_error(array('message' => __('Image could not be processed. Please go back and try again.')));
     }
     /** This filter is documented in wp-admin/custom-header.php */
     $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $attachment_id);
     // For replication
     $object = $this->create_attachment_object($cropped, $attachment_id);
     unset($object['ID']);
     $new_attachment_id = $this->insert_attachment($object, $cropped);
     $object['attachment_id'] = $new_attachment_id;
     $object['width'] = $dimensions['dst_width'];
     $object['height'] = $dimensions['dst_height'];
     wp_send_json_success($object);
 }
Beispiel #3
0
/**
 * AJAX handler for cropping an image.
 *
 * @since 4.3.0
 *
 * @global WP_Site_Icon $wp_site_icon
 */
function wp_ajax_crop_image()
{
    $attachment_id = absint($_POST['id']);
    check_ajax_referer('image_editor-' . $attachment_id, 'nonce');
    if (!current_user_can('customize')) {
        wp_send_json_error();
    }
    $context = str_replace('_', '-', $_POST['context']);
    $data = array_map('absint', $_POST['cropDetails']);
    $cropped = wp_crop_image($attachment_id, $data['x1'], $data['y1'], $data['width'], $data['height'], $data['dst_width'], $data['dst_height']);
    if (!$cropped || is_wp_error($cropped)) {
        wp_send_json_error(array('message' => __('Image could not be processed.')));
    }
    switch ($context) {
        case 'site-icon':
            require_once ABSPATH . '/wp-admin/includes/class-wp-site-icon.php';
            global $wp_site_icon;
            /** This filter is documented in wp-admin/custom-header.php */
            $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $attachment_id);
            // For replication.
            $object = $wp_site_icon->create_attachment_object($cropped, $attachment_id);
            unset($object['ID']);
            // Update the attachment.
            add_filter('intermediate_image_sizes_advanced', array($wp_site_icon, 'additional_sizes'));
            $attachment_id = $wp_site_icon->insert_attachment($object, $cropped);
            remove_filter('intermediate_image_sizes_advanced', array($wp_site_icon, 'additional_sizes'));
            // Additional sizes in wp_prepare_attachment_for_js().
            add_filter('image_size_names_choose', array($wp_site_icon, 'additional_sizes'));
            break;
        default:
            /**
             * Filters the attachment id for a cropped image.
             *
             * @since 4.3.0
             *
             * @param int    $attachment_id The ID of the cropped image.
             * @param string $context       The feature requesting the cropped image.
             */
            $attachment_id = apply_filters('wp_ajax_cropped_attachment_id', 0, $context);
            if (!$attachment_id) {
                wp_send_json_error();
            }
    }
    wp_send_json_success(wp_prepare_attachment_for_js($attachment_id));
}
Beispiel #4
0
/**
 * AJAX handler for cropping an image.
 *
 * @since 4.3.0
 *
 * @global WP_Site_Icon $wp_site_icon
 */
function wp_ajax_crop_image()
{
    $attachment_id = absint($_POST['id']);
    check_ajax_referer('image_editor-' . $attachment_id, 'nonce');
    if (!current_user_can('customize')) {
        wp_send_json_error();
    }
    $context = str_replace('_', '-', $_POST['context']);
    $data = array_map('absint', $_POST['cropDetails']);
    $cropped = wp_crop_image($attachment_id, $data['x1'], $data['y1'], $data['width'], $data['height'], $data['dst_width'], $data['dst_height']);
    if (!$cropped || is_wp_error($cropped)) {
        wp_send_json_error(array('message' => __('Image could not be processed.')));
    }
    switch ($context) {
        case 'site-icon':
            require_once ABSPATH . '/wp-admin/includes/class-wp-site-icon.php';
            global $wp_site_icon;
            // Skip creating a new attachment if the attachment is a Site Icon.
            if (get_post_meta($attachment_id, '_wp_attachment_context', true) == $context) {
                // Delete the temporary cropped file, we don't need it.
                wp_delete_file($cropped);
                // Additional sizes in wp_prepare_attachment_for_js().
                add_filter('image_size_names_choose', array($wp_site_icon, 'additional_sizes'));
                break;
            }
            /** This filter is documented in wp-admin/custom-header.php */
            $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $attachment_id);
            // For replication.
            $object = $wp_site_icon->create_attachment_object($cropped, $attachment_id);
            unset($object['ID']);
            // Update the attachment.
            add_filter('intermediate_image_sizes_advanced', array($wp_site_icon, 'additional_sizes'));
            $attachment_id = $wp_site_icon->insert_attachment($object, $cropped);
            remove_filter('intermediate_image_sizes_advanced', array($wp_site_icon, 'additional_sizes'));
            // Additional sizes in wp_prepare_attachment_for_js().
            add_filter('image_size_names_choose', array($wp_site_icon, 'additional_sizes'));
            break;
        default:
            /**
             * Fires before a cropped image is saved.
             *
             * Allows to add filters to modify the way a cropped image is saved.
             *
             * @since 4.3.0
             *
             * @param string $context       The Customizer control requesting the cropped image.
             * @param int    $attachment_id The attachment ID of the original image.
             * @param string $cropped       Path to the cropped image file.
             */
            do_action('wp_ajax_crop_image_pre_save', $context, $attachment_id, $cropped);
            /** This filter is documented in wp-admin/custom-header.php */
            $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $attachment_id);
            // For replication.
            $parent_url = wp_get_attachment_url($attachment_id);
            $url = str_replace(basename($parent_url), basename($cropped), $parent_url);
            $size = @getimagesize($cropped);
            $image_type = $size ? $size['mime'] : 'image/jpeg';
            $object = array('post_title' => basename($cropped), 'post_content' => $url, 'post_mime_type' => $image_type, 'guid' => $url, 'context' => $context);
            $attachment_id = wp_insert_attachment($object, $cropped);
            $metadata = wp_generate_attachment_metadata($attachment_id, $cropped);
            /**
             * Filter the cropped image attachment metadata.
             *
             * @since 4.3.0
             *
             * @see wp_generate_attachment_metadata()
             *
             * @param array $metadata Attachment metadata.
             */
            $metadata = apply_filters('wp_ajax_cropped_attachment_metadata', $metadata);
            wp_update_attachment_metadata($attachment_id, $metadata);
            /**
             * Filter the attachment ID for a cropped image.
             *
             * @since 4.3.0
             *
             * @param int    $attachment_id The attachment ID of the cropped image.
             * @param string $context       The Customizer control requesting the cropped image.
             */
            $attachment_id = apply_filters('wp_ajax_cropped_attachment_id', $attachment_id, $context);
    }
    wp_send_json_success(wp_prepare_attachment_for_js($attachment_id));
}
Beispiel #5
0
 /**
  * Display third step of custom header image page.
  *
  * @since unknown
  */
 function step_3()
 {
     check_admin_referer('custom-header');
     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'];
     }
     $original = get_attached_file($_POST['attachment_id']);
     $cropped = wp_crop_image($_POST['attachment_id'], $_POST['x1'], $_POST['y1'], $_POST['width'], $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT);
     $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $_POST['attachment_id']);
     // For replication
     $parent = get_post($_POST['attachment_id']);
     $parent_url = $parent->guid;
     $url = str_replace(basename($parent_url), basename($cropped), $parent_url);
     // Construct the object array
     $object = array('ID' => $_POST['attachment_id'], 'post_title' => basename($cropped), 'post_content' => $url, 'post_mime_type' => 'image/jpeg', 'guid' => $url);
     // Update the attachment
     wp_insert_attachment($object, $cropped);
     wp_update_attachment_metadata($_POST['attachment_id'], wp_generate_attachment_metadata($_POST['attachment_id'], $cropped));
     set_theme_mod('header_image', $url);
     // cleanup
     $medium = str_replace(basename($original), 'midsize-' . basename($original), $original);
     @unlink(apply_filters('wp_delete_file', $medium));
     @unlink(apply_filters('wp_delete_file', $original));
     return $this->finished();
 }
function bp_core_avatar_cropstore($source, $canvas, $v1_x1, $v1_y1, $v1_w, $v1_h, $v2_x1, $v2_y1, $v2_w, $v2_h, $from_signup = false, $filename = 'avatar', $item_id = null)
{
    $size = getimagesize($source);
    $dims = getimagesize($canvas);
    // Figure out multiplier for scaling
    $multi = $size[0] / $dims[0];
    if ($item_id) {
        $filename_item_id = $item_id . '-';
    }
    if ($filename != 'avatar') {
        $v1_filename = '-' . $filename_item_id . $filename . '-thumb';
        $v2_filename = '-' . $filename_item_id . $filename . '-full';
    } else {
        $v1_filename = '-avatar1';
        $v2_filename = '-avatar2';
    }
    $v1_filename = apply_filters('bp_avatar_v1_filename', $v1_filename);
    $v2_filename = apply_filters('bp_avatar_v2_filename', $v2_filename);
    // Perform v1 crop
    $v1_dest = apply_filters('bp_avatar_v1_dest', dirname($source) . '/' . preg_replace('!(\\.[^.]+)?$!', $v1_filename . '$1', basename($source), 1), $source);
    if ($from_signup) {
        $v1_out = wp_crop_image($source, $v1_x1, $v1_y1, $v1_w, $v1_h, CORE_AVATAR_V1_W, CORE_AVATAR_V1_H, false, $v1_dest);
    } else {
        $v1_out = wp_crop_image($source, $v1_x1 * $multi, $v1_y1 * $multi, $v1_w * $multi, $v1_h * $multi, CORE_AVATAR_V1_W, CORE_AVATAR_V1_H, false, $v1_dest);
    }
    // Perform v2 crop
    if (CORE_AVATAR_V2_W !== false && CORE_AVATAR_V2_H !== false) {
        $v2_dest = apply_filters('bp_avatar_v2_dest', dirname($source) . '/' . preg_replace('!(\\.[^.]+)?$!', $v2_filename . '$1', basename($source), 1), $source);
        if ($from_signup) {
            $v2_out = wp_crop_image($source, $v2_x1, $v2_y1, $v2_w, $v2_h, CORE_AVATAR_V2_W, CORE_AVATAR_V2_H, false, $v2_dest);
        } else {
            $v2_out = wp_crop_image($source, $v2_x1 * $multi, $v2_y1 * $multi, $v2_w * $multi, $v2_h * $multi, CORE_AVATAR_V2_W, CORE_AVATAR_V2_H, false, $v2_dest);
        }
    }
    // Clean up canvas and original images used during cropping
    foreach (array(str_replace('..', '', $source), str_replace('..', '', $canvas)) as $f) {
        @unlink($f);
    }
    $dir = $source;
    do {
        $dir = dirname($dir);
        @rmdir($dir);
        // will fail on non-empty directories
    } while (substr_count($dir, '/') >= 2 && stristr($dir, ABSPATH));
    return apply_filters('bp_core_avatar_cropstore', array('v1_out' => $v1_out, 'v2_out' => $v2_out));
}
	function step_3() {
		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'];
		}

		$header = wp_crop_image($_POST['attachment_id'], $_POST['x1'], $_POST['y1'], $_POST['width'], $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT);
		$header = apply_filters('wp_create_file_in_uploads', $header); // For replication

		$parent = get_post($_POST['attachment_id']);

		$parent_url = $parent->guid;

		$url = str_replace(basename($parent_url), basename($header), $parent_url);

		set_theme_mod('header_image', $url);

		// cleanup
		$file = get_attached_file( $_POST['attachment_id'] );
		$medium = str_replace(basename($file), 'midsize-'.basename($file), $file);
		@unlink( apply_filters( 'wp_delete_file', $medium ) );
		wp_delete_attachment( $_POST['attachment_id'] );

		return $this->finished();
	}
 /**
  * Saves a new Site Icon.
  *
  * @since 4.3.0
  */
 public function set_site_icon()
 {
     check_admin_referer('set-site-icon');
     $attachment_id = absint($_REQUEST['attachment_id']);
     $create_new_attachement = !empty($_REQUEST['create-new-attachment']);
     /*
      * If the current attachment as been set as site icon don't delete it.
      */
     if (get_option('site_icon') == $attachment_id) {
         // Get the file path.
         $image_url = get_attached_file($attachment_id);
         // Update meta data and possibly regenerate intermediate sizes.
         add_filter('intermediate_image_sizes_advanced', array($this, 'additional_sizes'));
         $this->update_attachment_metadata($attachment_id, $image_url);
         remove_filter('intermediate_image_sizes_advanced', array($this, 'additional_sizes'));
     } else {
         // Delete any existing site icon images.
         $this->delete_site_icon();
         if (empty($_REQUEST['skip-cropping'])) {
             $cropped = wp_crop_image($attachment_id, $_REQUEST['crop-x'], $_REQUEST['crop-y'], $_REQUEST['crop-w'], $_REQUEST['crop-h'], $this->min_size, $this->min_size);
         } elseif ($create_new_attachement) {
             $cropped = _copy_image_file($attachment_id);
         } else {
             $cropped = get_attached_file($attachment_id);
         }
         if (!$cropped || is_wp_error($cropped)) {
             wp_die(__('Image could not be processed. Please go back and try again.'), __('Image Processing Error'));
         }
         $object = $this->create_attachment_object($cropped, $attachment_id);
         if ($create_new_attachement) {
             unset($object['ID']);
         }
         // Update the attachment.
         add_filter('intermediate_image_sizes_advanced', array($this, 'additional_sizes'));
         $attachment_id = $this->insert_attachment($object, $cropped);
         remove_filter('intermediate_image_sizes_advanced', array($this, 'additional_sizes'));
         // Save the site_icon data into option
         update_option('site_icon', $attachment_id);
     }
     add_settings_error('site-icon', 'icon-updated', __('Site Icon updated.'), 'updated');
 }
Beispiel #9
0
function upme_initialize_upload_box()
{
    global $current_user, $upme_save;
    $id = $_GET['upme_id'];
    $meta = isset($_GET['upme_meta']) ? $_GET['upme_meta'] : '';
    $disabled = isset($_GET['upme_disabled']) ? $_GET['upme_disabled'] : '';
    $settings = get_option('upme_options');
    $display = '<html>
                    <head>
                        ' . upme_crop_iframe_head() . '
                        <style type="text/css">
                            html{
                                overflow: hidden;
                            }
                            
                        </style>
                    </head>
                    <body>
                        <form id="upme-crop-frm" action="" method="post" enctype="multipart/form-data">';
    $display .= '           <div class="upme-crop-wrap">';
    $display .= '           <div class="upme-wrap">';
    $display .= '               <div class="upme-field upme-separator upme-edit upme-clearfix" style="display: block;">' . __('Update Profile Picture', 'upme') . '</div>';
    $profile_pic_url = get_the_author_meta($meta, $id);
    if (is_array($upme_save->errors) && count($upme_save->errors) != 0) {
        if (($id == $current_user->ID || current_user_can('edit_users')) && is_numeric($id)) {
            $display .= upme_display_upload_box($id, $meta, $disabled, $profile_pic_url, 'block');
            $display .= upme_display_crop_box($id, $meta, $profile_pic_url, 'none');
        }
    } elseif (isset($_POST['upme-upload-submit-' . $id]) || isset($_POST['upme-crop-request-' . $id])) {
        // Display crop area on file upload or crop link click
        if (($id == $current_user->ID || current_user_can('edit_users')) && is_numeric($id)) {
            $display .= upme_display_crop_box($id, $meta, $profile_pic_url, 'block');
        }
    } elseif (isset($_POST['upme-crop-submit-' . $id])) {
        // Crop the image on area selection and submit
        $data_x1 = isset($_POST['upme-crop-x1']) ? $_POST['upme-crop-x1'] : 0;
        $data_y1 = isset($_POST['upme-crop-y1']) ? $_POST['upme-crop-y1'] : 0;
        $data_width = isset($_POST['upme-crop-width']) ? $_POST['upme-crop-width'] : 50;
        $data_height = isset($_POST['upme-crop-height']) ? $_POST['upme-crop-height'] : 50;
        $src = get_the_author_meta($meta, $id);
        $upme_upload_path = '';
        $upme_upload_url = '';
        if ($upload_dir = upme_get_uploads_folder_details()) {
            $upme_upload_path = $upload_dir['basedir'] . "/upme/";
            $upme_upload_url = $upload_dir['baseurl'] . "/upme/";
            $src = str_replace($upme_upload_url, $upme_upload_path, $src);
        }
        if (is_readable($src)) {
            $result = wp_crop_image($src, $data_x1, $data_y1, $data_width, $data_height, $data_width, $data_height);
            if (!is_wp_error($result)) {
                $cropped_path = str_replace($upme_upload_path, $upme_upload_url, $result);
                update_user_meta($id, $meta, $cropped_path);
                $display .= upme_display_upload_box($id, $meta, $disabled, $profile_pic_url, 'block');
            }
        }
        update_crop_image_display($id, $meta, $cropped_path);
    } elseif (isset($_POST['upme-crop-save-' . $id])) {
        $src = get_the_author_meta($meta, $id);
        update_crop_image_display($id, $meta, $src);
    } else {
        if (($id == $current_user->ID || current_user_can('edit_users')) && is_numeric($id)) {
            $display .= upme_display_upload_box($id, $meta, $disabled, $profile_pic_url, 'block');
            $display .= upme_display_crop_box($id, $meta, $profile_pic_url, 'none');
        }
    }
    $display .= '           </div>';
    $display .= '           </div>';
    $display .= '       </form>
                    </body>
                </html>';
    echo $display;
    exit;
}
 /**
  * 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')) {
         wp_die(__('Cheatin&#8217; uh?'));
     }
     if (!empty($_POST['skip-cropping']) && !(current_theme_supports('custom-header', 'flex-height') || current_theme_supports('custom-header', 'flex-width'))) {
         wp_die(__('Cheatin&#8217; 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);
     $max_width = 0;
     // For flex, limit size of image displayed to 1500px unless theme says otherwise
     if (current_theme_supports('custom-header', 'flex-width')) {
         $max_width = 1500;
     }
     if (current_theme_supports('custom-header', 'max-width')) {
         $max_width = max($max_width, get_theme_support('custom-header', 'max-width'));
     }
     $max_width = max($max_width, get_theme_support('custom-header', 'width'));
     if (current_theme_supports('custom-header', 'flex-height') && !current_theme_supports('custom-header', 'flex-width') || $_POST['width'] > $max_width) {
         $dst_height = absint($_POST['height'] * ($max_width / $_POST['width']));
     } elseif (current_theme_supports('custom-header', 'flex-height') && current_theme_supports('custom-header', 'flex-width')) {
         $dst_height = absint($_POST['height']);
     } else {
         $dst_height = get_theme_support('custom-header', 'height');
     }
     if (current_theme_supports('custom-header', 'flex-width') && !current_theme_supports('custom-header', 'flex-height') || $_POST['width'] > $max_width) {
         $dst_width = absint($_POST['width'] * ($max_width / $_POST['width']));
     } elseif (current_theme_supports('custom-header', 'flex-width') && current_theme_supports('custom-header', 'flex-height')) {
         $dst_width = absint($_POST['width']);
     } else {
         $dst_width = get_theme_support('custom-header', 'width');
     }
     if (empty($_POST['skip-cropping'])) {
         $cropped = wp_crop_image($attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $dst_width, $dst_height);
     } elseif (!empty($_POST['create-new-attachment'])) {
         $cropped = _copy_image_file($attachment_id);
     } else {
         $cropped = get_attached_file($attachment_id);
     }
     if (!$cropped || is_wp_error($cropped)) {
         wp_die(__('Image could not be processed. Please go back and try again.'), __('Image Processing Error'));
     }
     /** This filter is documented in wp-admin/custom-header.php */
     $cropped = apply_filters('wp_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);
     $size = @getimagesize($cropped);
     $image_type = $size ? $size['mime'] : 'image/jpeg';
     // Construct the object array
     $object = array('ID' => $attachment_id, 'post_title' => basename($cropped), 'post_content' => $url, 'post_mime_type' => $image_type, 'guid' => $url, 'context' => 'custom-header');
     if (!empty($_POST['create-new-attachment'])) {
         unset($object['ID']);
     }
     // Update the attachment
     $attachment_id = wp_insert_attachment($object, $cropped);
     wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $cropped));
     $width = $dst_width;
     $height = $dst_height;
     $this->set_header_image(compact('url', 'attachment_id', 'width', 'height'));
     // cleanup
     $medium = str_replace(basename($original), 'midsize-' . basename($original), $original);
     if (file_exists($medium)) {
         /**
          * Filter the path of the file to delete.
          *
          * @since 2.1.0
          *
          * @param string $medium Path to the file to delete.
          */
         @unlink(apply_filters('wp_delete_file', $medium));
     }
     if (empty($_POST['create-new-attachment']) && empty($_POST['skip-cropping'])) {
         /** This filter is documented in wp-admin/custom-header.php */
         @unlink(apply_filters('wp_delete_file', $original));
     }
     return $this->finished();
 }
 /**
  * Saves a new Site Icon.
  *
  * @since 4.3.0
  */
 public function set_site_icon()
 {
     check_admin_referer('set-site-icon');
     // Delete any existing site icon images.
     $this->delete_site_icon();
     $attachment_id = absint($_POST['attachment_id']);
     // TODO
     if (empty($_POST['skip-cropping'])) {
         $crop_ratio = (double) $_POST['crop_ratio'];
         $crop_data = $this->convert_coordinates_from_resized_to_full($_POST['crop-x'], $_POST['crop-y'], $_POST['crop-w'], $_POST['crop-h'], $crop_ratio);
         $cropped = wp_crop_image($attachment_id, $crop_data['crop_x'], $crop_data['crop_y'], $crop_data['crop_width'], $crop_data['crop_height'], $this->min_size, $this->min_size);
     } elseif (!empty($_POST['create-new-attachment'])) {
         $cropped = _copy_image_file($attachment_id);
     } else {
         $cropped = get_attached_file($attachment_id);
     }
     if (!$cropped || is_wp_error($cropped)) {
         wp_die(__('Image could not be processed. Please go back and try again.'), __('Image Processing Error'));
     }
     $object = $this->create_attachment_object($cropped, $attachment_id);
     if (!empty($_POST['create-new-attachment'])) {
         unset($object['ID']);
     }
     // Update the attachment
     add_filter('intermediate_image_sizes_advanced', array($this, 'additional_sizes'));
     $attachment_id = $this->insert_attachment($object, $cropped);
     remove_filter('intermediate_image_sizes_advanced', array($this, 'additional_sizes'));
     // Save the site_icon data into option
     update_option('site_icon', $attachment_id);
     add_settings_error('site-icon', 'icon-updated', __('Site Icon updated.'), 'updated');
 }
Beispiel #12
0
/**
 * Crop an uploaded avatar.
 * 
 * originally from bp file <>. It was copied since naming it does not have any filters
 * for renaming and changing the cropped avatae
 *
 *  $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
 *
 * @param array $args {
 *     Array of function parameters.
 *     @type string $object Object type of the item whose avatar you're
 *           handling. 'user', 'group', 'blog', or custom. Default: 'user'.
 *     @type string $avatar_dir Subdirectory where avatar should be stored.
 *           Default: 'avatars'.
 *     @type bool|int $item_id ID of the item that the avatar belongs to.
 *     @type bool|string $original_file Absolute papth to the original avatar
 *           file.
 *     @type int $crop_w Crop width. Default: the global 'full' avatar width,
 *           as retrieved by bp_core_avatar_full_width().
 *     @type int $crop_h Crop height. Default: the global 'full' avatar height,
 *           as retrieved by bp_core_avatar_full_height().
 *     @type int $crop_x The horizontal starting point of the crop. Default: 0.
 *     @type int $crop_y The vertical starting point of the crop. Default: 0.
 * }
 * @return bool True on success, false on failure.
 */
function bcp_core_avatar_handle_crop($args = '')
{
    $existing_avatar = '';
    $coverphoto_full_width = BCP_MAX_WIDTH;
    $coverphoto_full_height = BCP_MAX_HEIGHT;
    $coverphoto_thumb_full_width = BCP_THUMB_MAX_WIDTH;
    $coverphoto_thumb_full_height = BCP_THUMB_MAX_HEIGHT;
    $r = wp_parse_args($args, 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));
    /***
     * 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 (empty($original_file)) {
        return false;
    }
    $original_file = bp_core_avatar_upload_path() . $original_file;
    if (!file_exists($original_file)) {
        return false;
    }
    if (empty($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 . '/wp-admin/includes/image.php';
    require_once ABSPATH . '/wp-admin/includes/file.php';
    // Delete the existing avatar files for the object
    $args = array('object_id' => $item_id, 'type' => 'user');
    //change object type to groups for groups
    if ('group-avatars' == $avatar_dir) {
        $args['type'] = 'groups';
    }
    $existing_covers = bcp_fetch_cover_photo($args);
    if (!empty($existing_covers)) {
        // Check that the new avatar doesn't have the same name as the
        // old one before deleting
        $upload_dir = wp_upload_dir();
        $existing_avatar_path = str_replace($upload_dir['baseurl'], '', $existing_avatar);
        $new_avatar_path = str_replace($upload_dir['basedir'], '', $original_file);
        if ($existing_avatar_path !== $new_avatar_path) {
            if ($handle = opendir($avatar_folder_dir)) {
                while (false !== ($entry = readdir($handle))) {
                    if ($entry != "." && $entry != "..") {
                        $file_info = pathinfo($entry);
                        $file_name = $file_info['filename'];
                        $file_ext = $file_info['extension'];
                        $cover_photos = array('coverphoto-full', 'coverphoto-thumb');
                        if (in_array($file_name, $cover_photos)) {
                            // cover photo exists
                            $file = $avatar_folder_dir . '/' . $file_name . '.' . $file_ext;
                            @unlink($file);
                        }
                    }
                }
                // close the directory
                closedir($handle);
            }
        }
    }
    // Make sure we at least have a width and height for cropping
    if (empty($crop_w)) {
        $crop_w = bp_core_avatar_full_width();
    }
    if (empty($crop_h)) {
        $crop_h = bp_core_avatar_full_height();
    }
    // Get the file extension
    $data = @getimagesize($original_file);
    $ext = $data['mime'] == 'image/png' ? 'png' : 'jpg';
    // Set the full and thumb filenames
    $full_filename = 'coverphoto-full.' . $ext;
    $thumb_filename = 'coverphoto-thumb.' . $ext;
    // Crop the image
    $full_cropped = wp_crop_image($original_file, (int) $crop_x, (int) $crop_y, (int) $crop_w, (int) $crop_h, $coverphoto_thumb_width, $coverphoto_thumb_height, false, $avatar_folder_dir . '/' . $full_filename);
    $thumb_cropped = wp_crop_image($original_file, (int) $crop_x, (int) $crop_y, (int) $crop_w, (int) $crop_h, $coverphoto_thumb_full_width, $coverphoto_thumb_full_height, false, $avatar_folder_dir . '/' . $thumb_filename);
    // Check for errors
    if (empty($full_cropped) || empty($thumb_cropped) || is_wp_error($full_cropped) || is_wp_error($thumb_cropped)) {
        return false;
    }
    // Remove the original
    @unlink($original_file);
    return true;
}
 /**
  * Save options on the VFB Pro > Email Design page
  *
  * @access public
  * @since 2.4.3
  * @return void
  */
 public function save_email_design()
 {
     global $wpdb;
     if (!isset($_REQUEST['action']) || !isset($_GET['page'])) {
         return;
     }
     if ('vfb-email-design' !== $_GET['page']) {
         return;
     }
     if ('email_design' !== $_REQUEST['action']) {
         return;
     }
     $form_id = absint($_REQUEST['form_id']);
     check_admin_referer('update-design-' . $form_id);
     $email = unserialize($wpdb->get_var($wpdb->prepare("SELECT form_email_design FROM {$this->form_table_name} WHERE form_id = %d", $form_id)));
     $header_image = !empty($email['header_image']) ? $email['header_image'] : '';
     if (isset($_FILES['header_image'])) {
         $value = $_FILES['header_image'];
         if ($value['size'] > 0) {
             // Handle the upload using WP's wp_handle_upload function. Takes the posted file and an options array
             $uploaded_file = wp_handle_upload($value, array('test_form' => false));
             @(list($width, $height, $type, $attr) = getimagesize($uploaded_file['file']));
             if ($width == 600 && $height == 137) {
                 $header_image = isset($uploaded_file['file']) ? $uploaded_file['url'] : '';
             } elseif ($width > 600) {
                 $oitar = $width / 600;
                 $image = wp_crop_image($uploaded_file['file'], 0, 0, $width, $height, 600, $height / $oitar, false, str_replace(basename($uploaded_file['file']), 'vfb-header-img-' . basename($uploaded_file['file']), $uploaded_file['file']));
                 if (is_wp_error($image)) {
                     wp_die(__('Image could not be processed.  Please go back and try again.'), __('Image Processing Error'));
                 }
                 $header_image = str_replace(basename($uploaded_file['url']), basename($image), $uploaded_file['url']);
             } else {
                 $dst_width = 600;
                 $dst_height = absint($height * (600 / $width));
                 $cropped = wp_crop_image($uploaded_file['file'], 0, 0, $width, $height, $dst_width, $dst_height, false, str_replace(basename($uploaded_file['file']), 'vfb-header-img-' . basename($uploaded_file['file']), $uploaded_file['file']));
                 if (is_wp_error($cropped)) {
                     wp_die(__('Image could not be processed.  Please go back and try again.'), __('Image Processing Error'));
                 }
                 $header_image = str_replace(basename($uploaded_file['url']), basename($cropped), $uploaded_file['url']);
             }
         }
     }
     $email_design = array('format' => $_REQUEST['format'], 'link_love' => $_REQUEST['link_love'], 'footer_text' => $_REQUEST['footer_text'], 'background_color' => $_REQUEST['background_color'], 'header_text' => $_REQUEST['header_text'], 'header_image' => $header_image, 'header_color' => $_REQUEST['header_color'], 'header_text_color' => $_REQUEST['header_text_color'], 'fieldset_color' => $_REQUEST['fieldset_color'], 'section_color' => $_REQUEST['section_color'], 'section_text_color' => $_REQUEST['section_text_color'], 'text_color' => $_REQUEST['text_color'], 'link_color' => $_REQUEST['link_color'], 'row_color' => $_REQUEST['row_color'], 'row_alt_color' => $_REQUEST['row_alt_color'], 'border_color' => $_REQUEST['border_color'], 'footer_color' => $_REQUEST['footer_color'], 'footer_text_color' => $_REQUEST['footer_text_color'], 'font_family' => $_REQUEST['font_family'], 'header_font_size' => $_REQUEST['header_font_size'], 'fieldset_font_size' => $_REQUEST['fieldset_font_size'], 'section_font_size' => $_REQUEST['section_font_size'], 'text_font_size' => $_REQUEST['text_font_size'], 'footer_font_size' => $_REQUEST['footer_font_size']);
     // Update form details
     $wpdb->update($this->form_table_name, array('form_email_design' => serialize($email_design)), array('form_id' => $form_id), array('%s'), array('%d'));
 }
 public function handle_crop()
 {
     require_once ABSPATH . '/wp-admin/includes/image.php';
     $user_id = bp_displayed_user_id();
     $cover_photo = get_transient('profile_cover_photo_' . $user_id);
     // Get the file extension
     $data = @getimagesize($cover_photo);
     $ext = $data['mime'] == 'image/png' ? 'png' : 'jpg';
     $base_filename = basename($cover_photo, '.' . $ext);
     // create a new filename but, if it's already been cropped, strip out the -cropped
     $new_filename = str_replace('-cropped', '', $base_filename) . '-cropped.' . $ext;
     $new_filepath = bp_core_avatar_upload_path() . '/cover-photo/' . $user_id . '/' . $new_filename;
     $new_fileurl = bp_core_avatar_url() . '/cover-photo/' . $user_id . '/' . $new_filename;
     $crop_fileurl = str_replace(trailingslashit(get_home_url()), '', bp_core_avatar_url()) . '/cover-photo/' . $user_id . '/' . $new_filename;
     // delete the old cover photo if it exists
     if (file_exists($new_filepath)) {
         @unlink($new_filepath);
     }
     $cropped_header = wp_crop_image($cover_photo, $_POST['x'], $_POST['y'], $_POST['w'], $_POST['h'], $this->width, $this->height, false, $crop_fileurl);
     if (!is_wp_error($cropped_header)) {
         $old_file_path = get_user_meta(bp_loggedin_user_id(), 'profile_cover_photo_path', true);
         if (file_exists($old_file_path)) {
             @unlink($old_file_path);
         }
         // update with the new image and path
         bp_update_user_meta(bp_loggedin_user_id(), 'profile_cover_photo', $new_fileurl);
         bp_update_user_meta(bp_loggedin_user_id(), 'profile_cover_photo_path', $new_filepath);
         delete_transient('is_cover_photo_uploaded_' . bp_displayed_user_id());
         delete_transient('profile_cover_photo_' . bp_displayed_user_id());
     }
 }
Beispiel #15
0
function step_3()
{
    //check_admin_referer('custom-header-crop-image');
    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'];
    }
    $custom = get_post_custom($_POST['banner']);
    $banner_height = $custom['height'][0];
    $banner_width = $custom['width'][0];
    $original_id = get_attached_file($_POST['attachment_id']);
    $cropped = wp_crop_image($original_id, $_POST['x1'], $_POST['y1'], $_POST['width'], $_POST['height'], $banner_width, $banner_height);
    if (is_wp_error($cropped)) {
        wp_die(__('Image could not be processed.  Please go back and try again.'), __('Image Processing Error'));
    }
    $cropped = apply_filters('wp_create_file_in_uploads', $cropped, $original_id);
    // For replication
    $parent = get_post($original_id);
    $parent_url = $parent->guid;
    $url = str_replace(basename($parent_url), basename($cropped), $parent_url);
    // Construct the object array
    $object = array('post_title' => basename($cropped), 'post_content' => $url, 'post_mime_type' => 'image/jpeg', 'guid' => $url);
    // Update the attachment
    $cropped_image = wp_insert_attachment($object, $cropped);
    require_once ABSPATH . 'wp-admin/includes/image.php';
    $attach_data = wp_generate_attachment_metadata($cropped_image, $cropped);
    wp_update_attachment_metadata($cropped_image, $attach_data);
    $cropped_image_url = wp_get_attachment_url($cropped_image);
    $cropped_image_id = $cropped_image;
    $sliderID = $_POST['banner'];
    $url = wp_get_attachment_url($cropped_image_id);
    $custom = get_post_custom($sliderID);
    $banner_height = $custom['height'][0];
    $banner_width = $custom['width'][0];
    $alcyone_slides = $custom['slides'][0];
    $i = 0;
    $loop = true;
    while ($loop) {
        //for ($i=0;$i<10;$i++){
        $i++;
        if ($custom['alcyone_slide_' . $i][0] == "") {
            update_post_meta($sliderID, 'alcyone_slide_' . $i, $cropped_image_id);
            update_post_meta($sliderID, 'slides', $alcyone_slides + 1);
            $slide_id = 'alcyone_slide_' . $i;
            $loop = false;
        }
    }
    ?>
		<li>
			<img src="<?php 
    echo $url;
    ?>
" width="<?php 
    echo $prew_width;
    ?>
" /></br>
			<input type="hidden" id="image_id" value="<?php 
    echo $cropped_image_id;
    ?>
"/>
			<input type="hidden" id="slide_id" value="<?php 
    echo $slide_id;
    ?>
"/>
			<input type="submit" value="" class="settings"/>						              
			<input type="submit" value="" class="delete"/>                
		</li>
		<?php 
    // cleanup
    $medium = str_replace(basename($original), 'midsize-' . basename($original), $original);
    @unlink(apply_filters('wp_delete_file', $medium));
    @unlink(apply_filters('wp_delete_file', $original));
    //return finished();
    die;
}
Beispiel #16
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')) {
         wp_die(__('Cheatin&#8217; 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);
     $max_width = 0;
     // For flex, limit size of image displayed to 1500px unless theme says otherwise
     if (current_theme_supports('custom-header', 'flex-width')) {
         $max_width = 1500;
     }
     if (current_theme_supports('custom-header', 'max-width')) {
         $max_width = max($max_width, get_theme_support('custom-header', 'max-width'));
     }
     $max_width = max($max_width, get_theme_support('custom-header', 'width'));
     if (current_theme_supports('custom-header', 'flex-height') && !current_theme_supports('custom-header', 'flex-width') || $_POST['width'] > $max_width) {
         $dst_height = absint($_POST['height'] * ($max_width / $_POST['width']));
     } elseif (current_theme_supports('custom-header', 'flex-height') && current_theme_supports('custom-header', 'flex-width')) {
         $dst_height = absint($_POST['height']);
     } else {
         $dst_height = get_theme_support('custom-header', 'height');
     }
     if (current_theme_supports('custom-header', 'flex-width') && !current_theme_supports('custom-header', 'flex-height') || $_POST['width'] > $max_width) {
         $dst_width = absint($_POST['width'] * ($max_width / $_POST['width']));
     } elseif (current_theme_supports('custom-header', 'flex-width') && current_theme_supports('custom-header', 'flex-height')) {
         $dst_width = absint($_POST['width']);
     } else {
         $dst_width = get_theme_support('custom-header', 'width');
     }
     $cropped = wp_crop_image($attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $dst_width, $dst_height);
     if (is_wp_error($cropped)) {
         wp_die(__('Image could not be processed. Please go back and try again.'), __('Image Processing Error'));
     }
     $cropped = apply_filters('wp_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');
     if (isset($_POST['new-attachment']) && $_POST['new-attachment']) {
         unset($object['ID']);
     }
     // Update the attachment
     $attachment_id = wp_insert_attachment($object, $cropped);
     wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $cropped));
     update_post_meta($attachment_id, '_wp_attachment_is_custom_header', get_option('stylesheet'));
     set_theme_mod('header_image', $url);
     $header_data = new stdClass();
     $header_data->attachment_id = $attachment_id;
     $header_data->url = $url;
     $header_data->thumbnail_url = $url;
     $header_data->width = $dst_width;
     $header_data->height = $dst_height;
     set_theme_mod('header_image_data', $header_data);
     // cleanup
     $medium = str_replace(basename($original), 'midsize-' . basename($original), $original);
     if (file_exists($medium)) {
         @unlink(apply_filters('wp_delete_file', $medium));
     }
     if (empty($_POST['new-attachment'])) {
         @unlink(apply_filters('wp_delete_file', $original));
     }
     return $this->finished();
 }
Beispiel #17
0
 /**
  * Add a new image size to the an already existing attachment. Handy for one
  * off spaces where you don't want to have an image size created for every
  * attachment uploaded. Once created the image will be accessable via the
  * normal wp route with your size name.
  *
  * @param integer $id          ID of the attachment we want to mod
  * @param integer $crop_width  The width of the new image in px
  * @param integer $crop_height Height of the new image in px
  * @param boolean $crop        Are we going to crop this image
  * @param string $size_name   Name of the image size
  *
  * @return Mixed wp_error or string, wp_error on fail otherwise the url to the new image.
  */
 function add_resized_attachment($id, $crop_width, $crop_height, $crop = false, $size_name = 'special-size')
 {
     if (!wp_attachment_is_image($id)) {
         return new WP_Error('attachment_invalid', sprintf(__('The passed ID (%d) is not that of an attachment or an image.', icit_core::DOM), $id));
     }
     // If it's been created already lets not do anything.
     $image_auto = image_get_intermediate_size($id, $size_name);
     if ($crop === true && (int) $image_auto['width'] == $crop_width && (int) $image_auto['height'] == $crop_height && $image_auto['url']) {
         return $image_auto['url'];
     } elseif ($crop === false && ($crop_width == $image_auto['width'] || $crop_height == $image_auto['height']) && $image_auto['url']) {
         return $image_auto['url'];
     }
     // Make sure we've got the right tools to hand.
     if (!function_exists('wp_crop_image') && file_exists(ABSPATH . '/wp-admin/includes/image.php')) {
         require ABSPATH . '/wp-admin/includes/image.php';
     }
     //	Collect and check the attachments meta data.
     $file = get_attached_file($id);
     $attachment_meta = wp_get_attachment_metadata($id, true);
     // Can't do much if there is no file listed on the attachment.
     if ($file == '' || !preg_match('/(png|jpg|gif|jpeg)$/i', $file)) {
         return new WP_Error('attachment_invalid', __('The attachment has not got a file path attached.', icit_core::DOM));
     }
     // Collect Width and Height
     $width = $attachment_meta['width'];
     $height = $attachment_meta['height'];
     if ($width == '' || $height == '') {
         list($width, $height) = getimagesize($file);
     }
     // The image is broken, probabaly a bmp
     if ((int) $width == 0 || (int) $height == 0) {
         return new WP_Error('attachment_invalid', __('The attachment is reporting a width or height of zero.', icit_core::DOM));
     }
     // The image is already the right size @todo Add this the intermediate image size array
     if ($width == $crop_width && $height == $crop_height) {
         list($image, $width, $hight) = wp_get_attachment_image_src($id, 'fullsize');
         return $image;
     }
     // If the Crop Width == 0 then I'll assume that dimension is to remain unchanged
     if ($crop_width == 0) {
         $crop_width = $width;
     }
     // Height is 0 so don't change from source only width is to change.
     if ($crop_height == 0) {
         $crop_height = $height;
     }
     // Init the cropped image var
     $cropped_image = false;
     // Image isn't to be cropped or scaled up just a new size created if needed
     if ($crop === false) {
         list($new_width, $new_height) = wp_constrain_dimensions($width, $height, $crop_width, $crop_height);
         //	Find a unique filename in the destination directory.
         $new_name = preg_replace('/^([^\\.]*)\\.(.*)$/', "\$1-{$new_width}x{$new_height}.\$2", basename($file));
         $file_name = dirname($file) . '/' . wp_unique_filename(dirname($file), $new_name);
         $cropped_image = wp_crop_image($file, 0, 0, $width, $height, $new_width, $new_height, false, $file_name);
     } else {
         //	Find a unique filename in the destination directory.
         $new_name = preg_replace('/^([^\\.]*)\\.(.*)$/', "\$1-{$crop_width}x{$crop_height}.\$2", basename($file));
         $file_name = dirname($file) . '/' . wp_unique_filename(dirname($file), $new_name);
         if ($width < $crop_width && $height < $crop_height && $crop === true) {
             //Scale up
             if ($crop_width / $width * $height >= $crop_height) {
                 $new_height = ceil($crop_height / ($crop_width / $width));
                 $offset = ceil(($height - $new_height) / 2);
                 $cropped_image = wp_crop_image($file, 0, $offset, $width, $new_height, $crop_width, $crop_height, false, $file_name);
             } else {
                 $new_width = ceil($crop_width / ($crop_height / $height));
                 $offset = ceil(($width - $new_width) / 2);
                 $cropped_image = wp_crop_image($file, $offset, 0, $new_width, $height, $crop_width, $crop_height, false, $file_name);
             }
         } elseif (($width > $crop_width || $height > $crop_height) && $crop === true) {
             //Scale Down
             if ($height / ($width / $crop_width) < $crop_height) {
                 $new_width = ceil($height * ($crop_width / $crop_height));
                 $offset = ceil(($width - $new_width) / 2);
                 $cropped_image = wp_crop_image($file, $offset, 0, $new_width, $height, $crop_width, $crop_height, false, $file_name);
             } else {
                 $new_height = ceil($width * ($crop_height / $crop_width));
                 $offset = ceil(($height - $new_height) / 2);
                 $cropped_image = wp_crop_image($file, 0, $offset, $width, $new_height, $crop_width, $crop_height, false, $file_name);
             }
         }
     }
     // Something went wrong up there
     if (is_wp_error($cropped_image)) {
         return $cropped_image;
     }
     // Something didn't got quite as wrong but still not good
     if ($cropped_image === false || !file_exists($cropped_image)) {
         return new WP_Error('attachment_invalid', __('Couldn\'t create the new resized image.', icit_core::DOM));
     }
     // Grab the file size of our new image
     $new_sizes = getimagesize($cropped_image);
     // Add the new image size to the originals meta.
     $attachment_meta['sizes'][$size_name] = array('file' => basename($cropped_image), 'width' => absint($new_sizes[0]), 'height' => absint($new_sizes[1]));
     wp_update_attachment_metadata($id, $attachment_meta);
     // Lets send the url to the new image as the return.
     return wp_get_attachment_image_src($id, $size_name);
 }
Beispiel #18
0
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_AVATAR_FULL_WIDTH,
		'crop_h' => BP_AVATAR_FULL_HEIGHT,
		'crop_x' => 0,
		'crop_y' => 0
	);

	$r = wp_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_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_AVATAR_UPLOAD_PATH . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir );

	if ( !file_exists( $avatar_folder_dir ) )
		return false;

	require_once( ABSPATH . '/wp-admin/includes/image.php' );
	require_once( ABSPATH . '/wp-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_AVATAR_FULL_WIDTH;

	if ( !(int)$crop_h )
		$crop_h = BP_AVATAR_FULL_HEIGHT;

	/* Set the full and thumb filenames */
	$full_filename = wp_hash( $original_file . time() ) . '-bpfull.jpg';
	$thumb_filename = wp_hash( $original_file . time() ) . '-bpthumb.jpg';

	/* Crop the image */
	$full_cropped = wp_crop_image( $original_file, (int)$crop_x, (int)$crop_y, (int)$crop_w, (int)$crop_h, BP_AVATAR_FULL_WIDTH, BP_AVATAR_FULL_HEIGHT, false, $avatar_folder_dir . '/' . $full_filename );
	$thumb_cropped = wp_crop_image( $original_file, (int)$crop_x, (int)$crop_y, (int)$crop_w, (int)$crop_h, BP_AVATAR_THUMB_WIDTH, BP_AVATAR_THUMB_HEIGHT, false, $avatar_folder_dir . '/' . $thumb_filename );

	/* Remove the original */
	@unlink( $original_file );

	return true;
}
Beispiel #19
0
/**
 * user_avatar_add_photo_step3 function.
 * The Third Step in the Process
 * Description: Deletes previous uploaded picture and creates a new cropped image in its place. 
 * @access public
 * @param mixed $uid
 * @return void
 */
function user_avatar_add_photo_step3($uid)
{
    check_admin_referer('user-avatar');
    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'];
    }
    if (!file_exists($_POST['attachment_file'])) {
        echo "<div class='error'><p>Sorry, No file available</p></div>";
        return true;
    }
    $original_file = $_POST['attachment_file'];
    $cropped_full = USER_AVATAR_UPLOAD_PATH . "{$uid}/" . time() . "-bpfull.jpg";
    $cropped_thumb = USER_AVATAR_UPLOAD_PATH . "{$uid}/" . time() . "-bpthumb.jpg";
    // delete the previous files
    user_avatar_delete_files($uid);
    if (!file_exists(USER_AVATAR_UPLOAD_PATH . "{$uid}/")) {
        mkdir(USER_AVATAR_UPLOAD_PATH . "{$uid}/");
    }
    // update the files
    $cropped_full = wp_crop_image($original_file, $_POST['x1'], $_POST['y1'], $_POST['width'], $_POST['height'], USER_AVATAR_FULL_WIDTH, USER_AVATAR_FULL_HEIGHT, false, $cropped_full);
    $cropped_thumb = wp_crop_image($original_file, $_POST['x1'], $_POST['y1'], $_POST['width'], $_POST['height'], USER_AVATAR_THUMB_WIDTH, USER_AVATAR_THUMB_HEIGHT, false, $cropped_thumb);
    /* Remove the original */
    @unlink($original_file);
    if (is_wp_error($cropped_full)) {
        wp_die(__('Image could not be processed.  Please go back and try again.'), __('Image Processing Error'));
    }
    ?>
	
	<script type="text/javascript">
		self.parent.user_avatar_refresh_image('<?php 
    echo get_avatar($uid, 150);
    ?>
');
	</script>
	<div id="user-avatar-step3">
		<h3>Here's your new profile picture...</h3>
		<span style="float:left;">
		<?php 
    echo get_avatar($uid, 150);
    ?>
		</span>
		<a id="user-avatar-step3-close" class="button" onclick="self.parent.tb_remove();" >Close</a>
	</div>
<?php 
}
Beispiel #20
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
 *
 * @param array $args {
 *     Array of function parameters.
 *     @type string $object Object type of the item whose avatar you're
 *           handling. 'user', 'group', 'blog', or custom. Default: 'user'.
 *     @type string $avatar_dir Subdirectory where avatar should be stored.
 *           Default: 'avatars'.
 *     @type bool|int $item_id ID of the item that the avatar belongs to.
 *     @type bool|string $original_file Absolute papth to the original avatar
 *           file.
 *     @type int $crop_w Crop width. Default: the global 'full' avatar width,
 *           as retrieved by bp_core_avatar_full_width().
 *     @type int $crop_h Crop height. Default: the global 'full' avatar height,
 *           as retrieved by bp_core_avatar_full_height().
 *     @type int $crop_x The horizontal starting point of the crop. Default: 0.
 *     @type int $crop_y The vertical starting point of the crop. Default: 0.
 * }
 * @return bool True on success, false on failure.
 */
function bp_core_avatar_handle_crop($args = '')
{
    $r = wp_parse_args($args, 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));
    /***
     * 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 (empty($original_file)) {
        return false;
    }
    $original_file = bp_core_avatar_upload_path() . $original_file;
    if (!file_exists($original_file)) {
        return false;
    }
    if (empty($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 . '/wp-admin/includes/image.php';
    require_once ABSPATH . '/wp-admin/includes/file.php';
    // Delete the existing avatar files for the object
    $existing_avatar = bp_core_fetch_avatar(array('object' => $object, 'item_id' => $item_id, 'html' => false));
    if (!empty($existing_avatar)) {
        // Check that the new avatar doesn't have the same name as the
        // old one before deleting
        $upload_dir = wp_upload_dir();
        $existing_avatar_path = str_replace($upload_dir['baseurl'], '', $existing_avatar);
        $new_avatar_path = str_replace($upload_dir['basedir'], '', $original_file);
        if ($existing_avatar_path !== $new_avatar_path) {
            bp_core_delete_existing_avatar(array('object' => $object, 'item_id' => $item_id, 'avatar_path' => $avatar_folder_dir));
        }
    }
    // Make sure we at least have a width and height for cropping
    if (empty($crop_w)) {
        $crop_w = bp_core_avatar_full_width();
    }
    if (empty($crop_h)) {
        $crop_h = bp_core_avatar_full_height();
    }
    // Get the file extension
    $data = @getimagesize($original_file);
    $ext = $data['mime'] == 'image/png' ? 'png' : 'jpg';
    // Set the full and thumb filenames
    $full_filename = wp_hash($original_file . time()) . '-bpfull.' . $ext;
    $thumb_filename = wp_hash($original_file . time()) . '-bpthumb.' . $ext;
    // Crop the image
    $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);
    $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);
    // Check for errors
    if (empty($full_cropped) || empty($thumb_cropped) || is_wp_error($full_cropped) || is_wp_error($thumb_cropped)) {
        return false;
    }
    // Remove the original
    @unlink($original_file);
    return true;
}
Beispiel #21
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 = wp_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 . '/wp-admin/includes/image.php';
    require_once ABSPATH . '/wp-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 = wp_hash($original_file . time()) . '-bpfull.jpg';
    $thumb_filename = wp_hash($original_file . time()) . '-bpthumb.jpg';
    // Crop the image
    $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);
    $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);
    // Remove the original
    @unlink($original_file);
    return true;
}
 /**
  * Crop an image file.
  *
  * @since 2.3.0
  *
  * @param array $args {
  *     @type string $original_file The source file (absolute path) for the Attachment.
  *     @type int    $crop_x        The start x position to crop from.
  *     @type int    $crop_y        The start y position to crop from.
  *     @type int    $crop_w        The width to crop.
  *     @type int    $crop_h        The height to crop.
  *     @type int    $dst_w         The destination width.
  *     @type int    $dst_h         The destination height.
  *     @type int    $src_abs       Optional. If the source crop points are absolute.
  *     @type string $dst_file      Optional. The destination file to write to.
  * }
  *
  * @return string|WP_Error New filepath on success, WP_Error on failure.
  */
 public function crop($args = array())
 {
     $wp_error = new WP_Error();
     $r = bp_parse_args($args, array('original_file' => '', 'crop_x' => 0, 'crop_y' => 0, 'crop_w' => 0, 'crop_h' => 0, 'dst_w' => 0, 'dst_h' => 0, 'src_abs' => false, 'dst_file' => false), 'bp_attachment_crop_args');
     if (empty($r['original_file']) || !file_exists($r['original_file'])) {
         $wp_error->add('crop_error', __('Cropping the file failed: missing source file.', 'buddypress'));
         return $wp_error;
     }
     // Check image file pathes.
     $path_error = __('Cropping the file failed: the file path is not allowed.', 'buddypress');
     // Make sure it's coming from an uploaded file.
     if (false === strpos($r['original_file'], $this->upload_path)) {
         $wp_error->add('crop_error', $path_error);
         return $wp_error;
     }
     /**
      * If no destination file is provided, WordPress will use a default name
      * and will write the file in the source file's folder.
      * If a destination file is provided, we need to make sure it's going into uploads.
      */
     if (!empty($r['dst_file']) && false === strpos($r['dst_file'], $this->upload_path)) {
         $wp_error->add('crop_error', $path_error);
         return $wp_error;
     }
     // Check image file types.
     $check_types = array('src_file' => array('file' => $r['original_file'], 'error' => _x('source file', 'Attachment source file', 'buddypress')));
     if (!empty($r['dst_file'])) {
         $check_types['dst_file'] = array('file' => $r['dst_file'], 'error' => _x('destination file', 'Attachment destination file', 'buddypress'));
     }
     /**
      * WordPress image supported types.
      * @see wp_attachment_is()
      */
     $supported_image_types = array('jpg' => 1, 'jpeg' => 1, 'jpe' => 1, 'gif' => 1, 'png' => 1);
     foreach ($check_types as $file) {
         $is_image = wp_check_filetype($file['file']);
         $ext = $is_image['ext'];
         if (empty($ext) || empty($supported_image_types[$ext])) {
             $wp_error->add('crop_error', sprintf(__('Cropping the file failed: %s is not a supported image file.', 'buddypress'), $file['error']));
             return $wp_error;
         }
     }
     // Add the image.php to the required WordPress files, if it's not already the case.
     $required_files = array_flip($this->required_wp_files);
     if (!isset($required_files['image'])) {
         $this->required_wp_files[] = 'image';
     }
     // Load the files.
     $this->includes();
     // Finally crop the image.
     return wp_crop_image($r['original_file'], (int) $r['crop_x'], (int) $r['crop_y'], (int) $r['crop_w'], (int) $r['crop_h'], (int) $r['dst_w'], (int) $r['dst_h'], $r['src_abs'], $r['dst_file']);
 }
 /**
  * @ticket 23325
  */
 public function test_wp_crop_image_error_on_saving()
 {
     WP_Image_Editor_Mock::$save_return = new WP_Error();
     add_filter('wp_image_editors', array($this, 'mock_image_editor'));
     $file = wp_crop_image(DIR_TESTDATA . '/images/canola.jpg', 0, 0, 100, 100, 100, 100);
     $this->assertInstanceOf('WP_Error', $file);
     remove_filter('wp_image_editors', array($this, 'mock_image_editor'));
     WP_Image_Editor_Mock::$save_return = array();
 }
Beispiel #24
0
 /**
  * Handle-function called via ajax request.
  * Check and crop multiple Images. Update with wp_update_attachment_metadata if needed.
  * Input parameters:
  *    * $_REQUEST['selection'] - json-object - data of the selection/crop
  *    * $_REQUEST['raw_values'] - json-object - data of the original image
  *    * $_REQUEST['active_values'] - json-array - array with data of the images to crop
  * The main code is wraped via try-catch - the errorMessage will send back to JavaScript for displaying in an alert-box.
  * Called die() at the end. 
  */
 function saveThumbnail()
 {
     global $cptSettings;
     $json_return = array();
     try {
         /** get data **/
         $upload_dir = wp_upload_dir();
         $tmp_dir = $upload_dir['basedir'] . "/tmp/";
         $options = $cptSettings->getOptions();
         //from $_REQUEST
         $selection = json_decode(stripcslashes($_REQUEST['selection']));
         $sourceImgData = json_decode(stripcslashes($_REQUEST['raw_values']));
         $targetImgData = json_decode(stripcslashes($_REQUEST['active_values']));
         //from DB
         $dbImageSizes = $cptSettings->getImageSizes();
         $obj = get_post($sourceImgData->id);
         $sourceImgPath = get_attached_file($obj->ID);
         $post_metadata = wp_get_attachment_metadata($obj->ID, true);
         //get the attachement metadata of the post
         $this->validation($selection, $obj, $sourceImgPath, $post_metadata);
         #$debug.= "\nselection\n".print_r($selection,true);
         #$debug.= "\ntargetImgData\n".print_r($sourceImgData,true);
         #$debug.= "\ntargetImgData\n".print_r($targetImgData,true);
         #$debug.= "\nimageObject\n".print_r($obj,true);
         #$debug.= "\nsource:".$sourceImgPath."\n";
         /**
          * will be true if the image format issnt in the attachements metadata, 
          * and wordpress dont know about the image-file
          */
         $_changed_image_format = false;
         $_processing_error = array();
         foreach ($targetImgData as $_imageSize) {
             $this->addDebug('submitted image-data');
             $this->addDebug(print_r($_imageSize, true));
             $_delete_old_file = '';
             if (!$this->isImageSizeValid($_imageSize, $dbImageSizes)) {
                 $this->addDebug("Image Size not valid.");
                 continue;
             }
             if (empty($post_metadata['sizes'][$_imageSize->name])) {
                 $_changed_image_format = true;
             } else {
                 //the old size hasent got the right image-size/image-ratio --> delete it or nobody will ever delete it correct
                 if ($post_metadata['sizes'][$_imageSize->name]['width'] != intval($_imageSize->width) || $post_metadata['sizes'][$_imageSize->name]['height'] != intval($_imageSize->height)) {
                     $_delete_old_file = $post_metadata['sizes'][$_imageSize->name]['file'];
                     $_changed_image_format = true;
                 }
             }
             $_filepath = $this->generateFilename($sourceImgPath, $_imageSize->width, $_imageSize->height);
             $_filepath_info = pathinfo($_filepath);
             $_tmp_filepath = $tmp_dir . $_filepath_info['basename'];
             $this->addDebug("filename:" . $_filepath);
             $crop_width = $_imageSize->width;
             $crop_height = $_imageSize->height;
             if (!$_imageSize->crop || $_imageSize->width == 0 || $_imageSize->height == 0) {
                 //handle images with no crop
                 $crop_width = $selection->x2 - $selection->x;
                 $crop_height = $selection->y2 - $selection->y;
             }
             $result = wp_crop_image(intval($sourceImgData->id), $selection->x, $selection->y, $selection->x2 - $selection->x, $selection->y2 - $selection->y, $crop_width, $crop_height, false, $_tmp_filepath);
             $_error = false;
             if (empty($result)) {
                 $_processing_error[] = sprintf(__('Cant generate filesize "%s".', CROP_THUMBS_LANG), $_imageSize->name);
                 $_error = true;
             } else {
                 if (!empty($_delete_old_file)) {
                     @unlink($_filepath_info['dirname'] . '/' . $_delete_old_file);
                 }
                 if (!@copy($result, $_filepath)) {
                     $_processing_error[] = sprintf(__('Cant copy temporary file to media-library.', CROP_THUMBS_LANG));
                     $_error = true;
                 }
                 if (!@unlink($result)) {
                     $_processing_error[] = sprintf(__('Cant delete temporary file.', CROP_THUMBS_LANG));
                     $_error = true;
                 }
             }
             if (!$_error) {
                 //update metadata --> otherwise new sizes will not be updated
                 $_new_meta = array('file' => $_filepath_info['basename'], 'width' => intval($crop_width), 'height' => intval($crop_height));
                 if (!empty($dbImageSizes[$_imageSize->name]['crop'])) {
                     $_new_meta['crop'] = $dbImageSizes[$_imageSize->name]['crop'];
                 }
                 $post_metadata['sizes'][$_imageSize->name] = $_new_meta;
             } else {
                 $this->addDebug('error on ' . $_filepath_info['basename']);
             }
         }
         //we have to update the posts metadate
         //otherwise new sizes will not be updated
         wp_update_attachment_metadata($obj->ID, $post_metadata);
         //generate result;
         $json_return['debug'] = $this->getDebugOutput($options);
         if (!empty($_processing_error)) {
             //one or more errors happend when generating thumbnails
             $json_return['processingErrors'] = implode("\n", $_processing_error);
         }
         if ($_changed_image_format) {
             //there was a change in the image-formats
             $json_return['changed_image_format'] = true;
         }
         $json_return['success'] = time();
         //time for cache-breaker
         echo json_encode($json_return);
     } catch (Exception $e) {
         $json_return['debug'] = $this->getDebugOutput($options);
         $json_return['error'] = $e->getMessage();
         echo json_encode($json_return);
     }
     die;
 }
 /**
  * Gets FB profile image and sets it as BuddyPress avatar.
  */
 function set_fb_image_as_bp_avatar($user_id, $me)
 {
     if (!defined('BP_VERSION')) {
         return true;
     }
     if (!function_exists('bp_core_avatar_upload_path')) {
         return true;
     }
     if (!$me || !@$me['id']) {
         return false;
     }
     $fb_uid = $me['id'];
     if (function_exists('xprofile_avatar_upload_dir')) {
         $xpath = xprofile_avatar_upload_dir(false, $user_id);
         $path = $xpath['path'];
     }
     if (!function_exists('xprofile_avatar_upload_dir') || empty($path)) {
         $object = 'user';
         $avatar_dir = apply_filters('bp_core_avatar_dir', 'avatars', $object);
         $path = bp_core_avatar_upload_path() . "/{$avatar_dir}/" . $user_id;
         $path = apply_filters('bp_core_avatar_folder_dir', $path, $user_id, $object, $avatar_dir);
         if (!realpath($path)) {
             @wp_mkdir_p($path);
         }
     }
     // Get FB picture
     //$fb_img = file_get_contents("http://graph.facebook.com/{$fb_uid}/picture?type=large");
     $page = wp_remote_get("http://graph.facebook.com/{$fb_uid}/picture?type=large", array('method' => 'GET', 'timeout' => '5', 'redirection' => '5', 'user-agent' => 'wdfb', 'blocking' => true, 'compress' => false, 'decompress' => true, 'sslverify' => false));
     if (is_wp_error($page)) {
         return false;
     }
     // Request fail
     if ((int) $page['response']['code'] != 200) {
         return false;
     }
     // Request fail
     $fb_img = $page['body'];
     $filename = md5($fb_uid);
     $filepath = "{$path}/{$filename}";
     file_put_contents($filepath, $fb_img);
     // Determine the right extension
     $info = getimagesize($filepath);
     $extension = false;
     if (function_exists('image_type_to_extension')) {
         $extension = image_type_to_extension($info[2], false);
     } else {
         switch ($info[2]) {
             case IMAGETYPE_GIF:
                 $extension = 'gif';
                 break;
             case IMAGETYPE_JPEG:
                 $extension = 'jpg';
                 break;
             case IMAGETYPE_PNG:
                 $extension = 'png';
                 break;
         }
     }
     // Unknown file type, clean up
     if (!$extension) {
         @unlink($filepath);
         return false;
     }
     $extension = 'jpeg' == strtolower($extension) ? 'jpg' : $extension;
     // Forcing .jpg extension for JPEGs
     // Clear old avatars
     $imgs = glob($path . '/*.{gif,png,jpg}', GLOB_BRACE);
     if (is_array($imgs)) {
         foreach ($imgs as $old) {
             @unlink($old);
         }
     }
     // Create and set new avatar
     if (defined('WDFB_BP_AVATAR_AUTO_CROP') && WDFB_BP_AVATAR_AUTO_CROP) {
         // Explicitly requested thumbnail processing
         // First, determine the centering position for cropping
         if ($info && isset($info[0]) && $info[0] && isset($info[1]) && $info[1]) {
             $full = apply_filters('wdfb-avatar-auto_crop', array('x' => (int) (($info[0] - bp_core_avatar_full_width()) / 2), 'y' => (int) (($info[1] - bp_core_avatar_full_height()) / 2), 'width' => bp_core_avatar_full_width(), 'height' => bp_core_avatar_full_height()), $filepath, $info);
         }
         $crop = $full ? wp_crop_image($filepath, $full['x'], $full['y'], $full['width'], $full['height'], bp_core_avatar_full_width(), bp_core_avatar_full_height(), false, "{$filepath}-bpfull.{$extension}") : false;
         if (!$crop) {
             @unlink($filepath);
             return false;
         }
         // Now, the thumbnail. First, try to resize the full avatar
         $thumb_file = wp_create_thumbnail("{$filepath}-bpfull.{$extension}", bp_core_avatar_thumb_width());
         if (!is_wp_error($thumb_file)) {
             // All good! We're done - clean up
             copy($thumb_file, "{$filepath}-bpthumb.{$extension}");
             @unlink($thumb_file);
         } else {
             // Sigh. Let's just fake it by using the original file then.
             copy("{$filepath}-bpfull.{$extension}", "{$filepath}-bpthumb.{$extension}");
         }
         @unlink($filepath);
         return true;
     } else {
         // No auto-crop, move on
         copy($filepath, "{$filepath}-bpfull.{$extension}");
         copy($filepath, "{$filepath}-bpthumb.{$extension}");
         @unlink($filepath);
         return true;
     }
     return false;
 }
 /**
  * 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')) {
         wp_die(__('Cheatin&#8217; 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 = wp_crop_image($attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT);
     if (is_wp_error($cropped)) {
         wp_die(__('Image could not be processed.  Please go back and try again.'), __('Image Processing Error'));
     }
     $cropped = apply_filters('wp_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
     wp_insert_attachment($object, $cropped);
     wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $cropped));
     update_post_meta($attachment_id, '_wp_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('wp_delete_file', $medium));
     @unlink(apply_filters('wp_delete_file', $original));
     return $this->finished();
 }
 function slide_image_crop()
 {
     global $wpdb;
     $filepath = $_REQUEST['image_org'];
     $myPostId = $_REQUEST['myPost_id'];
     $query = "SELECT ID FROM {$wpdb->posts} WHERE post_type='attachment' and guid='{$filepath}'";
     $id = $wpdb->get_var($query);
     if ($id != 0) {
         $img_x = $_REQUEST['image_x'];
         $img_y = $_REQUEST['image_y'];
         $img_w = $_REQUEST['image_w'];
         $img_h = $_REQUEST['image_h'];
         $src_file_path = get_attached_file($id);
         $dist_file = str_replace(basename($src_file_path), 'slider-' . $myPostId . '-' . basename($src_file_path), $src_file_path);
         $ff = wp_crop_image($src_file_path, $img_x, $img_y, $img_w, $img_h, $img_w, $img_h, false, $dist_file);
         if ($ff) {
             $filepath = str_replace(basename($src_file_path), 'slider-' . $myPostId . '-' . basename($src_file_path), $filepath);
             die($filepath);
         } else {
             die('0');
         }
     } else {
         die('0');
     }
 }