|
12 | 12 | use Classifai\Providers\Watson\Classifier;
|
13 | 13 | use Classifai\Normalizer;
|
14 | 14 | use Classifai\Providers\Watson\PostClassifier;
|
15 |
| -use Classifai\Providers\Azure\ComputerVision; |
16 |
| -use Classifai\Providers\Azure\SmartCropping; |
17 | 15 | use Classifai\Providers\OpenAI\Embeddings;
|
18 | 16 |
|
19 | 17 | use function Classifai\Providers\Watson\get_username;
|
@@ -804,21 +802,26 @@ public function image( $args = [], $opts = [] ) {
|
804 | 802 | $message = "Classifying $limit_total images ...";
|
805 | 803 |
|
806 | 804 | $progress_bar = \WP_CLI\Utils\make_progress_bar( $message, $limit_total );
|
| 805 | + $text_feature = new DescriptiveTextGenerator(); |
| 806 | + $crop_feature = new ImageCropping(); |
807 | 807 |
|
808 | 808 | for ( $index = 0; $index < $limit_total; $index++ ) {
|
809 | 809 | $attachment_id = $attachment_ids[ $index ];
|
810 | 810 |
|
811 | 811 | $progress_bar->tick();
|
812 | 812 |
|
813 |
| - $current_meta = wp_get_attachment_metadata( $attachment_id ); |
814 | 813 | \WP_CLI::line( 'Processing ' . $attachment_id );
|
815 |
| - $feature = new DescriptiveTextGenerator(); |
816 |
| - $result = $feature->run( $attachment_id, 'descriptive_text' ); |
817 |
| - $feature->save( $result, $attachment_id ); |
818 | 814 |
|
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 | + } |
822 | 825 | }
|
823 | 826 |
|
824 | 827 | $progress_bar->finish();
|
@@ -888,28 +891,12 @@ public function crop( $args = [], $opts = [] ) {
|
888 | 891 |
|
889 | 892 | $progress_bar->tick();
|
890 | 893 |
|
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; |
913 | 900 | }
|
914 | 901 | }
|
915 | 902 |
|
|
0 commit comments