[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/includes/ -> image-edit.php (source)

   1  <?php
   2  /**
   3   * WordPress Image Editor
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /**
  10   * Loads the WP image-editing interface.
  11   *
  12   * @since 2.9.0
  13   *
  14   * @param int          $post_id Attachment post ID.
  15   * @param false|object $msg     Optional. Message to display for image editor updates or errors.
  16   *                              Default false.
  17   */
  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      = '';
  24  
  25      if ( isset( $meta['width'], $meta['height'] ) ) {
  26          $big = max( $meta['width'], $meta['height'] );
  27      } else {
  28          die( __( 'Image data does not exist. Please re-upload the image.' ) );
  29      }
  30  
  31      $sizer = $big > 600 ? 600 / $big : 1;
  32  
  33      $backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true );
  34      $can_restore  = false;
  35  
  36      if ( ! empty( $backup_sizes ) && isset( $backup_sizes['full-orig'], $meta['file'] ) ) {
  37          $can_restore = wp_basename( $meta['file'] ) !== $backup_sizes['full-orig']['file'];
  38      }
  39  
  40      if ( $msg ) {
  41          if ( isset( $msg->error ) ) {
  42              $note = "<div class='notice notice-error' role='alert'><p>$msg->error</p></div>";
  43          } elseif ( isset( $msg->msg ) ) {
  44              $note = "<div class='notice notice-success' role='alert'><p>$msg->msg</p></div>";
  45          }
  46      }
  47  
  48      /**
  49       * Shows the settings in the Image Editor that allow selecting to edit only the thumbnail of an image.
  50       *
  51       * @since 6.3.0
  52       *
  53       * @param bool $show Whether to show the settings in the Image Editor. Default false.
  54       */
  55      $edit_thumbnails_separately = (bool) apply_filters( 'image_edit_thumbnails_separately', false );
  56  
  57      ?>
  58      <div class="imgedit-wrap wp-clearfix">
  59      <div id="imgedit-panel-<?php echo $post_id; ?>">
  60      <?php echo $note; ?>
  61      <div class="imgedit-panel-content imgedit-panel-tools wp-clearfix">
  62          <div class="imgedit-menu wp-clearfix">
  63              <button type="button" onclick="imageEdit.toggleCropTool( <?php echo "$post_id, '$nonce'"; ?>, this );" aria-expanded="false" aria-controls="imgedit-crop" class="imgedit-crop button disabled" disabled><?php esc_html_e( 'Crop' ); ?></button>
  64              <button type="button" class="imgedit-scale button" onclick="imageEdit.toggleControls(this);" aria-expanded="false" aria-controls="imgedit-scale"><?php echo esc_html_x( 'Scale', 'verb' ); ?></button>
  65              <div class="imgedit-rotate-menu-container">
  66                  <button type="button" aria-controls="imgedit-rotate-menu" class="imgedit-rotate button" aria-expanded="false" onclick="imageEdit.togglePopup(this)" onblur="imageEdit.monitorPopup()"><?php esc_html_e( 'Image Rotation' ); ?></button>
  67                  <div id="imgedit-rotate-menu" class="imgedit-popup-menu">
  68              <?php
  69              // On some setups GD library does not provide imagerotate() - Ticket #11536.
  70              if ( wp_image_editor_supports(
  71                  array(
  72                      'mime_type' => get_post_mime_type( $post_id ),
  73                      'methods'   => array( 'rotate' ),
  74                  )
  75              ) ) {
  76                  $note_no_rotate = '';
  77                  ?>
  78                      <button type="button" class="imgedit-rleft button" onkeydown="imageEdit.browsePopup(event, this)" onclick="imageEdit.rotate( 90, <?php echo "$post_id, '$nonce'"; ?>, this)" onblur="imageEdit.monitorPopup()"><?php esc_html_e( 'Rotate 90&deg; left' ); ?></button>
  79                      <button type="button" class="imgedit-rright button" onkeydown="imageEdit.browsePopup(event, this)" onclick="imageEdit.rotate(-90, <?php echo "$post_id, '$nonce'"; ?>, this)" onblur="imageEdit.monitorPopup()"><?php esc_html_e( 'Rotate 90&deg; right' ); ?></button>
  80                      <button type="button" class="imgedit-rfull button" onkeydown="imageEdit.browsePopup(event, this)" onclick="imageEdit.rotate(180, <?php echo "$post_id, '$nonce'"; ?>, this)" onblur="imageEdit.monitorPopup()"><?php esc_html_e( 'Rotate 180&deg;' ); ?></button>
  81                  <?php
  82              } else {
  83                  $note_no_rotate = '<p class="note-no-rotate"><em>' . __( 'Image rotation is not supported by your web host.' ) . '</em></p>';
  84                  ?>
  85                      <button type="button" class="imgedit-rleft button disabled" disabled></button>
  86                      <button type="button" class="imgedit-rright button disabled" disabled></button>
  87                  <?php
  88              }
  89              ?>
  90                      <hr />
  91                      <button type="button" onkeydown="imageEdit.browsePopup(event, this)" onclick="imageEdit.flip(1, <?php echo "$post_id, '$nonce'"; ?>, this)" onblur="imageEdit.monitorPopup()" class="imgedit-flipv button"><?php esc_html_e( 'Flip vertical' ); ?></button>
  92                      <button type="button" onkeydown="imageEdit.browsePopup(event, this)" onclick="imageEdit.flip(2, <?php echo "$post_id, '$nonce'"; ?>, this)" onblur="imageEdit.monitorPopup()" class="imgedit-fliph button"><?php esc_html_e( 'Flip horizontal' ); ?></button>
  93                      <?php echo $note_no_rotate; ?>
  94                  </div>
  95              </div>
  96          </div>
  97          <div class="imgedit-submit imgedit-menu">
  98              <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><?php esc_html_e( 'Undo' ); ?></button>
  99              <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><?php esc_html_e( 'Redo' ); ?></button>
 100              <button type="button" onclick="imageEdit.close(<?php echo $post_id; ?>, 1)" class="button imgedit-cancel-btn"><?php esc_html_e( 'Cancel Editing' ); ?></button>
 101              <button type="button" onclick="imageEdit.save(<?php echo "$post_id, '$nonce'"; ?>)" disabled="disabled" class="button button-primary imgedit-submit-btn"><?php esc_html_e( 'Save Edits' ); ?></button>
 102          </div>
 103      </div>
 104  
 105      <div class="imgedit-panel-content wp-clearfix">
 106          <div class="imgedit-tools">
 107              <input type="hidden" id="imgedit-nonce-<?php echo $post_id; ?>" value="<?php echo $nonce; ?>" />
 108              <input type="hidden" id="imgedit-sizer-<?php echo $post_id; ?>" value="<?php echo $sizer; ?>" />
 109              <input type="hidden" id="imgedit-history-<?php echo $post_id; ?>" value="" />
 110              <input type="hidden" id="imgedit-undone-<?php echo $post_id; ?>" value="0" />
 111              <input type="hidden" id="imgedit-selection-<?php echo $post_id; ?>" value="" />
 112              <input type="hidden" id="imgedit-x-<?php echo $post_id; ?>" value="<?php echo $meta['width'] ?? 0; ?>" />
 113              <input type="hidden" id="imgedit-y-<?php echo $post_id; ?>" value="<?php echo $meta['height'] ?? 0; ?>" />
 114  
 115              <div id="imgedit-crop-<?php echo $post_id; ?>" class="imgedit-crop-wrap">
 116              <div class="imgedit-crop-grid"></div>
 117              <img id="image-preview-<?php echo $post_id; ?>" onload="imageEdit.imgLoaded('<?php echo $post_id; ?>')"
 118                  src="<?php echo esc_url( admin_url( 'admin-ajax.php', 'relative' ) ) . '?action=imgedit-preview&amp;_ajax_nonce=' . $nonce . '&amp;postid=' . $post_id . '&amp;rand=' . rand( 1, 99999 ); ?>" alt="" />
 119              </div>
 120          </div>
 121          <div class="imgedit-settings">
 122              <div class="imgedit-tool-active">
 123                  <div class="imgedit-group">
 124                  <div id="imgedit-scale" tabindex="-1" class="imgedit-group-controls">
 125                      <div class="imgedit-group-top">
 126                          <h2><?php _e( 'Scale Image' ); ?></h2>
 127                          <button type="button" class="dashicons dashicons-editor-help imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);" aria-expanded="false"><span class="screen-reader-text">
 128                          <?php
 129                          /* translators: Hidden accessibility text. */
 130                          esc_html_e( 'Scale Image Help' );
 131                          ?>
 132                          </span></button>
 133                          <div class="imgedit-help">
 134                          <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>
 135                          </div>
 136                          <?php if ( isset( $meta['width'], $meta['height'] ) ) : ?>
 137                          <p>
 138                              <?php
 139                              printf(
 140                                  /* translators: %s: Image width and height in pixels. */
 141                                  __( 'Original dimensions %s' ),
 142                                  '<span class="imgedit-original-dimensions">' . $meta['width'] . ' &times; ' . $meta['height'] . '</span>'
 143                              );
 144                              ?>
 145                          </p>
 146                          <?php endif; ?>
 147                          <div class="imgedit-submit">
 148                          <fieldset class="imgedit-scale-controls">
 149                              <legend><?php _e( 'New dimensions:' ); ?></legend>
 150                              <div class="nowrap">
 151                              <label for="imgedit-scale-width-<?php echo $post_id; ?>" class="screen-reader-text">
 152                              <?php
 153                              /* translators: Hidden accessibility text. */
 154                              _e( 'Width' );
 155                              ?>
 156                              </label>
 157                              <input type="number" step="1" min="0" max="<?php echo $meta['width'] ?? ''; ?>" aria-describedby="imgedit-scale-warn-<?php echo $post_id; ?>"  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 $meta['width'] ?? 0; ?>" />
 158                              <span class="imgedit-separator" aria-hidden="true">&times;</span>
 159                              <label for="imgedit-scale-height-<?php echo $post_id; ?>" class="screen-reader-text">
 160                              <?php
 161                              /* translators: Hidden accessibility text. */
 162                              _e( 'Height' );
 163                              ?>
 164                              </label>
 165                              <input type="number" step="1" min="0" max="<?php echo $meta['height'] ?? ''; ?>" aria-describedby="imgedit-scale-warn-<?php echo $post_id; ?>" 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 $meta['height'] ?? 0; ?>" />
 166                              <button id="imgedit-scale-button" type="button" onclick="imageEdit.action(<?php echo "$post_id, '$nonce'"; ?>, 'scale')" class="button button-primary"><?php echo esc_html_x( 'Scale', 'verb' ); ?></button>
 167                              </div>
 168                              <span class="imgedit-scale-warn" id="imgedit-scale-warn-<?php echo $post_id; ?>"><span class="dashicons dashicons-warning" aria-hidden="true"></span><?php esc_html_e( 'Images cannot be scaled to a size larger than the original.' ); ?></span>
 169                          </fieldset>
 170                          </div>
 171                      </div>
 172                  </div>
 173              </div>
 174  
 175          <?php if ( $can_restore ) { ?>
 176                  <div class="imgedit-group">
 177                  <div class="imgedit-group-top">
 178                      <h2><button type="button" onclick="imageEdit.toggleHelp(this);" class="button-link" aria-expanded="false"><?php _e( 'Restore original image' ); ?> <span class="dashicons dashicons-arrow-down imgedit-help-toggle"></span></button></h2>
 179                      <div class="imgedit-help imgedit-restore">
 180                      <p>
 181                      <?php
 182                      _e( 'Discard any changes and restore the original image.' );
 183                      if ( ! defined( 'IMAGE_EDIT_OVERWRITE' ) || ! IMAGE_EDIT_OVERWRITE ) {
 184                          echo ' ' . __( 'Previously edited copies of the image will not be deleted.' );
 185                      }
 186                      ?>
 187                      </p>
 188                      <div class="imgedit-submit">
 189                          <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; ?> />
 190                      </div>
 191                  </div>
 192              </div>
 193              </div>
 194          <?php } ?>
 195              <div class="imgedit-group">
 196                  <div id="imgedit-crop" tabindex="-1" class="imgedit-group-controls">
 197                  <div class="imgedit-group-top">
 198                      <h2><?php _e( 'Crop Image' ); ?></h2>
 199                      <button type="button" class="dashicons dashicons-editor-help imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);" aria-expanded="false"><span class="screen-reader-text">
 200                      <?php
 201                      /* translators: Hidden accessibility text. */
 202                      _e( 'Image Crop Help' );
 203                      ?>
 204                      </span></button>
 205                      <div class="imgedit-help">
 206                          <p><?php _e( 'To crop the image, click on it and drag to make your selection.' ); ?></p>
 207                          <p><strong><?php _e( 'Crop Aspect Ratio' ); ?></strong><br />
 208                          <?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>
 209  
 210                          <p><strong><?php _e( 'Crop Selection' ); ?></strong><br />
 211                          <?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>
 212                      </div>
 213                  </div>
 214                  <fieldset class="imgedit-crop-ratio">
 215                      <legend><?php _e( 'Aspect ratio:' ); ?></legend>
 216                      <div class="nowrap">
 217                      <label for="imgedit-crop-width-<?php echo $post_id; ?>" class="screen-reader-text">
 218                      <?php
 219                      /* translators: Hidden accessibility text. */
 220                      _e( 'crop ratio width' );
 221                      ?>
 222                      </label>
 223                      <input type="number" step="1" min="1" 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)" />
 224                      <span class="imgedit-separator" aria-hidden="true">:</span>
 225                      <label for="imgedit-crop-height-<?php echo $post_id; ?>" class="screen-reader-text">
 226                      <?php
 227                      /* translators: Hidden accessibility text. */
 228                      _e( 'crop ratio height' );
 229                      ?>
 230                      </label>
 231                      <input  type="number" step="1" min="0" 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)" />
 232                      </div>
 233                  </fieldset>
 234                  <fieldset id="imgedit-crop-sel-<?php echo $post_id; ?>" class="imgedit-crop-sel">
 235                      <legend><?php _e( 'Selection:' ); ?></legend>
 236                      <div class="nowrap">
 237                      <label for="imgedit-sel-width-<?php echo $post_id; ?>" class="screen-reader-text">
 238                      <?php
 239                      /* translators: Hidden accessibility text. */
 240                      _e( 'selection width' );
 241                      ?>
 242                      </label>
 243                      <input  type="number" step="1" min="0" id="imgedit-sel-width-<?php echo $post_id; ?>" onkeyup="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" onblur="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" />
 244                      <span class="imgedit-separator" aria-hidden="true">&times;</span>
 245                      <label for="imgedit-sel-height-<?php echo $post_id; ?>" class="screen-reader-text">
 246                      <?php
 247                      /* translators: Hidden accessibility text. */
 248                      _e( 'selection height' );
 249                      ?>
 250                      </label>
 251                      <input  type="number" step="1" min="0" id="imgedit-sel-height-<?php echo $post_id; ?>" onkeyup="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" onblur="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" />
 252                      </div>
 253                  </fieldset>
 254                  <fieldset id="imgedit-crop-sel-<?php echo $post_id; ?>" class="imgedit-crop-sel">
 255                      <legend><?php _e( 'Starting Coordinates:' ); ?></legend>
 256                      <div class="nowrap">
 257                      <label for="imgedit-start-x-<?php echo $post_id; ?>" class="screen-reader-text">
 258                      <?php
 259                      /* translators: Hidden accessibility text. */
 260                      _e( 'horizontal start position' );
 261                      ?>
 262                      </label>
 263                      <input  type="number" step="1" min="0" id="imgedit-start-x-<?php echo $post_id; ?>" onkeyup="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" onblur="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" value="0" />
 264                      <span class="imgedit-separator" aria-hidden="true">&times;</span>
 265                      <label for="imgedit-start-y-<?php echo $post_id; ?>" class="screen-reader-text">
 266                      <?php
 267                      /* translators: Hidden accessibility text. */
 268                      _e( 'vertical start position' );
 269                      ?>
 270                      </label>
 271                      <input  type="number" step="1" min="0" id="imgedit-start-y-<?php echo $post_id; ?>" onkeyup="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" onblur="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" value="0" />
 272                      </div>
 273                  </fieldset>
 274                  <div class="imgedit-crop-apply imgedit-menu container">
 275                      <button class="button button-primary" type="button" onclick="imageEdit.handleCropToolClick( <?php echo "$post_id, '$nonce'"; ?>, this );" class="imgedit-crop-apply button"><?php esc_html_e( 'Apply Crop' ); ?></button> <button type="button" onclick="imageEdit.handleCropToolClick( <?php echo "$post_id, '$nonce'"; ?>, this );" class="imgedit-crop-clear button" disabled="disabled"><?php esc_html_e( 'Clear Crop' ); ?></button>
 276                  </div>
 277              </div>
 278          </div>
 279      </div>
 280  
 281      <?php
 282      if ( $edit_thumbnails_separately && $thumb && $sub_sizes ) {
 283          $thumb_img = wp_constrain_dimensions( $thumb['width'], $thumb['height'], 160, 120 );
 284          ?>
 285  
 286      <div class="imgedit-group imgedit-applyto">
 287          <div class="imgedit-group-top">
 288              <h2><?php _e( 'Thumbnail Settings' ); ?></h2>
 289              <button type="button" class="dashicons dashicons-editor-help imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);" aria-expanded="false"><span class="screen-reader-text">
 290              <?php
 291              /* translators: Hidden accessibility text. */
 292              esc_html_e( 'Thumbnail Settings Help' );
 293              ?>
 294              </span></button>
 295              <div class="imgedit-help">
 296              <p><?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>
 297              </div>
 298          </div>
 299          <div class="imgedit-thumbnail-preview-group">
 300              <figure class="imgedit-thumbnail-preview">
 301                  <img src="<?php echo esc_url( $thumb['url'] ); ?>" width="<?php echo esc_attr( $thumb_img[0] ); ?>" height="<?php echo esc_attr( $thumb_img[1] ); ?>" class="imgedit-size-preview" alt="" draggable="false" />
 302                  <figcaption class="imgedit-thumbnail-preview-caption"><?php _e( 'Current thumbnail' ); ?></figcaption>
 303              </figure>
 304              <div id="imgedit-save-target-<?php echo $post_id; ?>" class="imgedit-save-target">
 305              <fieldset>
 306                  <legend><?php _e( 'Apply changes to:' ); ?></legend>
 307  
 308                  <span class="imgedit-label">
 309                      <input type="radio" id="imgedit-target-all" name="imgedit-target-<?php echo $post_id; ?>" value="all" checked="checked" />
 310                      <label for="imgedit-target-all"><?php _e( 'All image sizes' ); ?></label>
 311                  </span>
 312  
 313                  <span class="imgedit-label">
 314                      <input type="radio" id="imgedit-target-thumbnail" name="imgedit-target-<?php echo $post_id; ?>" value="thumbnail" />
 315                      <label for="imgedit-target-thumbnail"><?php _e( 'Thumbnail' ); ?></label>
 316                  </span>
 317  
 318                  <span class="imgedit-label">
 319                      <input type="radio" id="imgedit-target-nothumb" name="imgedit-target-<?php echo $post_id; ?>" value="nothumb" />
 320                      <label for="imgedit-target-nothumb"><?php _e( 'All sizes except thumbnail' ); ?></label>
 321                  </span>
 322  
 323                  </fieldset>
 324              </div>
 325          </div>
 326      </div>
 327      <?php } ?>
 328          </div>
 329      </div>
 330  
 331      </div>
 332  
 333      <div class="imgedit-wait" id="imgedit-wait-<?php echo $post_id; ?>"></div>
 334      <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>
 335      </div>
 336      <?php
 337  }
 338  
 339  /**
 340   * Streams image in WP_Image_Editor to browser.
 341   *
 342   * @since 2.9.0
 343   *
 344   * @param WP_Image_Editor $image         The image editor instance.
 345   * @param string          $mime_type     The mime type of the image.
 346   * @param int             $attachment_id The image's attachment post ID.
 347   * @return bool True on success, false on failure.
 348   */
 349  function wp_stream_image( $image, $mime_type, $attachment_id ) {
 350      if ( $image instanceof WP_Image_Editor ) {
 351  
 352          /**
 353           * Filters the WP_Image_Editor instance for the image to be streamed to the browser.
 354           *
 355           * @since 3.5.0
 356           *
 357           * @param WP_Image_Editor $image         The image editor instance.
 358           * @param int             $attachment_id The attachment post ID.
 359           */
 360          $image = apply_filters( 'image_editor_save_pre', $image, $attachment_id );
 361  
 362          if ( is_wp_error( $image->stream( $mime_type ) ) ) {
 363              return false;
 364          }
 365  
 366          return true;
 367      } else {
 368          /* translators: 1: $image, 2: WP_Image_Editor */
 369          _deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( '%1$s needs to be a %2$s object.' ), '$image', 'WP_Image_Editor' ) );
 370  
 371          /**
 372           * Filters the GD image resource to be streamed to the browser.
 373           *
 374           * @since 2.9.0
 375           * @deprecated 3.5.0 Use {@see 'image_editor_save_pre'} instead.
 376           *
 377           * @param resource|GdImage $image         Image resource to be streamed.
 378           * @param int              $attachment_id The attachment post ID.
 379           */
 380          $image = apply_filters_deprecated( 'image_save_pre', array( $image, $attachment_id ), '3.5.0', 'image_editor_save_pre' );
 381  
 382          switch ( $mime_type ) {
 383              case 'image/jpeg':
 384                  header( 'Content-Type: image/jpeg' );
 385                  return imagejpeg( $image, null, 90 );
 386              case 'image/png':
 387                  header( 'Content-Type: image/png' );
 388                  return imagepng( $image );
 389              case 'image/gif':
 390                  header( 'Content-Type: image/gif' );
 391                  return imagegif( $image );
 392              case 'image/webp':
 393                  if ( function_exists( 'imagewebp' ) ) {
 394                      header( 'Content-Type: image/webp' );
 395                      return imagewebp( $image, null, 90 );
 396                  }
 397                  return false;
 398              case 'image/avif':
 399                  if ( function_exists( 'imageavif' ) ) {
 400                      header( 'Content-Type: image/avif' );
 401                      return imageavif( $image, null, 90 );
 402                  }
 403                  return false;
 404              default:
 405                  return false;
 406          }
 407      }
 408  }
 409  
 410  /**
 411   * Saves image to file.
 412   *
 413   * @since 2.9.0
 414   * @since 3.5.0 The `$image` parameter expects a `WP_Image_Editor` instance.
 415   * @since 6.0.0 The `$filesize` value was added to the returned array.
 416   *
 417   * @param string          $filename  Name of the file to be saved.
 418   * @param WP_Image_Editor $image     The image editor instance.
 419   * @param string          $mime_type The mime type of the image.
 420   * @param int             $post_id   Attachment post ID.
 421   * @return array|WP_Error|bool {
 422   *     Array on success or WP_Error if the file failed to save.
 423   *     When called with a deprecated value for the `$image` parameter,
 424   *     i.e. a non-`WP_Image_Editor` image resource or `GdImage` instance,
 425   *     the function will return true on success, false on failure.
 426   *
 427   *     @type string $path      Path to the image file.
 428   *     @type string $file      Name of the image file.
 429   *     @type int    $width     Image width.
 430   *     @type int    $height    Image height.
 431   *     @type string $mime-type The mime type of the image.
 432   *     @type int    $filesize  File size of the image.
 433   * }
 434   */
 435  function wp_save_image_file( $filename, $image, $mime_type, $post_id ) {
 436      if ( $image instanceof WP_Image_Editor ) {
 437  
 438          /** This filter is documented in wp-admin/includes/image-edit.php */
 439          $image = apply_filters( 'image_editor_save_pre', $image, $post_id );
 440  
 441          /**
 442           * Filters whether to skip saving the image file.
 443           *
 444           * Returning a non-null value will short-circuit the save method,
 445           * returning that value instead.
 446           *
 447           * @since 3.5.0
 448           *
 449           * @param bool|null       $override  Value to return instead of saving. Default null.
 450           * @param string          $filename  Name of the file to be saved.
 451           * @param WP_Image_Editor $image     The image editor instance.
 452           * @param string          $mime_type The mime type of the image.
 453           * @param int             $post_id   Attachment post ID.
 454           */
 455          $saved = apply_filters( 'wp_save_image_editor_file', null, $filename, $image, $mime_type, $post_id );
 456  
 457          if ( null !== $saved ) {
 458              return $saved;
 459          }
 460  
 461          return $image->save( $filename, $mime_type );
 462      } else {
 463          /* translators: 1: $image, 2: WP_Image_Editor */
 464          _deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( '%1$s needs to be a %2$s object.' ), '$image', 'WP_Image_Editor' ) );
 465  
 466          /** This filter is documented in wp-admin/includes/image-edit.php */
 467          $image = apply_filters_deprecated( 'image_save_pre', array( $image, $post_id ), '3.5.0', 'image_editor_save_pre' );
 468  
 469          /**
 470           * Filters whether to skip saving the image file.
 471           *
 472           * Returning a non-null value will short-circuit the save method,
 473           * returning that value instead.
 474           *
 475           * @since 2.9.0
 476           * @deprecated 3.5.0 Use {@see 'wp_save_image_editor_file'} instead.
 477           *
 478           * @param bool|null        $override  Value to return instead of saving. Default null.
 479           * @param string           $filename  Name of the file to be saved.
 480           * @param resource|GdImage $image     Image resource or GdImage instance.
 481           * @param string           $mime_type The mime type of the image.
 482           * @param int              $post_id   Attachment post ID.
 483           */
 484          $saved = apply_filters_deprecated(
 485              'wp_save_image_file',
 486              array( null, $filename, $image, $mime_type, $post_id ),
 487              '3.5.0',
 488              'wp_save_image_editor_file'
 489          );
 490  
 491          if ( null !== $saved ) {
 492              return $saved;
 493          }
 494  
 495          switch ( $mime_type ) {
 496              case 'image/jpeg':
 497                  /** This filter is documented in wp-includes/class-wp-image-editor.php */
 498                  return imagejpeg( $image, $filename, apply_filters( 'jpeg_quality', 90, 'edit_image' ) );
 499              case 'image/png':
 500                  return imagepng( $image, $filename );
 501              case 'image/gif':
 502                  return imagegif( $image, $filename );
 503              case 'image/webp':
 504                  if ( function_exists( 'imagewebp' ) ) {
 505                      return imagewebp( $image, $filename );
 506                  }
 507                  return false;
 508              case 'image/avif':
 509                  if ( function_exists( 'imageavif' ) ) {
 510                      return imageavif( $image, $filename );
 511                  }
 512                  return false;
 513              default:
 514                  return false;
 515          }
 516      }
 517  }
 518  
 519  /**
 520   * Image preview ratio. Internal use only.
 521   *
 522   * @since 2.9.0
 523   *
 524   * @ignore
 525   * @param int $w Image width in pixels.
 526   * @param int $h Image height in pixels.
 527   * @return float|int Image preview ratio.
 528   */
 529  function _image_get_preview_ratio( $w, $h ) {
 530      $max = max( $w, $h );
 531      return $max > 600 ? ( 600 / $max ) : 1;
 532  }
 533  
 534  /**
 535   * Returns an image resource. Internal use only.
 536   *
 537   * @since 2.9.0
 538   * @deprecated 3.5.0 Use WP_Image_Editor::rotate()
 539   * @see WP_Image_Editor::rotate()
 540   *
 541   * @ignore
 542   * @param resource|GdImage $img   Image resource.
 543   * @param float|int        $angle Image rotation angle, in degrees.
 544   * @return resource|GdImage|false GD image resource or GdImage instance, false otherwise.
 545   */
 546  function _rotate_image_resource( $img, $angle ) {
 547      _deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::rotate()' );
 548  
 549      if ( function_exists( 'imagerotate' ) ) {
 550          $rotated = imagerotate( $img, $angle, 0 );
 551  
 552          if ( is_gd_image( $rotated ) ) {
 553              if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
 554                  imagedestroy( $img );
 555              }
 556  
 557              $img = $rotated;
 558          }
 559      }
 560  
 561      return $img;
 562  }
 563  
 564  /**
 565   * Flips an image resource. Internal use only.
 566   *
 567   * @since 2.9.0
 568   * @since 7.2.0 Switched to using imageflip() instead of imagecopyresampled() with negative dimensions.
 569   * @deprecated 3.5.0 Use WP_Image_Editor::flip()
 570   * @see WP_Image_Editor::flip()
 571   *
 572   * @ignore
 573   * @param resource|GdImage $img  Image resource or GdImage instance.
 574   * @param bool             $horz Whether to flip horizontally.
 575   * @param bool             $vert Whether to flip vertically.
 576   * @return resource|GdImage (maybe) flipped image resource or GdImage instance.
 577   */
 578  function _flip_image_resource( $img, $horz, $vert ) {
 579      _deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::flip()' );
 580  
 581      if ( ! is_gd_image( $img ) ) {
 582          return $img;
 583      }
 584  
 585      // Keep returning a truecolor image, like the previous implementation did. imagewebp() rejects palette images.
 586      if ( ! imageistruecolor( $img ) && ! imagepalettetotruecolor( $img ) ) {
 587          return $img;
 588      }
 589  
 590      // Match the alpha settings of the previous implementation's copy, including when no axis is flipped.
 591      imagealphablending( $img, false );
 592      imagesavealpha( $img, true );
 593  
 594      /*
 595       * imageflip() changes the image in place, so the image keeps its current resolution and interlace setting.
 596       * _rotate_image_resource() and _crop_image_resource() copy into a new image, which resets them to GD's defaults:
 597       * 96 DPI and no interlacing. Reset them here too, so a flip gives the same result as the other edits.
 598       *
 599       * @link https://core.trac.wordpress.org/ticket/66113
 600       */
 601      imageresolution( $img, 96, 96 );
 602      imageinterlace( $img, 0 );
 603  
 604      if ( ! $horz && ! $vert ) {
 605          return $img;
 606      }
 607  
 608      if ( $horz && $vert ) {
 609          $mode = IMG_FLIP_BOTH;
 610      } elseif ( $horz ) {
 611          $mode = IMG_FLIP_VERTICAL;
 612      } else {
 613          $mode = IMG_FLIP_HORIZONTAL;
 614      }
 615  
 616      imageflip( $img, $mode );
 617  
 618      return $img;
 619  }
 620  
 621  /**
 622   * Crops an image resource. Internal use only.
 623   *
 624   * @since 2.9.0
 625   *
 626   * @ignore
 627   * @param resource|GdImage $img Image resource or GdImage instance.
 628   * @param float            $x   Source point x-coordinate.
 629   * @param float            $y   Source point y-coordinate.
 630   * @param float            $w   Source width.
 631   * @param float            $h   Source height.
 632   * @return resource|GdImage (maybe) cropped image resource or GdImage instance.
 633   */
 634  function _crop_image_resource( $img, $x, $y, $w, $h ) {
 635      $dst = wp_imagecreatetruecolor( $w, $h );
 636  
 637      if ( is_gd_image( $dst ) ) {
 638          if ( imagecopy( $dst, $img, 0, 0, $x, $y, $w, $h ) ) {
 639              if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
 640                  imagedestroy( $img );
 641              }
 642  
 643              $img = $dst;
 644          }
 645      }
 646  
 647      return $img;
 648  }
 649  
 650  /**
 651   * Performs group of changes on Editor specified.
 652   *
 653   * @since 2.9.0
 654   *
 655   * @param WP_Image_Editor $image   WP_Image_Editor instance.
 656   * @param array           $changes Array of change operations.
 657   * @return WP_Image_Editor WP_Image_Editor instance with changes applied.
 658   */
 659  function image_edit_apply_changes( $image, $changes ) {
 660      if ( is_gd_image( $image ) ) {
 661          /* translators: 1: $image, 2: WP_Image_Editor */
 662          _deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( '%1$s needs to be a %2$s object.' ), '$image', 'WP_Image_Editor' ) );
 663      }
 664  
 665      if ( ! is_array( $changes ) ) {
 666          return $image;
 667      }
 668  
 669      // Expand change operations.
 670      foreach ( $changes as $key => $obj ) {
 671          if ( isset( $obj->r ) ) {
 672              $obj->type  = 'rotate';
 673              $obj->angle = $obj->r;
 674              unset( $obj->r );
 675          } elseif ( isset( $obj->f ) ) {
 676              $obj->type = 'flip';
 677              $obj->axis = $obj->f;
 678              unset( $obj->f );
 679          } elseif ( isset( $obj->c ) ) {
 680              $obj->type = 'crop';
 681              $obj->sel  = $obj->c;
 682              unset( $obj->c );
 683          }
 684  
 685          $changes[ $key ] = $obj;
 686      }
 687  
 688      // Combine operations.
 689      if ( count( $changes ) > 1 ) {
 690          $filtered = array( $changes[0] );
 691  
 692          for ( $i = 0, $j = 1, $c = count( $changes ); $j < $c; $j++ ) {
 693              $combined = false;
 694  
 695              if ( $filtered[ $i ]->type === $changes[ $j ]->type ) {
 696                  switch ( $filtered[ $i ]->type ) {
 697                      case 'rotate':
 698                          $filtered[ $i ]->angle += $changes[ $j ]->angle;
 699                          $combined               = true;
 700                          break;
 701                      case 'flip':
 702                          $filtered[ $i ]->axis ^= $changes[ $j ]->axis;
 703                          $combined              = true;
 704                          break;
 705                  }
 706              }
 707  
 708              if ( ! $combined ) {
 709                  $filtered[ ++$i ] = $changes[ $j ];
 710              }
 711          }
 712  
 713          $changes = $filtered;
 714          unset( $filtered );
 715      }
 716  
 717      // Image resource before applying the changes.
 718      if ( $image instanceof WP_Image_Editor ) {
 719  
 720          /**
 721           * Filters the WP_Image_Editor instance before applying changes to the image.
 722           *
 723           * @since 3.5.0
 724           *
 725           * @param WP_Image_Editor $image   WP_Image_Editor instance.
 726           * @param array           $changes Array of change operations.
 727           */
 728          $image = apply_filters( 'wp_image_editor_before_change', $image, $changes );
 729      } elseif ( is_gd_image( $image ) ) {
 730  
 731          /**
 732           * Filters the GD image resource before applying changes to the image.
 733           *
 734           * @since 2.9.0
 735           * @deprecated 3.5.0 Use {@see 'wp_image_editor_before_change'} instead.
 736           *
 737           * @param resource|GdImage $image   GD image resource or GdImage instance.
 738           * @param array            $changes Array of change operations.
 739           */
 740          $image = apply_filters_deprecated( 'image_edit_before_change', array( $image, $changes ), '3.5.0', 'wp_image_editor_before_change' );
 741      }
 742  
 743      foreach ( $changes as $operation ) {
 744          switch ( $operation->type ) {
 745              case 'rotate':
 746                  if ( 0 !== $operation->angle ) {
 747                      if ( $image instanceof WP_Image_Editor ) {
 748                          $image->rotate( $operation->angle );
 749                      } else {
 750                          $image = _rotate_image_resource( $image, $operation->angle );
 751                      }
 752                  }
 753                  break;
 754              case 'flip':
 755                  if ( 0 !== $operation->axis ) {
 756                      if ( $image instanceof WP_Image_Editor ) {
 757                          $image->flip( ( $operation->axis & 1 ) !== 0, ( $operation->axis & 2 ) !== 0 );
 758                      } else {
 759                          $image = _flip_image_resource( $image, ( $operation->axis & 1 ) !== 0, ( $operation->axis & 2 ) !== 0 );
 760                      }
 761                  }
 762                  break;
 763              case 'crop':
 764                  $sel = $operation->sel;
 765  
 766                  if ( $image instanceof WP_Image_Editor ) {
 767                      $size = $image->get_size();
 768                      $w    = $size['width'];
 769                      $h    = $size['height'];
 770  
 771                      $scale = $sel->r ?? 1 / _image_get_preview_ratio( $w, $h ); // Discard preview scaling.
 772                      $image->crop( (int) ( $sel->x * $scale ), (int) ( $sel->y * $scale ), (int) ( $sel->w * $scale ), (int) ( $sel->h * $scale ) );
 773                  } else {
 774                      $scale = $sel->r ?? 1 / _image_get_preview_ratio( imagesx( $image ), imagesy( $image ) ); // Discard preview scaling.
 775                      $image = _crop_image_resource( $image, $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale );
 776                  }
 777                  break;
 778          }
 779      }
 780  
 781      return $image;
 782  }
 783  
 784  
 785  /**
 786   * Streams image in post to browser, along with enqueued changes
 787   * in `$_REQUEST['history']`.
 788   *
 789   * @since 2.9.0
 790   *
 791   * @param int $post_id Attachment post ID.
 792   * @return bool True on success, false on failure.
 793   */
 794  function stream_preview_image( $post_id ) {
 795      $post = get_post( $post_id );
 796  
 797      wp_raise_memory_limit( 'admin' );
 798  
 799      $img = wp_get_image_editor( _load_image_to_edit_path( $post_id ) );
 800  
 801      if ( is_wp_error( $img ) ) {
 802          return false;
 803      }
 804  
 805      $changes = ! empty( $_REQUEST['history'] ) ? json_decode( wp_unslash( $_REQUEST['history'] ) ) : null;
 806      if ( $changes ) {
 807          $img = image_edit_apply_changes( $img, $changes );
 808      }
 809  
 810      // Scale the image.
 811      $size = $img->get_size();
 812      $w    = $size['width'];
 813      $h    = $size['height'];
 814  
 815      $ratio = _image_get_preview_ratio( $w, $h );
 816      $w2    = max( 1, $w * $ratio );
 817      $h2    = max( 1, $h * $ratio );
 818  
 819      if ( is_wp_error( $img->resize( $w2, $h2 ) ) ) {
 820          return false;
 821      }
 822  
 823      return wp_stream_image( $img, $post->post_mime_type, $post_id );
 824  }
 825  
 826  /**
 827   * Restores the metadata for a given attachment.
 828   *
 829   * @since 2.9.0
 830   *
 831   * @param int $post_id Attachment post ID.
 832   * @return stdClass Image restoration message object.
 833   */
 834  function wp_restore_image( $post_id ) {
 835      $meta             = wp_get_attachment_metadata( $post_id );
 836      $file             = get_attached_file( $post_id );
 837      $backup_sizes     = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true );
 838      $old_backup_sizes = $backup_sizes;
 839      $restored         = false;
 840      $msg              = new stdClass();
 841  
 842      if ( ! is_array( $meta ) || ! is_array( $backup_sizes ) ) {
 843          $msg->error = __( 'Cannot load image metadata.' );
 844          return $msg;
 845      }
 846  
 847      $meta['sizes'] ??= array();
 848  
 849      $parts         = pathinfo( $file );
 850      $suffix        = time() . rand( 100, 999 );
 851      $default_sizes = get_intermediate_image_sizes();
 852  
 853      if ( isset( $backup_sizes['full-orig'] ) && is_array( $backup_sizes['full-orig'] ) ) {
 854          $data = $backup_sizes['full-orig'];
 855  
 856          if ( $parts['basename'] !== $data['file'] ) {
 857              if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) {
 858                  // Delete only if it's an edited image.
 859                  if ( preg_match( '/-e[0-9]{13}\./', $parts['basename'] ) ) {
 860                      wp_delete_file( $file );
 861                  }
 862              } elseif ( isset( $meta['width'], $meta['height'] ) ) {
 863                  $backup_sizes[ "full-$suffix" ] = array(
 864                      'width'    => $meta['width'],
 865                      'height'   => $meta['height'],
 866                      'filesize' => $meta['filesize'],
 867                      'file'     => $parts['basename'],
 868                  );
 869              }
 870          }
 871  
 872          $restored_file = path_join( $parts['dirname'], $data['file'] );
 873          $restored      = update_attached_file( $post_id, $restored_file );
 874  
 875          $meta['file']   = _wp_relative_upload_path( $restored_file );
 876          $meta['width']  = $data['width'];
 877          $meta['height'] = $data['height'];
 878          if ( isset( $data['filesize'] ) ) {
 879              /*
 880               * Restore the original filesize if it was backed up.
 881               *
 882               * See https://core.trac.wordpress.org/ticket/59684.
 883               */
 884              $meta['filesize'] = $data['filesize'];
 885          }
 886      }
 887  
 888      foreach ( $default_sizes as $default_size ) {
 889          if ( isset( $backup_sizes[ "$default_size-orig" ] ) ) {
 890              $data = $backup_sizes[ "$default_size-orig" ];
 891  
 892              if ( isset( $meta['sizes'][ $default_size ] ) && $meta['sizes'][ $default_size ]['file'] !== $data['file'] ) {
 893                  if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) {
 894                      // Delete only if it's an edited image.
 895                      if ( preg_match( '/-e[0-9]{13}-/', $meta['sizes'][ $default_size ]['file'] ) ) {
 896                          $delete_file = path_join( $parts['dirname'], $meta['sizes'][ $default_size ]['file'] );
 897                          wp_delete_file( $delete_file );
 898                      }
 899                  } else {
 900                      $backup_sizes[ "$default_size-{$suffix}" ] = $meta['sizes'][ $default_size ];
 901                  }
 902              }
 903  
 904              $meta['sizes'][ $default_size ] = $data;
 905          } else {
 906              unset( $meta['sizes'][ $default_size ] );
 907          }
 908      }
 909  
 910      if ( ! wp_update_attachment_metadata( $post_id, $meta )
 911          || ( $old_backup_sizes !== $backup_sizes && ! update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes ) )
 912      ) {
 913          $msg->error = __( 'Cannot save image metadata.' );
 914          return $msg;
 915      }
 916  
 917      if ( ! $restored ) {
 918          $msg->error = __( 'Image metadata is inconsistent.' );
 919      } else {
 920          $msg->msg = __( 'Image restored successfully.' );
 921  
 922          if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) {
 923              delete_post_meta( $post_id, '_wp_attachment_backup_sizes' );
 924          }
 925      }
 926  
 927      return $msg;
 928  }
 929  
 930  /**
 931   * Saves image to post, along with enqueued changes
 932   * in `$_REQUEST['history']`.
 933   *
 934   * @since 2.9.0
 935   *
 936   * @param int $post_id Attachment post ID.
 937   * @return stdClass
 938   */
 939  function wp_save_image( $post_id ) {
 940      $_wp_additional_image_sizes = wp_get_additional_image_sizes();
 941  
 942      $return  = new stdClass();
 943      $success = false;
 944      $delete  = false;
 945      $scaled  = false;
 946      $nocrop  = false;
 947      $post    = get_post( $post_id );
 948  
 949      $img = wp_get_image_editor( _load_image_to_edit_path( $post_id, 'full' ) );
 950  
 951      if ( is_wp_error( $img ) ) {
 952          $return->error = esc_js( __( 'Unable to create new image.' ) );
 953          return $return;
 954      }
 955  
 956      $full_width  = ! empty( $_REQUEST['fwidth'] ) ? (int) $_REQUEST['fwidth'] : 0;
 957      $full_height = ! empty( $_REQUEST['fheight'] ) ? (int) $_REQUEST['fheight'] : 0;
 958      $target      = ! empty( $_REQUEST['target'] ) ? preg_replace( '/[^a-z0-9_-]+/i', '', $_REQUEST['target'] ) : '';
 959      $scale       = ! empty( $_REQUEST['do'] ) && 'scale' === $_REQUEST['do'];
 960  
 961      /** This filter is documented in wp-admin/includes/image-edit.php */
 962      $edit_thumbnails_separately = (bool) apply_filters( 'image_edit_thumbnails_separately', false );
 963  
 964      if ( $scale ) {
 965          $size            = $img->get_size();
 966          $original_width  = $size['width'];
 967          $original_height = $size['height'];
 968  
 969          if ( $full_width > $original_width || $full_height > $original_height ) {
 970              $return->error = esc_js( __( 'Images cannot be scaled to a size larger than the original.' ) );
 971              return $return;
 972          }
 973  
 974          if ( $full_width > 0 && $full_height > 0 ) {
 975              // Check if it has roughly the same w / h ratio.
 976              $diff = round( $original_width / $original_height, 2 ) - round( $full_width / $full_height, 2 );
 977              if ( -0.1 < $diff && $diff < 0.1 ) {
 978                  // Scale the full size image.
 979                  if ( $img->resize( $full_width, $full_height ) ) {
 980                      $scaled = true;
 981                  }
 982              }
 983  
 984              if ( ! $scaled ) {
 985                  $return->error = esc_js( __( 'Error while saving the scaled image. Please reload the page and try again.' ) );
 986                  return $return;
 987              }
 988          }
 989      } elseif ( ! empty( $_REQUEST['history'] ) ) {
 990          $changes = json_decode( wp_unslash( $_REQUEST['history'] ) );
 991          if ( $changes ) {
 992              $img = image_edit_apply_changes( $img, $changes );
 993          }
 994      } else {
 995          $return->error = esc_js( __( 'Nothing to save, the image has not changed.' ) );
 996          return $return;
 997      }
 998  
 999      $meta         = wp_get_attachment_metadata( $post_id );
1000      $backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true );
1001  
1002      if ( ! is_array( $meta ) ) {
1003          $return->error = esc_js( __( 'Image data does not exist. Please re-upload the image.' ) );
1004          return $return;
1005      }
1006  
1007      $meta['sizes'] ??= array();
1008  
1009      if ( ! is_array( $backup_sizes ) ) {
1010          $backup_sizes = array();
1011      }
1012  
1013      // Generate new filename.
1014      $path = get_attached_file( $post_id );
1015  
1016      $basename = pathinfo( $path, PATHINFO_BASENAME );
1017      $dirname  = pathinfo( $path, PATHINFO_DIRNAME );
1018      $ext      = pathinfo( $path, PATHINFO_EXTENSION );
1019      $filename = pathinfo( $path, PATHINFO_FILENAME );
1020      $suffix   = time() . rand( 100, 999 );
1021  
1022      if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE
1023          && isset( $backup_sizes['full-orig'] ) && $backup_sizes['full-orig']['file'] !== $basename
1024      ) {
1025  
1026          if ( $edit_thumbnails_separately && 'thumbnail' === $target ) {
1027              $new_path = "{$dirname}/{$filename}-temp.{$ext}";
1028          } else {
1029              $new_path = $path;
1030          }
1031      } else {
1032          while ( true ) {
1033              $filename     = preg_replace( '/-e([0-9]+)$/', '', $filename );
1034              $filename    .= "-e{$suffix}";
1035              $new_filename = "{$filename}.{$ext}";
1036              $new_path     = "{$dirname}/$new_filename";
1037  
1038              if ( file_exists( $new_path ) ) {
1039                  ++$suffix;
1040              } else {
1041                  break;
1042              }
1043          }
1044      }
1045  
1046      $saved_image = wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id );
1047      // Save the full-size file, also needed to create sub-sizes.
1048      if ( ! $saved_image ) {
1049          $return->error = esc_js( __( 'Unable to save the image.' ) );
1050          return $return;
1051      }
1052  
1053      if ( 'nothumb' === $target || 'all' === $target || 'full' === $target || $scaled ) {
1054          $tag = false;
1055  
1056          if ( isset( $backup_sizes['full-orig'] ) ) {
1057              if ( ( ! defined( 'IMAGE_EDIT_OVERWRITE' ) || ! IMAGE_EDIT_OVERWRITE )
1058                  && $backup_sizes['full-orig']['file'] !== $basename
1059              ) {
1060                  $tag = "full-$suffix";
1061              }
1062          } else {
1063              $tag = 'full-orig';
1064          }
1065  
1066          if ( $tag ) {
1067              $backup_sizes[ $tag ] = array(
1068                  'width'    => $meta['width'],
1069                  'height'   => $meta['height'],
1070                  'filesize' => $meta['filesize'],
1071                  'file'     => $basename,
1072              );
1073          }
1074  
1075          $success = ( $path === $new_path ) || update_attached_file( $post_id, $new_path );
1076  
1077          $meta['file'] = _wp_relative_upload_path( $new_path );
1078  
1079          $size             = $img->get_size();
1080          $meta['width']    = $size['width'];
1081          $meta['height']   = $size['height'];
1082          $meta['filesize'] = $saved_image['filesize'];
1083  
1084          if ( $success && ( 'nothumb' === $target || 'all' === $target ) ) {
1085              $sizes = get_intermediate_image_sizes();
1086  
1087              if ( $edit_thumbnails_separately && 'nothumb' === $target ) {
1088                  $sizes = array_diff( $sizes, array( 'thumbnail' ) );
1089              }
1090          }
1091  
1092          $return->fw = $meta['width'];
1093          $return->fh = $meta['height'];
1094      } elseif ( $edit_thumbnails_separately && 'thumbnail' === $target ) {
1095          $sizes   = array( 'thumbnail' );
1096          $success = true;
1097          $delete  = true;
1098          $nocrop  = true;
1099      }
1100  
1101      /*
1102       * We need to remove any existing resized image files because
1103       * a new crop or rotate could generate different sizes (and hence, filenames),
1104       * keeping the new resized images from overwriting the existing image files.
1105       * https://core.trac.wordpress.org/ticket/32171
1106       */
1107      if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE && ! empty( $meta['sizes'] ) ) {
1108          foreach ( $meta['sizes'] as $size ) {
1109              if ( ! empty( $size['file'] ) && preg_match( '/-e[0-9]{13}-/', $size['file'] ) ) {
1110                  $delete_file = path_join( $dirname, $size['file'] );
1111                  wp_delete_file( $delete_file );
1112              }
1113          }
1114      }
1115  
1116      if ( isset( $sizes ) ) {
1117          $_sizes = array();
1118  
1119          foreach ( $sizes as $size ) {
1120              $tag = false;
1121  
1122              if ( isset( $meta['sizes'][ $size ] ) ) {
1123                  if ( isset( $backup_sizes[ "$size-orig" ] ) ) {
1124                      if ( ( ! defined( 'IMAGE_EDIT_OVERWRITE' ) || ! IMAGE_EDIT_OVERWRITE )
1125                          && $backup_sizes[ "$size-orig" ]['file'] !== $meta['sizes'][ $size ]['file']
1126                      ) {
1127                          $tag = "$size-$suffix";
1128                      }
1129                  } else {
1130                      $tag = "$size-orig";
1131                  }
1132  
1133                  if ( $tag ) {
1134                      $backup_sizes[ $tag ] = $meta['sizes'][ $size ];
1135                  }
1136              }
1137  
1138              if ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
1139                  $width  = (int) $_wp_additional_image_sizes[ $size ]['width'];
1140                  $height = (int) $_wp_additional_image_sizes[ $size ]['height'];
1141                  $crop   = ( $nocrop ) ? false : $_wp_additional_image_sizes[ $size ]['crop'];
1142              } else {
1143                  $height = get_option( "{$size}_size_h" );
1144                  $width  = get_option( "{$size}_size_w" );
1145                  $crop   = ( $nocrop ) ? false : get_option( "{$size}_crop" );
1146              }
1147  
1148              $_sizes[ $size ] = array(
1149                  'width'  => $width,
1150                  'height' => $height,
1151                  'crop'   => $crop,
1152              );
1153          }
1154  
1155          $meta['sizes'] = array_merge( $meta['sizes'], $img->multi_resize( $_sizes ) );
1156      }
1157  
1158      unset( $img );
1159  
1160      if ( $success ) {
1161          wp_update_attachment_metadata( $post_id, $meta );
1162          update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes );
1163  
1164          if ( 'thumbnail' === $target || 'all' === $target || 'full' === $target ) {
1165              // Check if it's an image edit from attachment edit screen.
1166              if ( ! empty( $_REQUEST['context'] ) && 'edit-attachment' === $_REQUEST['context'] ) {
1167                  $thumb_url = wp_get_attachment_image_src( $post_id, array( 900, 600 ), true );
1168  
1169                  $return->thumbnail = $thumb_url[0];
1170              } else {
1171                  $file_url = wp_get_attachment_url( $post_id );
1172  
1173                  if ( ! empty( $meta['sizes']['thumbnail'] ) ) {
1174                      $thumb             = $meta['sizes']['thumbnail'];
1175                      $return->thumbnail = path_join( dirname( $file_url ), $thumb['file'] );
1176                  } else {
1177                      $return->thumbnail = "$file_url?w=128&h=128";
1178                  }
1179              }
1180          }
1181      } else {
1182          $delete = true;
1183      }
1184  
1185      if ( $delete ) {
1186          wp_delete_file( $new_path );
1187      }
1188  
1189      $return->msg = esc_js( __( 'Image saved' ) );
1190  
1191      return $return;
1192  }


Generated : Wed Nov 11 08:20:02 2026 Cross-referenced by PHPXref