490 | | if ( is_wp_error( $result ) ) { |
491 | | return new WP_Error( |
492 | | 'rest_image_rotation_failed', |
493 | | __( 'Unable to rotate this image.' ), |
494 | | array( 'status' => 500 ) |
495 | | ); |
496 | | } |
497 | | } |
498 | | |
499 | | if ( $crop ) { |
500 | | $size = $image_editor->get_size(); |
501 | | |
502 | | $crop_x = round( ( $size['width'] * floatval( $request['x'] ) ) / 100.0 ); |
503 | | $crop_y = round( ( $size['height'] * floatval( $request['y'] ) ) / 100.0 ); |
504 | | $width = round( ( $size['width'] * floatval( $request['width'] ) ) / 100.0 ); |
505 | | $height = round( ( $size['height'] * floatval( $request['height'] ) ) / 100.0 ); |
506 | | |
507 | | $result = $image_editor->crop( $crop_x, $crop_y, $width, $height ); |
508 | | |
509 | | if ( is_wp_error( $result ) ) { |
510 | | return new WP_Error( |
511 | | 'rest_image_crop_failed', |
512 | | __( 'Unable to crop this image.' ), |
513 | | array( 'status' => 500 ) |
514 | | ); |
515 | | } |
516 | | } |
517 | | |
| 549 | if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { |
| 550 | // Set a custom header with the attachment_id. |
| 551 | // Used by the browser/client to resume creating image sub-sizes after a PHP fatal error. |
| 552 | header( 'X-WP-Upload-Attachment-ID: ' . $new_attachment_id ); |
| 553 | } |
| 554 | |
| 555 | // Edit the image. |
| 556 | if ( 0 !== $rotate ) { |
| 557 | $result = $image_editor->rotate( $rotate ); |
| 558 | |
| 559 | if ( is_wp_error( $result ) ) { |
| 560 | return new WP_Error( |
| 561 | 'rest_image_rotation_failed', |
| 562 | __( 'Unable to rotate this image.' ), |
| 563 | array( 'status' => 500 ) |
| 564 | ); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | if ( $crop ) { |
| 569 | $size = $image_editor->get_size(); |
| 570 | |
| 571 | $crop_x = round( ( $size['width'] * floatval( $request['x'] ) ) / 100.0 ); |
| 572 | $crop_y = round( ( $size['height'] * floatval( $request['y'] ) ) / 100.0 ); |
| 573 | $width = round( ( $size['width'] * floatval( $request['width'] ) ) / 100.0 ); |
| 574 | $height = round( ( $size['height'] * floatval( $request['height'] ) ) / 100.0 ); |
| 575 | |
| 576 | $result = $image_editor->crop( $crop_x, $crop_y, $width, $height ); |
| 577 | |
| 578 | if ( is_wp_error( $result ) ) { |
| 579 | return new WP_Error( |
| 580 | 'rest_image_crop_failed', |
| 581 | __( 'Unable to crop this image.' ), |
| 582 | array( 'status' => 500 ) |
| 583 | ); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | // Save to disk. |
| 588 | $saved = $image_editor->save( $uploads['path'] . "/$filename" ); |
| 589 | |
| 590 | if ( is_wp_error( $saved ) ) { |
| 591 | return $saved; |
| 592 | } |
| 593 | |
| 594 | // Ensure the new attachment mime type matches the image mime type as returned by $image_editor->save(). |
| 595 | if ( $mime_type !== $saved['mime-type'] ) { |
| 596 | wp_update_post( |
| 597 | array( |
| 598 | 'ID' => $new_attachment_id, |
| 599 | 'post_mime_type' => $saved['mime-type'], |
| 600 | ) |
| 601 | ); |
| 602 | } |
| 603 | |