Skip to content

Commit 85bcd9d

Browse files
committed
Fix the smart cropping and descriptive text CLI commands
1 parent 33ef6a2 commit 85bcd9d

File tree

1 file changed

+18
-31
lines changed

1 file changed

+18
-31
lines changed

includes/Classifai/Command/ClassifaiCommand.php

+18-31
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
use Classifai\Providers\Watson\Classifier;
1313
use Classifai\Normalizer;
1414
use Classifai\Providers\Watson\PostClassifier;
15-
use Classifai\Providers\Azure\ComputerVision;
16-
use Classifai\Providers\Azure\SmartCropping;
1715
use Classifai\Providers\OpenAI\Embeddings;
1816

1917
use function Classifai\Providers\Watson\get_username;
@@ -804,21 +802,26 @@ public function image( $args = [], $opts = [] ) {
804802
$message = "Classifying $limit_total images ...";
805803

806804
$progress_bar = \WP_CLI\Utils\make_progress_bar( $message, $limit_total );
805+
$text_feature = new DescriptiveTextGenerator();
806+
$crop_feature = new ImageCropping();
807807

808808
for ( $index = 0; $index < $limit_total; $index++ ) {
809809
$attachment_id = $attachment_ids[ $index ];
810810

811811
$progress_bar->tick();
812812

813-
$current_meta = wp_get_attachment_metadata( $attachment_id );
814813
\WP_CLI::line( 'Processing ' . $attachment_id );
815-
$feature = new DescriptiveTextGenerator();
816-
$result = $feature->run( $attachment_id, 'descriptive_text' );
817-
$feature->save( $result, $attachment_id );
818814

819-
$feature = new ImageCropping();
820-
$result = $feature->run( $attachment_id, 'crop' );
821-
$feature->save( $result, $attachment_id );
815+
$text_result = $text_feature->run( $attachment_id, 'descriptive_text' );
816+
if ( $text_result && ! is_wp_error( $text_result ) ) {
817+
$text_feature->save( $text_result, $attachment_id );
818+
}
819+
820+
$crop_result = $crop_feature->run( $attachment_id, 'crop' );
821+
if ( ! empty( $crop_result ) && ! is_wp_error( $crop_result ) ) {
822+
$meta = $crop_feature->save( $crop_result, $attachment_id );
823+
wp_update_attachment_metadata( $attachment_id, $meta );
824+
}
822825
}
823826

824827
$progress_bar->finish();
@@ -888,28 +891,12 @@ public function crop( $args = [], $opts = [] ) {
888891

889892
$progress_bar->tick();
890893

891-
$current_meta = wp_get_attachment_metadata( $attachment_id );
892-
893-
foreach ( $current_meta['sizes'] as $size => $size_data ) {
894-
switch ( $provider_class::ID ) {
895-
case ComputerVision::ID:
896-
$smart_cropping = new SmartCropping( $settings );
897-
898-
if ( ! $smart_cropping->should_crop( $size ) ) {
899-
break;
900-
}
901-
902-
$data = [
903-
'width' => $size_data['width'],
904-
'height' => $size_data['height'],
905-
];
906-
907-
$smart_thumbnail = $smart_cropping->get_cropped_thumbnail( $attachment_id, $data );
908-
if ( is_wp_error( $smart_thumbnail ) ) {
909-
$errors[ $attachment_id . ':' . $size_data['width'] . 'x' . $size_data['height'] ] = $smart_thumbnail;
910-
}
911-
break;
912-
}
894+
$crop_result = $image_cropping->run( $attachment_id, 'crop' );
895+
if ( ! empty( $crop_result ) && ! is_wp_error( $crop_result ) ) {
896+
$meta = $image_cropping->save( $crop_result, $attachment_id );
897+
wp_update_attachment_metadata( $attachment_id, $meta );
898+
} else {
899+
$errors[ $attachment_id ] = $crop_result;
913900
}
914901
}
915902

0 commit comments

Comments
 (0)