-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Create sub-sized images #74566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create sub-sized images #74566
Changes from all commits
4d2db5f
db2d0c0
9bf2ee1
610f25f
aed40f2
98c737c
f2014d3
42077a5
0903e04
a5781a8
fa7e267
740512c
4f59396
6caeab6
9f61d8f
cd16ae1
bfda0f7
37b776d
fc7cd3e
6f7ca75
382b02e
bfbc620
a1bb637
e155638
989324f
75a94ec
8dac9ad
4d93fa8
9f70bdf
4ed0b80
b7db54b
d436829
362560b
563a024
d3e939e
d308469
8c132a6
44f71b9
224c585
b850e62
f0c6cfa
a37e246
6ac7a29
65e5893
4a2ad32
33416b3
6bb8387
adb6e5c
795dfb7
892e4c3
1a0c8b6
a253703
f4fb34f
d8af45f
3ce45ac
132f54c
2c6e0f7
029fb95
ee789d5
d179aa4
91a509c
35def30
4a4b9ca
31eab03
5cc1bda
46f4640
3a43770
86ece0c
f768f9a
ed2b000
dfdf08c
35475dd
ccd49b3
fceec95
1cd5b9c
e6b53da
d3fa5f3
df87386
ba78a70
db4bd7e
b252589
f42a103
2c2c8db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,10 +78,31 @@ public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CRE | |
| return $args; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the attachment's schema, conforming to JSON Schema. | ||
| * | ||
| * Adds exif_orientation field to the schema. | ||
| * | ||
| * @return array Item schema data. | ||
| */ | ||
| public function get_item_schema() { | ||
| $schema = parent::get_item_schema(); | ||
|
|
||
| $schema['properties']['exif_orientation'] = array( | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we pass exit orientation back after uploading the original image, keeping the logic in one place (server side) for now. We can layer on client side support for image formats the server doesn't support in a later iteration. |
||
| 'description' => __( 'EXIF orientation value from the original image. Values 1-8 follow the EXIF specification. A value other than 1 indicates the image needs rotation.', 'gutenberg' ), | ||
| 'type' => 'integer', | ||
| 'context' => array( 'edit' ), | ||
| 'readonly' => true, | ||
| ); | ||
|
|
||
| return $schema; | ||
| } | ||
|
|
||
| /** | ||
| * Prepares a single attachment output for response. | ||
| * | ||
| * Ensures 'missing_image_sizes' is set for PDFs and not just images. | ||
| * Adds 'exif_orientation' for images that need client-side rotation. | ||
| * | ||
| * @param WP_Post $item Attachment object. | ||
| * @param WP_REST_Request $request Request object. | ||
|
|
@@ -92,10 +113,27 @@ public function prepare_item_for_response( $item, $request ): WP_REST_Response { | |
|
|
||
| $data = $response->get_data(); | ||
|
|
||
| // Handle missing image sizes for PDFs. | ||
|
|
||
| $fields = $this->get_fields_for_response( $request ); | ||
|
|
||
| // Add EXIF orientation for images. | ||
| if ( rest_is_field_included( 'exif_orientation', $fields ) ) { | ||
| if ( wp_attachment_is_image( $item ) ) { | ||
| $metadata = wp_get_attachment_metadata( $item->ID, true ); | ||
|
|
||
| // Get the EXIF orientation from the image metadata. | ||
| // This is stored by wp_read_image_metadata() during upload. | ||
| $orientation = 1; // Default: no rotation needed. | ||
| if ( | ||
| is_array( $metadata ) && | ||
| isset( $metadata['image_meta']['orientation'] ) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. core stores the |
||
| ) { | ||
| $orientation = (int) $metadata['image_meta']['orientation']; | ||
| } | ||
|
|
||
| $data['exif_orientation'] = $orientation; | ||
| } | ||
| } | ||
|
|
||
| if ( | ||
| rest_is_field_included( 'missing_image_sizes', $fields ) && | ||
| empty( $data['missing_image_sizes'] ) | ||
|
|
@@ -158,7 +196,9 @@ public function create_item( $request ) { | |
| if ( ! $request['generate_sub_sizes'] ) { | ||
| add_filter( 'intermediate_image_sizes_advanced', '__return_empty_array', 100 ); | ||
| add_filter( 'fallback_intermediate_image_sizes', '__return_empty_array', 100 ); | ||
|
|
||
| // Disable server-side EXIF rotation so the client can handle it. | ||
| // This preserves the original orientation value in the metadata. | ||
| add_filter( 'wp_image_maybe_exif_rotate', '__return_false', 100 ); | ||
| } | ||
|
|
||
| if ( ! $request['convert_format'] ) { | ||
|
|
@@ -169,6 +209,7 @@ public function create_item( $request ) { | |
|
|
||
| remove_filter( 'intermediate_image_sizes_advanced', '__return_empty_array', 100 ); | ||
| remove_filter( 'fallback_intermediate_image_sizes', '__return_empty_array', 100 ); | ||
| remove_filter( 'wp_image_maybe_exif_rotate', '__return_false', 100 ); | ||
| remove_filter( 'image_editor_output_format', '__return_empty_array', 100 ); | ||
|
|
||
| return $response; | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.