wp/wp-admin/includes/image-edit.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
    73 					'methods'   => array( 'rotate' ),
    73 					'methods'   => array( 'rotate' ),
    74 				)
    74 				)
    75 			) ) {
    75 			) ) {
    76 				$note_no_rotate = '';
    76 				$note_no_rotate = '';
    77 				?>
    77 				?>
    78 					<button type="button" class="imgedit-rleft button" onkeyup="imageEdit.browsePopup(this)" onclick="imageEdit.rotate( 90, <?php echo "$post_id, '$nonce'"; ?>, this)" onblur="imageEdit.monitorPopup()"><?php esc_html_e( 'Rotate 90&deg; left' ); ?></button>
    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" onkeyup="imageEdit.browsePopup(this)" onclick="imageEdit.rotate(-90, <?php echo "$post_id, '$nonce'"; ?>, this)" onblur="imageEdit.monitorPopup()"><?php esc_html_e( 'Rotate 90&deg; right' ); ?></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" onkeyup="imageEdit.browsePopup(this)" onclick="imageEdit.rotate(180, <?php echo "$post_id, '$nonce'"; ?>, this)" onblur="imageEdit.monitorPopup()"><?php esc_html_e( 'Rotate 180&deg;' ); ?></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
    81 				<?php
    82 			} else {
    82 			} else {
    83 				$note_no_rotate = '<p class="note-no-rotate"><em>' . __( 'Image rotation is not supported by your web host.' ) . '</em></p>';
    83 				$note_no_rotate = '<p class="note-no-rotate"><em>' . __( 'Image rotation is not supported by your web host.' ) . '</em></p>';
    84 				?>
    84 				?>
    85 					<button type="button" class="imgedit-rleft button disabled" disabled></button>
    85 					<button type="button" class="imgedit-rleft button disabled" disabled></button>
    86 					<button type="button" class="imgedit-rright button disabled" disabled></button>
    86 					<button type="button" class="imgedit-rright button disabled" disabled></button>
    87 				<?php
    87 				<?php
    88 			}
    88 			}
    89 			?>
    89 			?>
    90 					<hr />
    90 					<hr />
    91 					<button type="button" onkeyup="imageEdit.browsePopup(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>
    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" onkeyup="imageEdit.browsePopup(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>
    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; ?>
    93 					<?php echo $note_no_rotate; ?>
    94 				</div>
    94 				</div>
    95 			</div>
    95 			</div>
    96 		</div>
    96 		</div>
    97 		<div class="imgedit-submit imgedit-menu">
    97 		<div class="imgedit-submit imgedit-menu">
   291 			<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>
   291 			<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>
   292 			</div>
   292 			</div>
   293 		</div>
   293 		</div>
   294 		<div class="imgedit-thumbnail-preview-group">
   294 		<div class="imgedit-thumbnail-preview-group">
   295 			<figure class="imgedit-thumbnail-preview">
   295 			<figure class="imgedit-thumbnail-preview">
   296 				<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" />
   296 				<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" />
   297 				<figcaption class="imgedit-thumbnail-preview-caption"><?php _e( 'Current thumbnail' ); ?></figcaption>
   297 				<figcaption class="imgedit-thumbnail-preview-caption"><?php _e( 'Current thumbnail' ); ?></figcaption>
   298 			</figure>
   298 			</figure>
   299 			<div id="imgedit-save-target-<?php echo $post_id; ?>" class="imgedit-save-target">
   299 			<div id="imgedit-save-target-<?php echo $post_id; ?>" class="imgedit-save-target">
   300 			<fieldset>
   300 			<fieldset>
   301 				<legend><?php _e( 'Apply changes to:' ); ?></legend>
   301 				<legend><?php _e( 'Apply changes to:' ); ?></legend>
   532  * @since 2.9.0
   532  * @since 2.9.0
   533  * @deprecated 3.5.0 Use WP_Image_Editor::rotate()
   533  * @deprecated 3.5.0 Use WP_Image_Editor::rotate()
   534  * @see WP_Image_Editor::rotate()
   534  * @see WP_Image_Editor::rotate()
   535  *
   535  *
   536  * @ignore
   536  * @ignore
   537  * @param resource|GdImage  $img   Image resource.
   537  * @param resource|GdImage $img   Image resource.
   538  * @param float|int         $angle Image rotation angle, in degrees.
   538  * @param float|int        $angle Image rotation angle, in degrees.
   539  * @return resource|GdImage|false GD image resource or GdImage instance, false otherwise.
   539  * @return resource|GdImage|false GD image resource or GdImage instance, false otherwise.
   540  */
   540  */
   541 function _rotate_image_resource( $img, $angle ) {
   541 function _rotate_image_resource( $img, $angle ) {
   542 	_deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::rotate()' );
   542 	_deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::rotate()' );
   543 
   543 
   733 				if ( $image instanceof WP_Image_Editor ) {
   733 				if ( $image instanceof WP_Image_Editor ) {
   734 					$size = $image->get_size();
   734 					$size = $image->get_size();
   735 					$w    = $size['width'];
   735 					$w    = $size['width'];
   736 					$h    = $size['height'];
   736 					$h    = $size['height'];
   737 
   737 
   738 					$scale = 1 / _image_get_preview_ratio( $w, $h ); // Discard preview scaling.
   738 					$scale = isset( $sel->r ) ? $sel->r : 1 / _image_get_preview_ratio( $w, $h ); // Discard preview scaling.
   739 					$image->crop( (int) ( $sel->x * $scale ), (int) ( $sel->y * $scale ), (int) ( $sel->w * $scale ), (int) ( $sel->h * $scale ) );
   739 					$image->crop( (int) ( $sel->x * $scale ), (int) ( $sel->y * $scale ), (int) ( $sel->w * $scale ), (int) ( $sel->h * $scale ) );
   740 				} else {
   740 				} else {
   741 					$scale = 1 / _image_get_preview_ratio( imagesx( $image ), imagesy( $image ) ); // Discard preview scaling.
   741 					$scale = isset( $sel->r ) ? $sel->r : 1 / _image_get_preview_ratio( imagesx( $image ), imagesy( $image ) ); // Discard preview scaling.
   742 					$image = _crop_image_resource( $image, $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale );
   742 					$image = _crop_image_resource( $image, $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale );
   743 				}
   743 				}
   744 				break;
   744 				break;
   745 		}
   745 		}
   746 	}
   746 	}
   824 				if ( preg_match( '/-e[0-9]{13}\./', $parts['basename'] ) ) {
   824 				if ( preg_match( '/-e[0-9]{13}\./', $parts['basename'] ) ) {
   825 					wp_delete_file( $file );
   825 					wp_delete_file( $file );
   826 				}
   826 				}
   827 			} elseif ( isset( $meta['width'], $meta['height'] ) ) {
   827 			} elseif ( isset( $meta['width'], $meta['height'] ) ) {
   828 				$backup_sizes[ "full-$suffix" ] = array(
   828 				$backup_sizes[ "full-$suffix" ] = array(
   829 					'width'  => $meta['width'],
   829 					'width'    => $meta['width'],
   830 					'height' => $meta['height'],
   830 					'height'   => $meta['height'],
   831 					'file'   => $parts['basename'],
   831 					'filesize' => $meta['filesize'],
       
   832 					'file'     => $parts['basename'],
   832 				);
   833 				);
   833 			}
   834 			}
   834 		}
   835 		}
   835 
   836 
   836 		$restored_file = path_join( $parts['dirname'], $data['file'] );
   837 		$restored_file = path_join( $parts['dirname'], $data['file'] );
   837 		$restored      = update_attached_file( $post_id, $restored_file );
   838 		$restored      = update_attached_file( $post_id, $restored_file );
   838 
   839 
   839 		$meta['file']   = _wp_relative_upload_path( $restored_file );
   840 		$meta['file']   = _wp_relative_upload_path( $restored_file );
   840 		$meta['width']  = $data['width'];
   841 		$meta['width']  = $data['width'];
   841 		$meta['height'] = $data['height'];
   842 		$meta['height'] = $data['height'];
       
   843 		if ( isset( $data['filesize'] ) ) {
       
   844 			/*
       
   845 			 * Restore the original filesize if it was backed up.
       
   846 			 *
       
   847 			 * See https://core.trac.wordpress.org/ticket/59684.
       
   848 			 */
       
   849 			$meta['filesize'] = $data['filesize'];
       
   850 		}
   842 	}
   851 	}
   843 
   852 
   844 	foreach ( $default_sizes as $default_size ) {
   853 	foreach ( $default_sizes as $default_size ) {
   845 		if ( isset( $backup_sizes[ "$default_size-orig" ] ) ) {
   854 		if ( isset( $backup_sizes[ "$default_size-orig" ] ) ) {
   846 			$data = $backup_sizes[ "$default_size-orig" ];
   855 			$data = $backup_sizes[ "$default_size-orig" ];
   995 				break;
  1004 				break;
   996 			}
  1005 			}
   997 		}
  1006 		}
   998 	}
  1007 	}
   999 
  1008 
       
  1009 	$saved_image = wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id );
  1000 	// Save the full-size file, also needed to create sub-sizes.
  1010 	// Save the full-size file, also needed to create sub-sizes.
  1001 	if ( ! wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id ) ) {
  1011 	if ( ! $saved_image ) {
  1002 		$return->error = esc_js( __( 'Unable to save the image.' ) );
  1012 		$return->error = esc_js( __( 'Unable to save the image.' ) );
  1003 		return $return;
  1013 		return $return;
  1004 	}
  1014 	}
  1005 
  1015 
  1006 	if ( 'nothumb' === $target || 'all' === $target || 'full' === $target || $scaled ) {
  1016 	if ( 'nothumb' === $target || 'all' === $target || 'full' === $target || $scaled ) {
  1016 			$tag = 'full-orig';
  1026 			$tag = 'full-orig';
  1017 		}
  1027 		}
  1018 
  1028 
  1019 		if ( $tag ) {
  1029 		if ( $tag ) {
  1020 			$backup_sizes[ $tag ] = array(
  1030 			$backup_sizes[ $tag ] = array(
  1021 				'width'  => $meta['width'],
  1031 				'width'    => $meta['width'],
  1022 				'height' => $meta['height'],
  1032 				'height'   => $meta['height'],
  1023 				'file'   => $basename,
  1033 				'filesize' => $meta['filesize'],
       
  1034 				'file'     => $basename,
  1024 			);
  1035 			);
  1025 		}
  1036 		}
  1026 
  1037 
  1027 		$success = ( $path === $new_path ) || update_attached_file( $post_id, $new_path );
  1038 		$success = ( $path === $new_path ) || update_attached_file( $post_id, $new_path );
  1028 
  1039 
  1029 		$meta['file'] = _wp_relative_upload_path( $new_path );
  1040 		$meta['file'] = _wp_relative_upload_path( $new_path );
  1030 
  1041 
  1031 		$size           = $img->get_size();
  1042 		$size             = $img->get_size();
  1032 		$meta['width']  = $size['width'];
  1043 		$meta['width']    = $size['width'];
  1033 		$meta['height'] = $size['height'];
  1044 		$meta['height']   = $size['height'];
       
  1045 		$meta['filesize'] = $saved_image['filesize'];
  1034 
  1046 
  1035 		if ( $success && ( 'nothumb' === $target || 'all' === $target ) ) {
  1047 		if ( $success && ( 'nothumb' === $target || 'all' === $target ) ) {
  1036 			$sizes = get_intermediate_image_sizes();
  1048 			$sizes = get_intermediate_image_sizes();
  1037 
  1049 
  1038 			if ( $edit_thumbnails_separately && 'nothumb' === $target ) {
  1050 			if ( $edit_thumbnails_separately && 'nothumb' === $target ) {