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