wp/wp-admin/includes/image-edit.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
     9 /**
     9 /**
    10  * Loads the WP image-editing interface.
    10  * Loads the WP image-editing interface.
    11  *
    11  *
    12  * @since 2.9.0
    12  * @since 2.9.0
    13  *
    13  *
    14  * @param int         $post_id Attachment post ID.
    14  * @param int          $post_id Attachment post ID.
    15  * @param bool|object $msg     Optional. Message to display for image editor updates or errors.
    15  * @param false|object $msg     Optional. Message to display for image editor updates or errors.
    16  *                             Default false.
    16  *                              Default false.
    17  */
    17  */
    18 function wp_image_editor( $post_id, $msg = false ) {
    18 function wp_image_editor( $post_id, $msg = false ) {
    19 	$nonce     = wp_create_nonce( "image_editor-$post_id" );
    19 	$nonce     = wp_create_nonce( "image_editor-$post_id" );
    20 	$meta      = wp_get_attachment_metadata( $post_id );
    20 	$meta      = wp_get_attachment_metadata( $post_id );
    21 	$thumb     = image_get_intermediate_size( $post_id, 'thumbnail' );
    21 	$thumb     = image_get_intermediate_size( $post_id, 'thumbnail' );
    88 		<input type="hidden" id="imgedit-selection-<?php echo $post_id; ?>" value="" />
    88 		<input type="hidden" id="imgedit-selection-<?php echo $post_id; ?>" value="" />
    89 		<input type="hidden" id="imgedit-x-<?php echo $post_id; ?>" value="<?php echo isset( $meta['width'] ) ? $meta['width'] : 0; ?>" />
    89 		<input type="hidden" id="imgedit-x-<?php echo $post_id; ?>" value="<?php echo isset( $meta['width'] ) ? $meta['width'] : 0; ?>" />
    90 		<input type="hidden" id="imgedit-y-<?php echo $post_id; ?>" value="<?php echo isset( $meta['height'] ) ? $meta['height'] : 0; ?>" />
    90 		<input type="hidden" id="imgedit-y-<?php echo $post_id; ?>" value="<?php echo isset( $meta['height'] ) ? $meta['height'] : 0; ?>" />
    91 
    91 
    92 		<div id="imgedit-crop-<?php echo $post_id; ?>" class="imgedit-crop-wrap">
    92 		<div id="imgedit-crop-<?php echo $post_id; ?>" class="imgedit-crop-wrap">
    93 		<img id="image-preview-<?php echo $post_id; ?>" onload="imageEdit.imgLoaded('<?php echo $post_id; ?>')" src="<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>?action=imgedit-preview&amp;_ajax_nonce=<?php echo $nonce; ?>&amp;postid=<?php echo $post_id; ?>&amp;rand=<?php echo rand( 1, 99999 ); ?>" alt="" />
    93 		<img id="image-preview-<?php echo $post_id; ?>" onload="imageEdit.imgLoaded('<?php echo $post_id; ?>')"
       
    94 			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="" />
    94 		</div>
    95 		</div>
    95 
    96 
    96 		<div class="imgedit-submit">
    97 		<div class="imgedit-submit">
    97 			<input type="button" onclick="imageEdit.close(<?php echo $post_id; ?>, 1)" class="button imgedit-cancel-btn" value="<?php esc_attr_e( 'Cancel' ); ?>" />
    98 			<input type="button" onclick="imageEdit.close(<?php echo $post_id; ?>, 1)" class="button imgedit-cancel-btn" value="<?php esc_attr_e( 'Cancel' ); ?>" />
    98 			<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' ); ?>" />
    99 			<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' ); ?>" />
   115 				__( 'Original dimensions %s' ),
   116 				__( 'Original dimensions %s' ),
   116 				'<span class="imgedit-original-dimensions">' . $meta['width'] . ' &times; ' . $meta['height'] . '</span>'
   117 				'<span class="imgedit-original-dimensions">' . $meta['width'] . ' &times; ' . $meta['height'] . '</span>'
   117 			);
   118 			);
   118 			?>
   119 			?>
   119 		</p>
   120 		</p>
   120 		<?php endif ?>
   121 		<?php endif; ?>
   121 		<div class="imgedit-submit">
   122 		<div class="imgedit-submit">
   122 
   123 
   123 		<fieldset class="imgedit-scale">
   124 		<fieldset class="imgedit-scale">
   124 		<legend><?php _e( 'New dimensions:' ); ?></legend>
   125 		<legend><?php _e( 'New dimensions:' ); ?></legend>
   125 		<div class="nowrap">
   126 		<div class="nowrap">
   289 		 * Filters the GD image resource to be streamed to the browser.
   290 		 * Filters the GD image resource to be streamed to the browser.
   290 		 *
   291 		 *
   291 		 * @since 2.9.0
   292 		 * @since 2.9.0
   292 		 * @deprecated 3.5.0 Use {@see 'image_editor_save_pre'} instead.
   293 		 * @deprecated 3.5.0 Use {@see 'image_editor_save_pre'} instead.
   293 		 *
   294 		 *
   294 		 * @param resource $image         Image resource to be streamed.
   295 		 * @param resource|GdImage $image         Image resource to be streamed.
   295 		 * @param int      $attachment_id The attachment post ID.
   296 		 * @param int              $attachment_id The attachment post ID.
   296 		 */
   297 		 */
   297 		$image = apply_filters_deprecated( 'image_save_pre', array( $image, $attachment_id ), '3.5.0', 'image_editor_save_pre' );
   298 		$image = apply_filters_deprecated( 'image_save_pre', array( $image, $attachment_id ), '3.5.0', 'image_editor_save_pre' );
   298 
   299 
   299 		switch ( $mime_type ) {
   300 		switch ( $mime_type ) {
   300 			case 'image/jpeg':
   301 			case 'image/jpeg':
   304 				header( 'Content-Type: image/png' );
   305 				header( 'Content-Type: image/png' );
   305 				return imagepng( $image );
   306 				return imagepng( $image );
   306 			case 'image/gif':
   307 			case 'image/gif':
   307 				header( 'Content-Type: image/gif' );
   308 				header( 'Content-Type: image/gif' );
   308 				return imagegif( $image );
   309 				return imagegif( $image );
       
   310 			case 'image/webp':
       
   311 				if ( function_exists( 'imagewebp' ) ) {
       
   312 					header( 'Content-Type: image/webp' );
       
   313 					return imagewebp( $image, null, 90 );
       
   314 				}
       
   315 				return false;
   309 			default:
   316 			default:
   310 				return false;
   317 				return false;
   311 		}
   318 		}
   312 	}
   319 	}
   313 }
   320 }
   389 				return imagejpeg( $image, $filename, apply_filters( 'jpeg_quality', 90, 'edit_image' ) );
   396 				return imagejpeg( $image, $filename, apply_filters( 'jpeg_quality', 90, 'edit_image' ) );
   390 			case 'image/png':
   397 			case 'image/png':
   391 				return imagepng( $image, $filename );
   398 				return imagepng( $image, $filename );
   392 			case 'image/gif':
   399 			case 'image/gif':
   393 				return imagegif( $image, $filename );
   400 				return imagegif( $image, $filename );
       
   401 			case 'image/webp':
       
   402 				if ( function_exists( 'imagewebp' ) ) {
       
   403 					return imagewebp( $image, $filename );
       
   404 				}
       
   405 				return false;
   394 			default:
   406 			default:
   395 				return false;
   407 				return false;
   396 		}
   408 		}
   397 	}
   409 	}
   398 }
   410 }
   418  * @since 2.9.0
   430  * @since 2.9.0
   419  * @deprecated 3.5.0 Use WP_Image_Editor::rotate()
   431  * @deprecated 3.5.0 Use WP_Image_Editor::rotate()
   420  * @see WP_Image_Editor::rotate()
   432  * @see WP_Image_Editor::rotate()
   421  *
   433  *
   422  * @ignore
   434  * @ignore
   423  * @param resource  $img   Image resource.
   435  * @param resource|GdImage  $img   Image resource.
   424  * @param float|int $angle Image rotation angle, in degrees.
   436  * @param float|int         $angle Image rotation angle, in degrees.
   425  * @return resource|false GD image resource, false otherwise.
   437  * @return resource|GdImage|false GD image resource or GdImage instance, false otherwise.
   426  */
   438  */
   427 function _rotate_image_resource( $img, $angle ) {
   439 function _rotate_image_resource( $img, $angle ) {
   428 	_deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::rotate()' );
   440 	_deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::rotate()' );
       
   441 
   429 	if ( function_exists( 'imagerotate' ) ) {
   442 	if ( function_exists( 'imagerotate' ) ) {
   430 		$rotated = imagerotate( $img, $angle, 0 );
   443 		$rotated = imagerotate( $img, $angle, 0 );
   431 		if ( is_resource( $rotated ) ) {
   444 
       
   445 		if ( is_gd_image( $rotated ) ) {
   432 			imagedestroy( $img );
   446 			imagedestroy( $img );
   433 			$img = $rotated;
   447 			$img = $rotated;
   434 		}
   448 		}
   435 	}
   449 	}
       
   450 
   436 	return $img;
   451 	return $img;
   437 }
   452 }
   438 
   453 
   439 /**
   454 /**
   440  * Flips an image resource. Internal use only.
   455  * Flips an image resource. Internal use only.
   442  * @since 2.9.0
   457  * @since 2.9.0
   443  * @deprecated 3.5.0 Use WP_Image_Editor::flip()
   458  * @deprecated 3.5.0 Use WP_Image_Editor::flip()
   444  * @see WP_Image_Editor::flip()
   459  * @see WP_Image_Editor::flip()
   445  *
   460  *
   446  * @ignore
   461  * @ignore
   447  * @param resource $img  Image resource.
   462  * @param resource|GdImage $img  Image resource or GdImage instance.
   448  * @param bool     $horz Whether to flip horizontally.
   463  * @param bool             $horz Whether to flip horizontally.
   449  * @param bool     $vert Whether to flip vertically.
   464  * @param bool             $vert Whether to flip vertically.
   450  * @return resource (maybe) flipped image resource.
   465  * @return resource|GdImage (maybe) flipped image resource or GdImage instance.
   451  */
   466  */
   452 function _flip_image_resource( $img, $horz, $vert ) {
   467 function _flip_image_resource( $img, $horz, $vert ) {
   453 	_deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::flip()' );
   468 	_deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::flip()' );
       
   469 
   454 	$w   = imagesx( $img );
   470 	$w   = imagesx( $img );
   455 	$h   = imagesy( $img );
   471 	$h   = imagesy( $img );
   456 	$dst = wp_imagecreatetruecolor( $w, $h );
   472 	$dst = wp_imagecreatetruecolor( $w, $h );
   457 	if ( is_resource( $dst ) ) {
   473 
       
   474 	if ( is_gd_image( $dst ) ) {
   458 		$sx = $vert ? ( $w - 1 ) : 0;
   475 		$sx = $vert ? ( $w - 1 ) : 0;
   459 		$sy = $horz ? ( $h - 1 ) : 0;
   476 		$sy = $horz ? ( $h - 1 ) : 0;
   460 		$sw = $vert ? -$w : $w;
   477 		$sw = $vert ? -$w : $w;
   461 		$sh = $horz ? -$h : $h;
   478 		$sh = $horz ? -$h : $h;
   462 
   479 
   463 		if ( imagecopyresampled( $dst, $img, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) {
   480 		if ( imagecopyresampled( $dst, $img, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) {
   464 			imagedestroy( $img );
   481 			imagedestroy( $img );
   465 			$img = $dst;
   482 			$img = $dst;
   466 		}
   483 		}
   467 	}
   484 	}
       
   485 
   468 	return $img;
   486 	return $img;
   469 }
   487 }
   470 
   488 
   471 /**
   489 /**
   472  * Crops an image resource. Internal use only.
   490  * Crops an image resource. Internal use only.
   473  *
   491  *
   474  * @since 2.9.0
   492  * @since 2.9.0
   475  *
   493  *
   476  * @ignore
   494  * @ignore
   477  * @param resource $img Image resource.
   495  * @param resource|GdImage $img Image resource or GdImage instance.
   478  * @param float    $x   Source point x-coordinate.
   496  * @param float            $x   Source point x-coordinate.
   479  * @param float    $y   Source point y-coordinate.
   497  * @param float            $y   Source point y-coordinate.
   480  * @param float    $w   Source width.
   498  * @param float            $w   Source width.
   481  * @param float    $h   Source height.
   499  * @param float            $h   Source height.
   482  * @return resource (maybe) cropped image resource.
   500  * @return resource|GdImage (maybe) cropped image resource or GdImage instance.
   483  */
   501  */
   484 function _crop_image_resource( $img, $x, $y, $w, $h ) {
   502 function _crop_image_resource( $img, $x, $y, $w, $h ) {
   485 	$dst = wp_imagecreatetruecolor( $w, $h );
   503 	$dst = wp_imagecreatetruecolor( $w, $h );
   486 	if ( is_resource( $dst ) ) {
   504 
       
   505 	if ( is_gd_image( $dst ) ) {
   487 		if ( imagecopy( $dst, $img, 0, 0, $x, $y, $w, $h ) ) {
   506 		if ( imagecopy( $dst, $img, 0, 0, $x, $y, $w, $h ) ) {
   488 			imagedestroy( $img );
   507 			imagedestroy( $img );
   489 			$img = $dst;
   508 			$img = $dst;
   490 		}
   509 		}
   491 	}
   510 	}
       
   511 
   492 	return $img;
   512 	return $img;
   493 }
   513 }
   494 
   514 
   495 /**
   515 /**
   496  * Performs group of changes on Editor specified.
   516  * Performs group of changes on Editor specified.
   500  * @param WP_Image_Editor $image   WP_Image_Editor instance.
   520  * @param WP_Image_Editor $image   WP_Image_Editor instance.
   501  * @param array           $changes Array of change operations.
   521  * @param array           $changes Array of change operations.
   502  * @return WP_Image_Editor WP_Image_Editor instance with changes applied.
   522  * @return WP_Image_Editor WP_Image_Editor instance with changes applied.
   503  */
   523  */
   504 function image_edit_apply_changes( $image, $changes ) {
   524 function image_edit_apply_changes( $image, $changes ) {
   505 	if ( is_resource( $image ) ) {
   525 	if ( is_gd_image( $image ) ) {
   506 		/* translators: 1: $image, 2: WP_Image_Editor */
   526 		/* translators: 1: $image, 2: WP_Image_Editor */
   507 		_deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( '%1$s needs to be a %2$s object.' ), '$image', 'WP_Image_Editor' ) );
   527 		_deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( '%1$s needs to be a %2$s object.' ), '$image', 'WP_Image_Editor' ) );
   508 	}
   528 	}
   509 
   529 
   510 	if ( ! is_array( $changes ) ) {
   530 	if ( ! is_array( $changes ) ) {
   564 		 *
   584 		 *
   565 		 * @param WP_Image_Editor $image   WP_Image_Editor instance.
   585 		 * @param WP_Image_Editor $image   WP_Image_Editor instance.
   566 		 * @param array           $changes Array of change operations.
   586 		 * @param array           $changes Array of change operations.
   567 		 */
   587 		 */
   568 		$image = apply_filters( 'wp_image_editor_before_change', $image, $changes );
   588 		$image = apply_filters( 'wp_image_editor_before_change', $image, $changes );
   569 	} elseif ( is_resource( $image ) ) {
   589 	} elseif ( is_gd_image( $image ) ) {
   570 
   590 
   571 		/**
   591 		/**
   572 		 * Filters the GD image resource before applying changes to the image.
   592 		 * Filters the GD image resource before applying changes to the image.
   573 		 *
   593 		 *
   574 		 * @since 2.9.0
   594 		 * @since 2.9.0
   575 		 * @deprecated 3.5.0 Use {@see 'wp_image_editor_before_change'} instead.
   595 		 * @deprecated 3.5.0 Use {@see 'wp_image_editor_before_change'} instead.
   576 		 *
   596 		 *
   577 		 * @param resource $image   GD image resource.
   597 		 * @param resource|GdImage $image   GD image resource or GdImage instance.
   578 		 * @param array    $changes Array of change operations.
   598 		 * @param array            $changes Array of change operations.
   579 		 */
   599 		 */
   580 		$image = apply_filters_deprecated( 'image_edit_before_change', array( $image, $changes ), '3.5.0', 'wp_image_editor_before_change' );
   600 		$image = apply_filters_deprecated( 'image_edit_before_change', array( $image, $changes ), '3.5.0', 'wp_image_editor_before_change' );
   581 	}
   601 	}
   582 
   602 
   583 	foreach ( $changes as $operation ) {
   603 	foreach ( $changes as $operation ) {
   776 	if ( is_wp_error( $img ) ) {
   796 	if ( is_wp_error( $img ) ) {
   777 		$return->error = esc_js( __( 'Unable to create new image.' ) );
   797 		$return->error = esc_js( __( 'Unable to create new image.' ) );
   778 		return $return;
   798 		return $return;
   779 	}
   799 	}
   780 
   800 
   781 	$fwidth  = ! empty( $_REQUEST['fwidth'] ) ? intval( $_REQUEST['fwidth'] ) : 0;
   801 	$fwidth  = ! empty( $_REQUEST['fwidth'] ) ? (int) $_REQUEST['fwidth'] : 0;
   782 	$fheight = ! empty( $_REQUEST['fheight'] ) ? intval( $_REQUEST['fheight'] ) : 0;
   802 	$fheight = ! empty( $_REQUEST['fheight'] ) ? (int) $_REQUEST['fheight'] : 0;
   783 	$target  = ! empty( $_REQUEST['target'] ) ? preg_replace( '/[^a-z0-9_-]+/i', '', $_REQUEST['target'] ) : '';
   803 	$target  = ! empty( $_REQUEST['target'] ) ? preg_replace( '/[^a-z0-9_-]+/i', '', $_REQUEST['target'] ) : '';
   784 	$scale   = ! empty( $_REQUEST['do'] ) && 'scale' === $_REQUEST['do'];
   804 	$scale   = ! empty( $_REQUEST['do'] ) && 'scale' === $_REQUEST['do'];
   785 
   805 
   786 	if ( $scale && $fwidth > 0 && $fheight > 0 ) {
   806 	if ( $scale && $fwidth > 0 && $fheight > 0 ) {
   787 		$size = $img->get_size();
   807 		$size = $img->get_size();
   934 					$backup_sizes[ $tag ] = $meta['sizes'][ $size ];
   954 					$backup_sizes[ $tag ] = $meta['sizes'][ $size ];
   935 				}
   955 				}
   936 			}
   956 			}
   937 
   957 
   938 			if ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
   958 			if ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
   939 				$width  = intval( $_wp_additional_image_sizes[ $size ]['width'] );
   959 				$width  = (int) $_wp_additional_image_sizes[ $size ]['width'];
   940 				$height = intval( $_wp_additional_image_sizes[ $size ]['height'] );
   960 				$height = (int) $_wp_additional_image_sizes[ $size ]['height'];
   941 				$crop   = ( $nocrop ) ? false : $_wp_additional_image_sizes[ $size ]['crop'];
   961 				$crop   = ( $nocrop ) ? false : $_wp_additional_image_sizes[ $size ]['crop'];
   942 			} else {
   962 			} else {
   943 				$height = get_option( "{$size}_size_h" );
   963 				$height = get_option( "{$size}_size_h" );
   944 				$width  = get_option( "{$size}_size_w" );
   964 				$width  = get_option( "{$size}_size_w" );
   945 				$crop   = ( $nocrop ) ? false : get_option( "{$size}_crop" );
   965 				$crop   = ( $nocrop ) ? false : get_option( "{$size}_crop" );