Skip to content

Commit

Permalink
Only process if an image matches the supported mime types. Get the la…
Browse files Browse the repository at this point in the history
…rgest image based on filesize and dimensions. Remove the OCR class as it is no longer needed
  • Loading branch information
dkotter committed Nov 22, 2024
1 parent 5f65b93 commit 5d87ad6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 330 deletions.
47 changes: 40 additions & 7 deletions includes/Classifai/Providers/Azure/ComputerVision.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use WP_Error;

use function Classifai\computer_vision_max_filesize;
use function Classifai\get_largest_acceptable_image_url;
use function Classifai\get_largest_size_and_dimensions_image_url;
use function Classifai\get_modified_image_source_url;

class ComputerVision extends Provider {
Expand All @@ -27,6 +27,22 @@ class ComputerVision extends Provider {
*/
protected $analyze_url = 'computervision/imageanalysis:analyze?api-version=2024-02-01';

/**
* Image types to process.
*
* @var array
*/
private $image_types_to_process = [
'bmp',
'gif',
'jpeg',
'png',
'webp',
'ico',
'tiff',
'mpo',
];

/**
* ComputerVision constructor.
*
Expand Down Expand Up @@ -777,19 +793,36 @@ public function rest_endpoint_callback( $attachment_id, string $route_to_call =
return new WP_Error( 'invalid', esc_html__( 'No valid metadata found.', 'classifai' ) );
}

switch ( $route_to_call ) {
case 'crop':
return $this->smart_crop_image( $metadata, $attachment_id );
if ( 'crop' === $route_to_call ) {
return $this->smart_crop_image( $metadata, $attachment_id );
}

// Check if the image is of a type we can process.
$mime_type = get_post_mime_type( $attachment_id );
$matched_extensions = explode( '|', array_search( $mime_type, wp_get_mime_types(), true ) );
$process = false;

foreach ( $matched_extensions as $ext ) {
if ( in_array( $ext, $this->image_types_to_process, true ) ) {
$process = true;
break;
}
}

if ( ! $process ) {
return new WP_Error( 'invalid', esc_html__( 'Image does not match a valid mime type.', 'classifai' ) );
}

$image_url = get_modified_image_source_url( $attachment_id );

if ( empty( $image_url ) || ! filter_var( $image_url, FILTER_VALIDATE_URL ) ) {
if ( isset( $metadata['sizes'] ) && is_array( $metadata['sizes'] ) ) {
$image_url = get_largest_acceptable_image_url(
$image_url = get_largest_size_and_dimensions_image_url(
get_attached_file( $attachment_id ),
wp_get_attachment_url( $attachment_id ),
$metadata['sizes'],
$metadata,
[ 50, 16000 ],
[ 50, 16000 ],
computer_vision_max_filesize()
);
} else {
Expand All @@ -798,7 +831,7 @@ public function rest_endpoint_callback( $attachment_id, string $route_to_call =
}

if ( empty( $image_url ) ) {
return new WP_Error( 'error', esc_html__( 'Valid image size not found. Make sure the image is less than 4MB.', 'classifai' ) );
return new WP_Error( 'error', esc_html__( 'Image does not meet size requirements. Please ensure it is at least 50x50 but less than 16000x16000 and smaller than 20MB.', 'classifai' ) );
}

switch ( $route_to_call ) {
Expand Down
Loading

0 comments on commit 5d87ad6

Please sign in to comment.