wp/wp-admin/includes/image-edit.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * WordPress Image Editor
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * @subpackage Administration
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     9
/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    10
 * Loads the WP image-editing interface.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    11
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    12
 * @param int         $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    13
 * @param bool|object $msg     Optional. Message to display for image editor updates or errors.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    14
 *                             Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    15
 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
function wp_image_editor($post_id, $msg = false) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
	$nonce = wp_create_nonce("image_editor-$post_id");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
	$meta = wp_get_attachment_metadata($post_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
	$thumb = image_get_intermediate_size($post_id, 'thumbnail');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
	$sub_sizes = isset($meta['sizes']) && is_array($meta['sizes']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
	$note = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
	if ( isset( $meta['width'], $meta['height'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
		$big = max( $meta['width'], $meta['height'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
		die( __('Image data does not exist. Please re-upload the image.') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
	$sizer = $big > 400 ? 400 / $big : 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
	$backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
	$can_restore = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
	if ( ! empty( $backup_sizes ) && isset( $backup_sizes['full-orig'], $meta['file'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
		$can_restore = $backup_sizes['full-orig']['file'] != basename( $meta['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
	if ( $msg ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
		if ( isset($msg->error) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
			$note = "<div class='error'><p>$msg->error</p></div>";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
		elseif ( isset($msg->msg) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
			$note = "<div class='updated'><p>$msg->msg</p></div>";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
	?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
	<div class="imgedit-wrap">
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    44
	<div id="imgedit-panel-<?php echo $post_id; ?>">
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    46
	<div class="imgedit-settings">
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
	<div class="imgedit-group">
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
	<div class="imgedit-group-top">
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    49
		<h3><?php _e( 'Scale Image' ); ?> <a href="#" class="dashicons dashicons-editor-help imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);return false;"></a></h3>
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
		<div class="imgedit-help">
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    51
		<p><?php _e('You can proportionally scale the original image. For best results, scaling should be done before you crop, flip, or rotate. Images can only be scaled down, not up.'); ?></p>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    52
		</div>
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
		<?php if ( isset( $meta['width'], $meta['height'] ) ): ?>
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    54
		<p><?php printf( __('Original dimensions %s'), $meta['width'] . ' &times; ' . $meta['height'] ); ?></p>
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
		<?php endif ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
		<div class="imgedit-submit">
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    57
		<span class="nowrap"><input type="text" id="imgedit-scale-width-<?php echo $post_id; ?>" onkeyup="imageEdit.scaleChanged(<?php echo $post_id; ?>, 1)" onblur="imageEdit.scaleChanged(<?php echo $post_id; ?>, 1)" style="width:4em;" value="<?php echo isset( $meta['width'] ) ? $meta['width'] : 0; ?>" /> &times; <input type="text" id="imgedit-scale-height-<?php echo $post_id; ?>" onkeyup="imageEdit.scaleChanged(<?php echo $post_id; ?>, 0)" onblur="imageEdit.scaleChanged(<?php echo $post_id; ?>, 0)" style="width:4em;" value="<?php echo isset( $meta['height'] ) ? $meta['height'] : 0; ?>" />
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
		<span class="imgedit-scale-warn" id="imgedit-scale-warn-<?php echo $post_id; ?>">!</span></span>
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    59
		<input type="button" onclick="imageEdit.action(<?php echo "$post_id, '$nonce'"; ?>, 'scale')" class="button button-primary" value="<?php esc_attr_e( 'Scale' ); ?>" />
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
		</div>
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    61
	</div>
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
	</div>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
<?php if ( $can_restore ) { ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    66
	<div class="imgedit-group">
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
	<div class="imgedit-group-top">
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    68
		<h3><a onclick="imageEdit.toggleHelp(this);return false;" href="#"><?php _e('Restore Original Image'); ?> <span class="dashicons dashicons-arrow-down imgedit-help-toggle"></span></a></h3>
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
		<div class="imgedit-help">
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
		<p><?php _e('Discard any changes and restore the original image.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
		if ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
			echo ' '.__('Previously edited copies of the image will not be deleted.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
		?></p>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
		<div class="imgedit-submit">
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    77
		<input type="button" onclick="imageEdit.action(<?php echo "$post_id, '$nonce'"; ?>, 'restore')" class="button button-primary" value="<?php esc_attr_e( 'Restore image' ); ?>" <?php echo $can_restore; ?> />
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
		</div>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
		</div>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
	</div>
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    81
	</div>
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
<?php } ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
	<div class="imgedit-group">
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
	<div class="imgedit-group-top">
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    87
		<h3><?php _e('Image Crop'); ?> <a href="#" class="dashicons dashicons-editor-help imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);return false;"></a></h3>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    88
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
		<div class="imgedit-help">
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    90
		<p><?php _e('To crop the image, click on it and drag to make your selection.'); ?></p>
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
		<p><strong><?php _e('Crop Aspect Ratio'); ?></strong><br />
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    93
		<?php _e('The aspect ratio is the relationship between the width and height. You can preserve the aspect ratio by holding down the shift key while resizing your selection. Use the input box to specify the aspect ratio, e.g. 1:1 (square), 4:3, 16:9, etc.'); ?></p>
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
		<p><strong><?php _e('Crop Selection'); ?></strong><br />
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    96
		<?php _e('Once you have made your selection, you can adjust it by entering the size in pixels. The minimum selection size is the thumbnail size as set in the Media settings.'); ?></p>
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
		</div>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
	</div>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
	<p>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
		<?php _e('Aspect ratio:'); ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
		<span  class="nowrap">
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
		<input type="text" id="imgedit-crop-width-<?php echo $post_id; ?>" onkeyup="imageEdit.setRatioSelection(<?php echo $post_id; ?>, 0, this)" style="width:3em;" />
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
		:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
		<input type="text" id="imgedit-crop-height-<?php echo $post_id; ?>" onkeyup="imageEdit.setRatioSelection(<?php echo $post_id; ?>, 1, this)" style="width:3em;" />
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
		</span>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
	</p>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
	<p id="imgedit-crop-sel-<?php echo $post_id; ?>">
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
		<?php _e('Selection:'); ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
		<span  class="nowrap">
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
		<input type="text" id="imgedit-sel-width-<?php echo $post_id; ?>" onkeyup="imageEdit.setNumSelection(<?php echo $post_id; ?>)" style="width:4em;" />
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   113
		&times;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
		<input type="text" id="imgedit-sel-height-<?php echo $post_id; ?>" onkeyup="imageEdit.setNumSelection(<?php echo $post_id; ?>)" style="width:4em;" />
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
		</span>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
	</p>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
	</div>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
	<?php if ( $thumb && $sub_sizes ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
		$thumb_img = wp_constrain_dimensions( $thumb['width'], $thumb['height'], 160, 120 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
	?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
	<div class="imgedit-group imgedit-applyto">
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
	<div class="imgedit-group-top">
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   125
		<h3><?php _e('Thumbnail Settings'); ?> <a href="#" class="dashicons dashicons-editor-help imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);return false;"></a></h3>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   126
		<p class="imgedit-help"><?php _e('You can edit the image while preserving the thumbnail. For example, you may wish to have a square thumbnail that displays just a section of the image.'); ?></p>
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
	</div>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
	<p>
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   130
		<img src="<?php echo $thumb['url']; ?>" width="<?php echo $thumb_img[0]; ?>" height="<?php echo $thumb_img[1]; ?>" class="imgedit-size-preview" alt="" draggable="false" />
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   131
		<br /><?php _e('Current thumbnail'); ?>
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
	</p>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
	<p id="imgedit-save-target-<?php echo $post_id; ?>">
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
		<strong><?php _e('Apply changes to:'); ?></strong><br />
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
		<label class="imgedit-label">
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
		<input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="all" checked="checked" />
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
		<?php _e('All image sizes'); ?></label>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
		<label class="imgedit-label">
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
		<input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="thumbnail" />
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
		<?php _e('Thumbnail'); ?></label>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
		<label class="imgedit-label">
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
		<input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="nothumb" />
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
		<?php _e('All sizes except thumbnail'); ?></label>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
	</p>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
	</div>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
	<?php } ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   153
	</div>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   154
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   155
	<div class="imgedit-panel-content">
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   156
		<?php echo $note; ?>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   157
		<div class="imgedit-menu">
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   158
			<div onclick="imageEdit.crop(<?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-crop disabled" title="<?php esc_attr_e( 'Crop' ); ?>"></div><?php
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   159
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   160
		// On some setups GD library does not provide imagerotate() - Ticket #11536
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   161
		if ( wp_image_editor_supports( array( 'mime_type' => get_post_mime_type( $post_id ), 'methods' => array( 'rotate' ) ) ) ) { ?>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   162
			<div class="imgedit-rleft"  onclick="imageEdit.rotate( 90, <?php echo "$post_id, '$nonce'"; ?>, this)" title="<?php esc_attr_e( 'Rotate counter-clockwise' ); ?>"></div>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   163
			<div class="imgedit-rright" onclick="imageEdit.rotate(-90, <?php echo "$post_id, '$nonce'"; ?>, this)" title="<?php esc_attr_e( 'Rotate clockwise' ); ?>"></div>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   164
	<?php } else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   165
			$note_no_rotate = esc_attr__('Image rotation is not supported by your web host.');
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   166
	?>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   167
		    <div class="imgedit-rleft disabled"  title="<?php echo $note_no_rotate; ?>"></div>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   168
		    <div class="imgedit-rright disabled" title="<?php echo $note_no_rotate; ?>"></div>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   169
	<?php } ?>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   170
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   171
			<div onclick="imageEdit.flip(1, <?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-flipv" title="<?php esc_attr_e( 'Flip vertically' ); ?>"></div>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   172
			<div onclick="imageEdit.flip(2, <?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-fliph" title="<?php esc_attr_e( 'Flip horizontally' ); ?>"></div>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   173
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   174
			<div id="image-undo-<?php echo $post_id; ?>" onclick="imageEdit.undo(<?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-undo disabled" title="<?php esc_attr_e( 'Undo' ); ?>"></div>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   175
			<div id="image-redo-<?php echo $post_id; ?>" onclick="imageEdit.redo(<?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-redo disabled" title="<?php esc_attr_e( 'Redo' ); ?>"></div>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   176
			<br class="clear" />
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   177
		</div>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   178
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   179
		<input type="hidden" id="imgedit-sizer-<?php echo $post_id; ?>" value="<?php echo $sizer; ?>" />
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   180
		<input type="hidden" id="imgedit-history-<?php echo $post_id; ?>" value="" />
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   181
		<input type="hidden" id="imgedit-undone-<?php echo $post_id; ?>" value="0" />
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   182
		<input type="hidden" id="imgedit-selection-<?php echo $post_id; ?>" value="" />
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   183
		<input type="hidden" id="imgedit-x-<?php echo $post_id; ?>" value="<?php echo isset( $meta['width'] ) ? $meta['width'] : 0; ?>" />
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   184
		<input type="hidden" id="imgedit-y-<?php echo $post_id; ?>" value="<?php echo isset( $meta['height'] ) ? $meta['height'] : 0; ?>" />
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   185
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   186
		<div id="imgedit-crop-<?php echo $post_id; ?>" class="imgedit-crop-wrap">
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   187
		<img id="image-preview-<?php echo $post_id; ?>" onload="imageEdit.imgLoaded('<?php echo $post_id; ?>')" src="<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>?action=imgedit-preview&amp;_ajax_nonce=<?php echo $nonce; ?>&amp;postid=<?php echo $post_id; ?>&amp;rand=<?php echo rand(1, 99999); ?>" />
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   188
		</div>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   189
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   190
		<div class="imgedit-submit">
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   191
			<input type="button" onclick="imageEdit.close(<?php echo $post_id; ?>, 1)" class="button" value="<?php esc_attr_e( 'Cancel' ); ?>" />
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   192
			<input type="button" onclick="imageEdit.save(<?php echo "$post_id, '$nonce'"; ?>)" disabled="disabled" class="button button-primary imgedit-submit-btn" value="<?php esc_attr_e( 'Save' ); ?>" />
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   193
		</div>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   194
	</div>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   195
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   196
	</div>
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
	<div class="imgedit-wait" id="imgedit-wait-<?php echo $post_id; ?>"></div>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
	<script type="text/javascript">jQuery( function() { imageEdit.init(<?php echo $post_id; ?>); });</script>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
	<div class="hidden" id="imgedit-leaving-<?php echo $post_id; ?>"><?php _e("There are unsaved changes that will be lost. 'OK' to continue, 'Cancel' to return to the Image Editor."); ?></div>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
	</div>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
 * Streams image in WP_Image_Editor to browser.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
 * Provided for backcompat reasons
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
 * @param WP_Image_Editor $image
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
 * @param string $mime_type
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
 * @param int $post_id
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
 * @return boolean
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
function wp_stream_image( $image, $mime_type, $post_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
	if ( $image instanceof WP_Image_Editor ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   215
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   216
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   217
		 * Filter the WP_Image_Editor instance for the image to be streamed to the browser.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   218
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   219
		 * @since 3.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   220
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   221
		 * @param WP_Image_Editor $image   WP_Image_Editor instance.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   222
		 * @param int             $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   223
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   224
		$image = apply_filters( 'image_editor_save_pre', $image, $post_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
		if ( is_wp_error( $image->stream( $mime_type ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
		_deprecated_argument( __FUNCTION__, '3.5', __( '$image needs to be an WP_Image_Editor object' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   233
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   234
		 * Filter the GD image resource to be streamed to the browser.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   235
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   236
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   237
		 * @deprecated 3.5.0 Use image_editor_save_pre instead.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   238
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   239
		 * @param resource $image   Image resource to be streamed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   240
		 * @param int      $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   241
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   242
		$image = apply_filters( 'image_save_pre', $image, $post_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
		switch ( $mime_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
			case 'image/jpeg':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
				header( 'Content-Type: image/jpeg' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
				return imagejpeg( $image, null, 90 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
			case 'image/png':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
				header( 'Content-Type: image/png' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
				return imagepng( $image );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
			case 'image/gif':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
				header( 'Content-Type: image/gif' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
				return imagegif( $image );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
			default:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
 * Saves Image to File
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
 * @param string $filename
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
 * @param WP_Image_Editor $image
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
 * @param string $mime_type
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
 * @param int $post_id
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
 * @return boolean
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
function wp_save_image_file( $filename, $image, $mime_type, $post_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
	if ( $image instanceof WP_Image_Editor ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   271
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   272
		/** This filter is documented in wp-admin/includes/image-edit.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   273
		$image = apply_filters( 'image_editor_save_pre', $image, $post_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   274
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   275
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   276
		 * Filter whether to skip saving the image file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   277
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   278
		 * Returning a non-null value will short-circuit the save method,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   279
		 * returning that value instead.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   280
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   281
		 * @since 3.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   282
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   283
		 * @param mixed           $override  Value to return instead of saving. Default null.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   284
		 * @param string          $filename  Name of the file to be saved.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   285
		 * @param WP_Image_Editor $image     WP_Image_Editor instance.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   286
		 * @param string          $mime_type Image mime type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   287
		 * @param int             $post_id   Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   288
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   289
		$saved = apply_filters( 'wp_save_image_editor_file', null, $filename, $image, $mime_type, $post_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
		if ( null !== $saved )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
			return $saved;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
		return $image->save( $filename, $mime_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
		_deprecated_argument( __FUNCTION__, '3.5', __( '$image needs to be an WP_Image_Editor object' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   298
		/** This filter is documented in wp-admin/includes/image-edit.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   299
		$image = apply_filters( 'image_save_pre', $image, $post_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   300
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   301
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   302
		 * Filter whether to skip saving the image file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   303
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   304
		 * Returning a non-null value will short-circuit the save method,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   305
		 * returning that value instead.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   306
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   307
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   308
		 * @deprecated 3.5.0 Use wp_save_image_editor_file instead.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   309
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   310
		 * @param mixed           $override  Value to return instead of saving. Default null.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   311
		 * @param string          $filename  Name of the file to be saved.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   312
		 * @param WP_Image_Editor $image     WP_Image_Editor instance.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   313
		 * @param string          $mime_type Image mime type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   314
		 * @param int             $post_id   Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   315
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   316
		$saved = apply_filters( 'wp_save_image_file', null, $filename, $image, $mime_type, $post_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
		if ( null !== $saved )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
			return $saved;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
		switch ( $mime_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
			case 'image/jpeg':
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   323
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   324
				/** This filter is documented in wp-includes/class-wp-image-editor.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
				return imagejpeg( $image, $filename, apply_filters( 'jpeg_quality', 90, 'edit_image' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
			case 'image/png':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
				return imagepng( $image, $filename );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
			case 'image/gif':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
				return imagegif( $image, $filename );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
			default:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   336
/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   337
 * Image preview ratio. Internal use only.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   338
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   339
 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   340
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   341
 * @ignore
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   342
 * @param int $w Image width in pixels.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   343
 * @param int $h Image height in pixels.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   344
 * @return float|int Image preview ratio.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   345
 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
function _image_get_preview_ratio($w, $h) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
	$max = max($w, $h);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
	return $max > 400 ? (400 / $max) : 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   351
/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   352
 * Returns an image resource. Internal use only.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   353
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   354
 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   355
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   356
 * @ignore
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   357
 * @param resource  $img   Image resource.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   358
 * @param float|int $angle Image rotation angle, in degrees.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   359
 * @return resource|false GD image resource, false otherwise.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   360
 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
function _rotate_image_resource($img, $angle) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
	_deprecated_function( __FUNCTION__, '3.5', __( 'Use WP_Image_Editor::rotate' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
	if ( function_exists('imagerotate') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
		$rotated = imagerotate($img, $angle, 0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
		if ( is_resource($rotated) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
			imagedestroy($img);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
			$img = $rotated;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
	return $img;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   374
 * Flips an image resource. Internal use only.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   375
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   376
 * @since 2.9.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   378
 * @ignore
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   379
 * @param resource $img  Image resource.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   380
 * @param bool     $horz Whether to flip horizontally.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   381
 * @param bool     $vert Whether to flip vertically.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   382
 * @return resource (maybe) flipped image resource.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
function _flip_image_resource($img, $horz, $vert) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
	_deprecated_function( __FUNCTION__, '3.5', __( 'Use WP_Image_Editor::flip' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
	$w = imagesx($img);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
	$h = imagesy($img);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
	$dst = wp_imagecreatetruecolor($w, $h);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
	if ( is_resource($dst) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
		$sx = $vert ? ($w - 1) : 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
		$sy = $horz ? ($h - 1) : 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
		$sw = $vert ? -$w : $w;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
		$sh = $horz ? -$h : $h;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
		if ( imagecopyresampled($dst, $img, 0, 0, $sx, $sy, $w, $h, $sw, $sh) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
			imagedestroy($img);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
			$img = $dst;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
	return $img;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   404
 * Crops an image resource. Internal use only.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   405
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   406
 * @since 2.9.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   408
 * @ignore
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   409
 * @param resource $img Image resource.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   410
 * @param float    $x   Source point x-coordinate.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   411
 * @param float    $y   Source point y-cooredinate.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   412
 * @param float    $w   Source width.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   413
 * @param float    $h   Source height.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   414
 * @return resource (maybe) cropped image resource.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
function _crop_image_resource($img, $x, $y, $w, $h) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
	$dst = wp_imagecreatetruecolor($w, $h);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
	if ( is_resource($dst) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
		if ( imagecopy($dst, $img, 0, 0, $x, $y, $w, $h) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
			imagedestroy($img);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
			$img = $dst;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
	return $img;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
 * Performs group of changes on Editor specified.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   430
 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   431
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   432
 * @param WP_Image_Editor $image   {@see WP_Image_Editor} instance.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   433
 * @param array           $changes Array of change operations.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   434
 * @return WP_Image_Editor {@see WP_Image_Editor} instance with changes applied.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
function image_edit_apply_changes( $image, $changes ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
	if ( is_resource( $image ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
		_deprecated_argument( __FUNCTION__, '3.5', __( '$image needs to be an WP_Image_Editor object' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
	if ( !is_array($changes) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
		return $image;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   443
	// Expand change operations.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
	foreach ( $changes as $key => $obj ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
		if ( isset($obj->r) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
			$obj->type = 'rotate';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
			$obj->angle = $obj->r;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
			unset($obj->r);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
		} elseif ( isset($obj->f) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
			$obj->type = 'flip';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
			$obj->axis = $obj->f;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
			unset($obj->f);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
		} elseif ( isset($obj->c) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
			$obj->type = 'crop';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
			$obj->sel = $obj->c;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
			unset($obj->c);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
		$changes[$key] = $obj;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   461
	// Combine operations.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
	if ( count($changes) > 1 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
		$filtered = array($changes[0]);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   464
		for ( $i = 0, $j = 1, $c = count( $changes ); $j < $c; $j++ ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
			$combined = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
			if ( $filtered[$i]->type == $changes[$j]->type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
				switch ( $filtered[$i]->type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
					case 'rotate':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
						$filtered[$i]->angle += $changes[$j]->angle;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
						$combined = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
						break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
					case 'flip':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
						$filtered[$i]->axis ^= $changes[$j]->axis;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
						$combined = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
						break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
			if ( !$combined )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
				$filtered[++$i] = $changes[$j];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
		$changes = $filtered;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
		unset($filtered);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   485
	// Image resource before applying the changes.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   486
	if ( $image instanceof WP_Image_Editor ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   487
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   488
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   489
		 * Filter the WP_Image_Editor instance before applying changes to the image.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   490
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   491
		 * @since 3.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   492
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   493
		 * @param WP_Image_Editor $image   WP_Image_Editor instance.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   494
 		 * @param array           $changes Array of change operations.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   495
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   496
		$image = apply_filters( 'wp_image_editor_before_change', $image, $changes );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   497
	} elseif ( is_resource( $image ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   498
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   499
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   500
		 * Filter the GD image resource before applying changes to the image.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   501
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   502
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   503
		 * @deprecated 3.5.0 Use wp_image_editor_before_change instead.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   504
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   505
		 * @param resource $image   GD image resource.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   506
 		 * @param array    $changes Array of change operations.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   507
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   508
		$image = apply_filters( 'image_edit_before_change', $image, $changes );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   509
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
	foreach ( $changes as $operation ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
		switch ( $operation->type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
			case 'rotate':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
				if ( $operation->angle != 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
					if ( $image instanceof WP_Image_Editor )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
						$image->rotate( $operation->angle );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
					else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
						$image = _rotate_image_resource( $image, $operation->angle );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
			case 'flip':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
				if ( $operation->axis != 0 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
					if ( $image instanceof WP_Image_Editor )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
						$image->flip( ($operation->axis & 1) != 0, ($operation->axis & 2) != 0 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
					else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
						$image = _flip_image_resource( $image, ( $operation->axis & 1 ) != 0, ( $operation->axis & 2 ) != 0 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
			case 'crop':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
				$sel = $operation->sel;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
				if ( $image instanceof WP_Image_Editor ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
					$size = $image->get_size();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
					$w = $size['width'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
					$h = $size['height'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
					$scale = 1 / _image_get_preview_ratio( $w, $h ); // discard preview scaling
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
					$image->crop( $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
				} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
					$scale = 1 / _image_get_preview_ratio( imagesx( $image ), imagesy( $image ) ); // discard preview scaling
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
					$image = _crop_image_resource( $image, $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   545
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
	return $image;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   549
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
 * Streams image in post to browser, along with enqueued changes
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
 * in $_REQUEST['history']
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
 * @param int $post_id
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
 * @return boolean
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
function stream_preview_image( $post_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
	$post = get_post( $post_id );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   559
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   560
	/** This filter is documented in wp-admin/admin.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
	@ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   563
	$img = wp_get_image_editor( _load_image_to_edit_path( $post_id ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
    if ( is_wp_error( $img ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
        return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
	$changes = !empty($_REQUEST['history']) ? json_decode( wp_unslash($_REQUEST['history']) ) : null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
	if ( $changes )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
		$img = image_edit_apply_changes( $img, $changes );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   572
	// Scale the image.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
	$size = $img->get_size();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
	$w = $size['width'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
	$h = $size['height'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
	$ratio = _image_get_preview_ratio( $w, $h );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
	$w2 = max ( 1, $w * $ratio );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   579
	$h2 = max ( 1, $h * $ratio );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
	if ( is_wp_error( $img->resize( $w2, $h2 ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
	return wp_stream_image( $img, $post->post_mime_type, $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   585
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   586
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   587
/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   588
 * Restores the metadata for a given attachment.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   589
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   590
 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   591
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   592
 * @param int $post_id Attachment post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   593
 * @return stdClass Image restoration message object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   594
 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
function wp_restore_image($post_id) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
	$meta = wp_get_attachment_metadata($post_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
	$file = get_attached_file($post_id);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   598
	$backup_sizes = $old_backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
	$restored = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
	$msg = new stdClass;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
	if ( !is_array($backup_sizes) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
		$msg->error = __('Cannot load image metadata.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
		return $msg;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
	$parts = pathinfo($file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
	$suffix = time() . rand(100, 999);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
	$default_sizes = get_intermediate_image_sizes();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
	if ( isset($backup_sizes['full-orig']) && is_array($backup_sizes['full-orig']) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
		$data = $backup_sizes['full-orig'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
		if ( $parts['basename'] != $data['file'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
			if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   616
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   617
				// Delete only if it's edited image.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
				if ( preg_match('/-e[0-9]{13}\./', $parts['basename']) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   619
					wp_delete_file( $file );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
			} elseif ( isset( $meta['width'], $meta['height'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
				$backup_sizes["full-$suffix"] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $parts['basename']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
		$restored_file = path_join($parts['dirname'], $data['file']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   627
		$restored = update_attached_file($post_id, $restored_file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   628
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   629
		$meta['file'] = _wp_relative_upload_path( $restored_file );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   630
		$meta['width'] = $data['width'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
		$meta['height'] = $data['height'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
	foreach ( $default_sizes as $default_size ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   635
		if ( isset($backup_sizes["$default_size-orig"]) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
			$data = $backup_sizes["$default_size-orig"];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
			if ( isset($meta['sizes'][$default_size]) && $meta['sizes'][$default_size]['file'] != $data['file'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   638
				if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   639
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   640
					// Delete only if it's edited image
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   641
					if ( preg_match('/-e[0-9]{13}-/', $meta['sizes'][$default_size]['file']) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   642
						$delete_file = path_join( $parts['dirname'], $meta['sizes'][$default_size]['file'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   643
						wp_delete_file( $delete_file );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   645
				} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   646
					$backup_sizes["$default_size-{$suffix}"] = $meta['sizes'][$default_size];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   647
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   649
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
			$meta['sizes'][$default_size] = $data;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
			unset($meta['sizes'][$default_size]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   655
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   656
	if ( ! wp_update_attachment_metadata( $post_id, $meta ) ||
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   657
		( $old_backup_sizes !== $backup_sizes && ! update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes ) ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   658
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   659
		$msg->error = __('Cannot save image metadata.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   660
		return $msg;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   661
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   662
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
	if ( !$restored )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   664
		$msg->error = __('Image metadata is inconsistent.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   665
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   666
		$msg->msg = __('Image restored successfully.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   667
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   668
	return $msg;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   670
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   671
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   672
 * Saves image to post along with enqueued changes
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
 * in $_REQUEST['history']
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   674
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   675
 * @param int $post_id
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   676
 * @return \stdClass
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   677
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   678
function wp_save_image( $post_id ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   679
	global $_wp_additional_image_sizes;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   680
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   681
	$return = new stdClass;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   682
	$success = $delete = $scaled = $nocrop = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   683
	$post = get_post( $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   684
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   685
	$img = wp_get_image_editor( _load_image_to_edit_path( $post_id, 'full' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   686
	if ( is_wp_error( $img ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   687
		$return->error = esc_js( __('Unable to create new image.') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   688
		return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   689
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   690
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   691
	$fwidth = !empty($_REQUEST['fwidth']) ? intval($_REQUEST['fwidth']) : 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   692
	$fheight = !empty($_REQUEST['fheight']) ? intval($_REQUEST['fheight']) : 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   693
	$target = !empty($_REQUEST['target']) ? preg_replace('/[^a-z0-9_-]+/i', '', $_REQUEST['target']) : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   694
	$scale = !empty($_REQUEST['do']) && 'scale' == $_REQUEST['do'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   695
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   696
	if ( $scale && $fwidth > 0 && $fheight > 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   697
		$size = $img->get_size();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   698
		$sX = $size['width'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
		$sY = $size['height'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   700
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   701
		// Check if it has roughly the same w / h ratio.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   702
		$diff = round($sX / $sY, 2) - round($fwidth / $fheight, 2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   703
		if ( -0.1 < $diff && $diff < 0.1 ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   704
			// Scale the full size image.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   705
			if ( $img->resize( $fwidth, $fheight ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   706
				$scaled = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   707
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   708
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   709
		if ( !$scaled ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   710
			$return->error = esc_js( __('Error while saving the scaled image. Please reload the page and try again.') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   711
			return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   712
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   713
	} elseif ( !empty($_REQUEST['history']) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   714
		$changes = json_decode( wp_unslash($_REQUEST['history']) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   715
		if ( $changes )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   716
			$img = image_edit_apply_changes($img, $changes);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   717
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   718
		$return->error = esc_js( __('Nothing to save, the image has not changed.') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   719
		return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   720
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   721
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   722
	$meta = wp_get_attachment_metadata($post_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   723
	$backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   724
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   725
	if ( !is_array($meta) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   726
		$return->error = esc_js( __('Image data does not exist. Please re-upload the image.') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   727
		return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   728
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   729
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   730
	if ( !is_array($backup_sizes) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   731
		$backup_sizes = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   732
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   733
	// Generate new filename.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   734
	$path = get_attached_file($post_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   735
	$path_parts = pathinfo( $path );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   736
	$filename = $path_parts['filename'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   737
	$suffix = time() . rand(100, 999);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   738
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   739
	if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE &&
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   740
		isset($backup_sizes['full-orig']) && $backup_sizes['full-orig']['file'] != $path_parts['basename'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   741
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   742
		if ( 'thumbnail' == $target )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   743
			$new_path = "{$path_parts['dirname']}/{$filename}-temp.{$path_parts['extension']}";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   744
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   745
			$new_path = $path;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   746
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   747
		while( true ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   748
			$filename = preg_replace( '/-e([0-9]+)$/', '', $filename );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   749
			$filename .= "-e{$suffix}";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   750
			$new_filename = "{$filename}.{$path_parts['extension']}";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   751
			$new_path = "{$path_parts['dirname']}/$new_filename";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   752
			if ( file_exists($new_path) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   753
				$suffix++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   754
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   755
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   756
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   757
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   758
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   759
	// Save the full-size file, also needed to create sub-sizes.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   760
	if ( !wp_save_image_file($new_path, $img, $post->post_mime_type, $post_id) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   761
		$return->error = esc_js( __('Unable to save the image.') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   762
		return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   763
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   764
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   765
	if ( 'nothumb' == $target || 'all' == $target || 'full' == $target || $scaled ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   766
		$tag = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   767
		if ( isset($backup_sizes['full-orig']) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   768
			if ( ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE ) && $backup_sizes['full-orig']['file'] != $path_parts['basename'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   769
				$tag = "full-$suffix";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   770
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   771
			$tag = 'full-orig';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   772
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   773
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   774
		if ( $tag )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   775
			$backup_sizes[$tag] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $path_parts['basename']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   776
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   777
		$success = ( $path === $new_path ) || update_attached_file( $post_id, $new_path );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   778
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   779
		$meta['file'] = _wp_relative_upload_path( $new_path );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   780
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   781
		$size = $img->get_size();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   782
		$meta['width'] = $size['width'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   783
		$meta['height'] = $size['height'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   784
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   785
		if ( $success && ('nothumb' == $target || 'all' == $target) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   786
			$sizes = get_intermediate_image_sizes();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   787
			if ( 'nothumb' == $target )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   788
				$sizes = array_diff( $sizes, array('thumbnail') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   791
		$return->fw = $meta['width'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   792
		$return->fh = $meta['height'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   793
	} elseif ( 'thumbnail' == $target ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
		$sizes = array( 'thumbnail' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   795
		$success = $delete = $nocrop = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   796
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   798
	if ( isset( $sizes ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
		$_sizes = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   800
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
		foreach ( $sizes as $size ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   802
			$tag = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   803
			if ( isset( $meta['sizes'][$size] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   804
				if ( isset($backup_sizes["$size-orig"]) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
					if ( ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE ) && $backup_sizes["$size-orig"]['file'] != $meta['sizes'][$size]['file'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   806
						$tag = "$size-$suffix";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   807
				} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   808
					$tag = "$size-orig";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   810
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   811
				if ( $tag )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   812
					$backup_sizes[$tag] = $meta['sizes'][$size];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   813
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   814
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   815
			if ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   816
				$width  = intval( $_wp_additional_image_sizes[ $size ]['width'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   817
				$height = intval( $_wp_additional_image_sizes[ $size ]['height'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   818
				$crop   = ( $nocrop ) ? false : $_wp_additional_image_sizes[ $size ]['crop'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   819
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   820
				$height = get_option( "{$size}_size_h" );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   821
				$width  = get_option( "{$size}_size_w" );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   822
				$crop   = ( $nocrop ) ? false : get_option( "{$size}_crop" );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   823
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   824
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   825
			$_sizes[ $size ] = array( 'width' => $width, 'height' => $height, 'crop' => $crop );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   826
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   827
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   828
		$meta['sizes'] = array_merge( $meta['sizes'], $img->multi_resize( $_sizes ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   829
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   830
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   831
	unset( $img );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   832
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   833
	if ( $success ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   834
		wp_update_attachment_metadata( $post_id, $meta );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   835
		update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   836
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   837
		if ( $target == 'thumbnail' || $target == 'all' || $target == 'full' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   838
			// Check if it's an image edit from attachment edit screen
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   839
			if ( ! empty( $_REQUEST['context'] ) && 'edit-attachment' == $_REQUEST['context'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   840
				$thumb_url = wp_get_attachment_image_src( $post_id, array( 900, 600 ), true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   841
				$return->thumbnail = $thumb_url[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   842
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   843
				$file_url = wp_get_attachment_url($post_id);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   844
				if ( ! empty( $meta['sizes']['thumbnail'] ) && $thumb = $meta['sizes']['thumbnail'] ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   845
					$return->thumbnail = path_join( dirname($file_url), $thumb['file'] );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   846
				} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   847
					$return->thumbnail = "$file_url?w=128&h=128";
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   848
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   849
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   850
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   851
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   852
		$delete = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   853
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   854
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   855
	if ( $delete ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   856
		wp_delete_file( $new_path );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   857
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
	$return->msg = esc_js( __('Image saved') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   860
	return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   861
}