mirror of
https://github.com/WordPress/WordPress.git
synced 2024-12-23 09:37:42 +01:00
REST API, Media: Add batch image editing endpoints.
Introduces new endpoints to allow for batch image editing using the REST API. The new endpoints can take an array of modifiers that will be applied in the order they appear. Props ajlende, TimothyBlynJacobs, hellofromTonya, Mista-Flo. Fixes #52192. Built from https://develop.svn.wordpress.org/trunk@50124 git-svn-id: http://core.svn.wordpress.org/trunk@49803 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
cb7d0efb45
commit
3e6cc64e35
@ -448,26 +448,41 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if we need to do anything.
|
// The `modifiers` param takes precedence over the older format.
|
||||||
$rotate = 0;
|
if ( isset( $request['modifiers'] ) ) {
|
||||||
$crop = false;
|
$modifiers = $request['modifiers'];
|
||||||
|
} else {
|
||||||
|
$modifiers = array();
|
||||||
|
|
||||||
if ( ! empty( $request['rotation'] ) ) {
|
if ( ! empty( $request['rotation'] ) ) {
|
||||||
// Rotation direction: clockwise vs. counter clockwise.
|
$modifiers[] = array(
|
||||||
$rotate = 0 - (int) $request['rotation'];
|
'type' => 'rotate',
|
||||||
|
'args' => array(
|
||||||
|
'angle' => $request['rotation'],
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( isset( $request['x'], $request['y'], $request['width'], $request['height'] ) ) {
|
if ( isset( $request['x'], $request['y'], $request['width'], $request['height'] ) ) {
|
||||||
$crop = true;
|
$modifiers[] = array(
|
||||||
|
'type' => 'crop',
|
||||||
|
'args' => array(
|
||||||
|
'left' => $request['x'],
|
||||||
|
'top' => $request['y'],
|
||||||
|
'width' => $request['width'],
|
||||||
|
'height' => $request['height'],
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! $rotate && ! $crop ) {
|
if ( 0 === count( $modifiers ) ) {
|
||||||
return new WP_Error(
|
return new WP_Error(
|
||||||
'rest_image_not_edited',
|
'rest_image_not_edited',
|
||||||
__( 'The image was not edited. Edit the image before applying the changes.' ),
|
__( 'The image was not edited. Edit the image before applying the changes.' ),
|
||||||
array( 'status' => 400 )
|
array( 'status' => 400 )
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If the file doesn't exist, attempt a URL fopen on the src link.
|
* If the file doesn't exist, attempt a URL fopen on the src link.
|
||||||
@ -489,6 +504,13 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach ( $modifiers as $modifier ) {
|
||||||
|
$args = $modifier['args'];
|
||||||
|
switch ( $modifier['type'] ) {
|
||||||
|
case 'rotate':
|
||||||
|
// Rotation direction: clockwise vs. counter clockwise.
|
||||||
|
$rotate = 0 - $args['angle'];
|
||||||
|
|
||||||
if ( 0 !== $rotate ) {
|
if ( 0 !== $rotate ) {
|
||||||
$result = $image_editor->rotate( $rotate );
|
$result = $image_editor->rotate( $rotate );
|
||||||
|
|
||||||
@ -501,14 +523,17 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $crop ) {
|
break;
|
||||||
|
|
||||||
|
case 'crop':
|
||||||
$size = $image_editor->get_size();
|
$size = $image_editor->get_size();
|
||||||
|
|
||||||
$crop_x = round( ( $size['width'] * (float) $request['x'] ) / 100.0 );
|
$crop_x = round( ( $size['width'] * $args['left'] ) / 100.0 );
|
||||||
$crop_y = round( ( $size['height'] * (float) $request['y'] ) / 100.0 );
|
$crop_y = round( ( $size['height'] * $args['top'] ) / 100.0 );
|
||||||
$width = round( ( $size['width'] * (float) $request['width'] ) / 100.0 );
|
$width = round( ( $size['width'] * $args['width'] ) / 100.0 );
|
||||||
$height = round( ( $size['height'] * (float) $request['height'] ) / 100.0 );
|
$height = round( ( $size['height'] * $args['height'] ) / 100.0 );
|
||||||
|
|
||||||
|
if ( $size['width'] !== $width && $size['height'] !== $height ) {
|
||||||
$result = $image_editor->crop( $crop_x, $crop_y, $width, $height );
|
$result = $image_editor->crop( $crop_x, $crop_y, $width, $height );
|
||||||
|
|
||||||
if ( is_wp_error( $result ) ) {
|
if ( is_wp_error( $result ) ) {
|
||||||
@ -520,6 +545,11 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Calculate the file name.
|
// Calculate the file name.
|
||||||
$image_ext = pathinfo( $image_file, PATHINFO_EXTENSION );
|
$image_ext = pathinfo( $image_file, PATHINFO_EXTENSION );
|
||||||
$image_name = wp_basename( $image_file, ".{$image_ext}" );
|
$image_name = wp_basename( $image_file, ".{$image_ext}" );
|
||||||
@ -1286,8 +1316,90 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
|
|||||||
*/
|
*/
|
||||||
protected function get_edit_media_item_args() {
|
protected function get_edit_media_item_args() {
|
||||||
return array(
|
return array(
|
||||||
|
'src' => array(
|
||||||
|
'description' => __( 'URL to the edited image file.' ),
|
||||||
|
'type' => 'string',
|
||||||
|
'format' => 'uri',
|
||||||
|
'required' => true,
|
||||||
|
),
|
||||||
|
'modifiers' => array(
|
||||||
|
'description' => __( 'Array of image edits.' ),
|
||||||
|
'type' => 'array',
|
||||||
|
'minItems' => 1,
|
||||||
|
'items' => array(
|
||||||
|
'description' => __( 'Image edit.' ),
|
||||||
|
'type' => 'object',
|
||||||
|
'required' => array(
|
||||||
|
'type',
|
||||||
|
'args',
|
||||||
|
),
|
||||||
|
'oneOf' => array(
|
||||||
|
array(
|
||||||
|
'title' => __( 'Rotation' ),
|
||||||
|
'properties' => array(
|
||||||
|
'type' => array(
|
||||||
|
'description' => __( 'Rotation type.' ),
|
||||||
|
'type' => 'string',
|
||||||
|
'enum' => array( 'rotate' ),
|
||||||
|
),
|
||||||
|
'args' => array(
|
||||||
|
'description' => __( 'Rotation arguments.' ),
|
||||||
|
'type' => 'object',
|
||||||
|
'required' => array(
|
||||||
|
'angle',
|
||||||
|
),
|
||||||
|
'properties' => array(
|
||||||
|
'angle' => array(
|
||||||
|
'description' => __( 'Angle to rotate clockwise in degrees.' ),
|
||||||
|
'type' => 'number',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'title' => __( 'Crop' ),
|
||||||
|
'properties' => array(
|
||||||
|
'type' => array(
|
||||||
|
'description' => __( 'Crop type.' ),
|
||||||
|
'type' => 'string',
|
||||||
|
'enum' => array( 'crop' ),
|
||||||
|
),
|
||||||
|
'args' => array(
|
||||||
|
'description' => __( 'Crop arguments.' ),
|
||||||
|
'type' => 'object',
|
||||||
|
'required' => array(
|
||||||
|
'left',
|
||||||
|
'top',
|
||||||
|
'width',
|
||||||
|
'height',
|
||||||
|
),
|
||||||
|
'properties' => array(
|
||||||
|
'left' => array(
|
||||||
|
'description' => __( 'Horizontal position from the left to begin the crop as a percentage of the image width.' ),
|
||||||
|
'type' => 'number',
|
||||||
|
),
|
||||||
|
'top' => array(
|
||||||
|
'description' => __( 'Vertical position from the top to begin the crop as a percentage of the image height.' ),
|
||||||
|
'type' => 'number',
|
||||||
|
),
|
||||||
|
'width' => array(
|
||||||
|
'description' => __( 'Width of the crop as a percentage of the image width.' ),
|
||||||
|
'type' => 'number',
|
||||||
|
),
|
||||||
|
'height' => array(
|
||||||
|
'description' => __( 'Height of the crop as a percentage of the image height.' ),
|
||||||
|
'type' => 'number',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
'rotation' => array(
|
'rotation' => array(
|
||||||
'description' => __( 'The amount to rotate the image clockwise in degrees.' ),
|
'description' => __( 'The amount to rotate the image clockwise in degrees. DEPRECATED: Use `modifiers` instead.' ),
|
||||||
'type' => 'integer',
|
'type' => 'integer',
|
||||||
'minimum' => 0,
|
'minimum' => 0,
|
||||||
'exclusiveMinimum' => true,
|
'exclusiveMinimum' => true,
|
||||||
@ -1295,35 +1407,29 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
|
|||||||
'exclusiveMaximum' => true,
|
'exclusiveMaximum' => true,
|
||||||
),
|
),
|
||||||
'x' => array(
|
'x' => array(
|
||||||
'description' => __( 'As a percentage of the image, the x position to start the crop from.' ),
|
'description' => __( 'As a percentage of the image, the x position to start the crop from. DEPRECATED: Use `modifiers` instead.' ),
|
||||||
'type' => 'number',
|
'type' => 'number',
|
||||||
'minimum' => 0,
|
'minimum' => 0,
|
||||||
'maximum' => 100,
|
'maximum' => 100,
|
||||||
),
|
),
|
||||||
'y' => array(
|
'y' => array(
|
||||||
'description' => __( 'As a percentage of the image, the y position to start the crop from.' ),
|
'description' => __( 'As a percentage of the image, the y position to start the crop from. DEPRECATED: Use `modifiers` instead.' ),
|
||||||
'type' => 'number',
|
'type' => 'number',
|
||||||
'minimum' => 0,
|
'minimum' => 0,
|
||||||
'maximum' => 100,
|
'maximum' => 100,
|
||||||
),
|
),
|
||||||
'width' => array(
|
'width' => array(
|
||||||
'description' => __( 'As a percentage of the image, the width to crop the image to.' ),
|
'description' => __( 'As a percentage of the image, the width to crop the image to. DEPRECATED: Use `modifiers` instead.' ),
|
||||||
'type' => 'number',
|
'type' => 'number',
|
||||||
'minimum' => 0,
|
'minimum' => 0,
|
||||||
'maximum' => 100,
|
'maximum' => 100,
|
||||||
),
|
),
|
||||||
'height' => array(
|
'height' => array(
|
||||||
'description' => __( 'As a percentage of the image, the height to crop the image to.' ),
|
'description' => __( 'As a percentage of the image, the height to crop the image to. DEPRECATED: Use `modifiers` instead.' ),
|
||||||
'type' => 'number',
|
'type' => 'number',
|
||||||
'minimum' => 0,
|
'minimum' => 0,
|
||||||
'maximum' => 100,
|
'maximum' => 100,
|
||||||
),
|
),
|
||||||
'src' => array(
|
|
||||||
'description' => __( 'URL to the edited image file.' ),
|
|
||||||
'type' => 'string',
|
|
||||||
'format' => 'uri',
|
|
||||||
'required' => true,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '5.7-alpha-50123';
|
$wp_version = '5.7-alpha-50124';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
Loading…
Reference in New Issue
Block a user