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