-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Computer Vision - send next largest image when full is too big #110
Merged
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ce5cbb1
Merge branch 'develop' of https://github.com/10up/classifai into develop
johnwatkins0 1702e86
ComputerVision - send next largest image when full is too big
johnwatkins0 6c44a8c
Computer Vision - in file size comparisons, use >= instead of >
johnwatkins0 0cc1b7b
Fix bug preventing second-largest intermediate image URL from generating
johnwatkins0 34edd17
Fix failing unit tests
62b73d3
Merge branch 'develop' of https://github.com/10up/classifai into develop
johnwatkins0 4c16931
Merge branch 'develop' into feature/handle-overlarge-images
johnwatkins0 9c6238a
Merge branch 'develop' into feature/handle-overlarge-images
0092483
Merge branch 'develop' of https://github.com/10up/classifai into develop
johnwatkins0 404d084
Merge branch 'develop' into feature/handle-overlarge-images
johnwatkins0 88282cd
Merge branch 'feature/handle-overlarge-images' of https://github.com/…
johnwatkins0 0db2581
Check that image metadata sizes are set before using
johnwatkins0 4baf874
Use MB_IN_BYTES constant instead of hardcoded number
johnwatkins0 7b7b339
Merge branch 'develop' into feature/handle-overlarge-images
helen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,7 +60,23 @@ public function register() { | |
|
||
|
||
/** | ||
* Process the image via Computer Vision based on the settings. | ||
* Provides the max filesize for the ComputerVision service. | ||
* | ||
* @return int | ||
* | ||
* @since 1.4.0 | ||
*/ | ||
public function get_max_filesize() { | ||
/** | ||
* Filters the ComputerVision maximum allowed filesize. | ||
* | ||
* @param int Default 4MB. | ||
*/ | ||
return apply_filters( 'classifai_computervision_max_filesize', 4000000 ); // 4MB default. | ||
} | ||
|
||
/** | ||
* Generate the alt tags for the image being uploaded. | ||
* | ||
* @param array $metadata The metadata for the image. | ||
* @param int $attachment_id Post ID for the attachment. | ||
|
@@ -74,25 +90,74 @@ public function process_image( $metadata, $attachment_id ) { | |
'no' !== $settings['enable_image_tagging'] || | ||
'no' !== $settings['enable_image_captions'] | ||
) { | ||
$image_url = wp_get_attachment_image_url( $attachment_id ); | ||
$image_scan = $this->scan_image( $image_url ); | ||
if ( ! is_wp_error( $image_scan ) ) { | ||
// Check for captions | ||
if ( isset( $image_scan->description->captions ) ) { | ||
// Process the captions | ||
$this->generate_alt_tags( $image_scan->description->captions, $attachment_id ); | ||
} | ||
// Check for tags | ||
if ( isset( $image_scan->tags ) ) { | ||
// Process the tags | ||
$this->generate_image_tags( $image_scan->tags, $attachment_id ); | ||
$image_url = $this->get_largest_acceptable_image_url( | ||
get_attached_file( $attachment_id ), | ||
wp_get_attachment_url( $attachment_id, 'full' ), | ||
$metadata['sizes'] | ||
); | ||
|
||
if ( ! empty( $image_url ) ) { | ||
$image_scan = $this->scan_image( $image_url ); | ||
if ( ! is_wp_error( $image_scan ) ) { | ||
// Check for captions | ||
if ( isset( $image_scan->description->captions ) ) { | ||
// Process the captions | ||
$this->generate_alt_tags( $image_scan->description->captions, $attachment_id ); | ||
} | ||
// Check for tags | ||
if ( isset( $image_scan->tags ) ) { | ||
// Process the tags | ||
$this->generate_image_tags( $image_scan->tags, $attachment_id ); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $metadata; | ||
} | ||
|
||
/** | ||
* Retrieves the URL of the largest version of an attachment image accepted by the ComputerVision service. | ||
* | ||
* @param string $full_image The path to the full-sized image source file. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please keep these |
||
* @param string $full_url The URL of the full-sized image. | ||
* @param array $sizes Intermediate size data from attachment meta. | ||
* @return string|null The image URL, or null if no acceptable image found. | ||
* | ||
* @since 1.4.0 | ||
*/ | ||
public function get_largest_acceptable_image_url( $full_image, $full_url, $sizes ) { | ||
$file_size = @filesize( $full_image ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged | ||
if ( $file_size && $this->get_max_filesize() >= $file_size ) { | ||
return $full_url; | ||
} | ||
|
||
// Sort the image sizes in order of total width + height, descending. | ||
helen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$sort_sizes = function( $size_1, $size_2 ) { | ||
$size_1_total = $size_1['width'] + $size_1['height']; | ||
$size_2_total = $size_2['width'] + $size_2['height']; | ||
|
||
if ( $size_1_total === $size_2_total ) { | ||
return 0; | ||
} | ||
|
||
return $size_1_total > $size_2_total ? -1 : 1; | ||
}; | ||
|
||
usort( $sizes, $sort_sizes ); | ||
|
||
foreach ( $sizes as $size ) { | ||
$sized_file = str_replace( basename( $full_image ), $size['file'], $full_image ); | ||
$file_size = @filesize( $sized_file ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged | ||
|
||
if ( $file_size && $this->get_max_filesize() >= $file_size ) { | ||
return str_replace( basename( $full_url ), $size['file'], $full_url ); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Scan the image and return the captions. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
/** | ||
* Testing for the ComputerVisition class | ||
*/ | ||
|
||
namespace Classifai\Tests\Providers\Azure; | ||
|
||
use \WP_UnitTestCase; | ||
use Classifai\Providers\Azure\ComputerVision; | ||
|
||
/** | ||
* Class ComputerVisionTest | ||
* @package Classifai\Tests\Providers\Azure; | ||
* | ||
* @group azure | ||
*/ | ||
class ComputerVisionTest extends WP_UnitTestCase { | ||
protected $computer_vision; | ||
|
||
/** | ||
* Setup method. | ||
*/ | ||
public function setUp() { | ||
parent::setUp(); | ||
|
||
$this->computer_vision = new ComputerVision( 'my_service' ); | ||
} | ||
|
||
public function tearDown() { | ||
parent::tearDown(); | ||
|
||
$this->remove_added_uploads(); | ||
} | ||
|
||
/** | ||
* Tests the get_largest_acceptable_image_url method. | ||
*/ | ||
public function test_get_largest_acceptable_image_url() { | ||
$attachment = $this->factory->attachment->create_upload_object( DIR_TESTDATA .'/images/33772.jpg' ); // ~172KB image. | ||
|
||
$set_150kb_max_filesize = function() { | ||
return 150000; | ||
}; | ||
add_filter( 'classifai_computervision_max_filesize', $set_150kb_max_filesize ); | ||
|
||
$url = $this->computer_vision->get_largest_acceptable_image_url( | ||
get_attached_file( $attachment ), | ||
wp_get_attachment_url( $attachment, 'full' ), | ||
wp_get_attachment_metadata( $attachment )['sizes'] | ||
); | ||
$this->assertEquals( sprintf( 'http://example.org/wp-content/uploads/%s/%s/33772-1024x576.jpg', date( 'Y' ), date( 'm' ) ), $url ); | ||
|
||
$attachment = $this->factory->attachment->create_upload_object( DIR_TESTDATA .'/images/2004-07-22-DSC_0008.jpg' ); // ~109kb image. | ||
$url = $this->computer_vision->get_largest_acceptable_image_url( | ||
get_attached_file( $attachment ), | ||
wp_get_attachment_url( $attachment, 'full' ), | ||
wp_get_attachment_metadata( $attachment )['sizes'] | ||
); | ||
$this->assertEquals( sprintf( 'http://example.org/wp-content/uploads/%s/%s/2004-07-22-DSC_0008.jpg', date( 'Y' ), date( 'm' ) ), $url ); | ||
|
||
remove_filter( 'classifai_computervision_max_filesize', $set_150kb_max_filesize ); | ||
|
||
$set_1kb_max_filesize = function() { | ||
return 1000; | ||
}; | ||
add_filter( 'classifai_computervision_max_filesize', $set_1kb_max_filesize ); | ||
|
||
$url = $this->computer_vision->get_largest_acceptable_image_url( | ||
get_attached_file( $attachment ), | ||
wp_get_attachment_url( $attachment, 'full' ), | ||
wp_get_attachment_metadata( $attachment )['sizes'] | ||
); | ||
$this->assertNull( $url ); | ||
|
||
remove_filter( 'classifai_computervision_max_filesize', $set_1kb_max_filesize ); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WordPress has some fun built-in constants for file sizes and time units, like
MB_IN_BYTES
. Would advise using that here for legibility and technical accuracy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Didn't know about
MB_IN_BYTES
. Updated.