Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions includes/Classifai/Features/ImageCropping.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function feature_setup() {
add_action( 'edit_attachment', [ $this, 'maybe_crop_image' ] );

add_filter( 'attachment_fields_to_edit', [ $this, 'add_rescan_button_to_media_modal' ], 10, 2 );
add_filter( 'wp_prepare_attachment_for_js', [ $this, 'set_media_library_attachment_size' ], 10, 1 );
add_filter( 'wp_generate_attachment_metadata', [ $this, 'generate_smart_crops' ], 7, 2 );
}

Expand Down Expand Up @@ -163,6 +164,28 @@ public function generate_smart_crops( array $metadata, int $attachment_id ): arr
return $metadata;
}

/**
* Adjust the media library attachment size to account for cropped thumbnails.
*
* @param array $response Array of prepared attachment data.
* @return array
*/
public function set_media_library_attachment_size( $response ) {
if ( ! isset( $response['type'] ) || 'image' !== $response['type'] ) {
return $response;
}

/*
* Replace the `medium` thumbnail with the original `thumbnail` size, which has been
* smart cropped to show the actual cropped image on the media library thumbnail.
*/
if ( isset( $response['sizes']['thumbnail'] ) && isset( $response['sizes']['medium'] ) ) {
$response['sizes']['medium'] = $response['sizes']['thumbnail'];
}

return $response;
}

/**
* Save the cropped images.
*
Expand Down