Skip to content
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 14 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from 8 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
91 changes: 78 additions & 13 deletions includes/Classifai/Providers/Azure/ComputerVision.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

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.

Copy link
Member Author

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.

}

/**
* 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.
Expand All @@ -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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please keep these @param descriptions aligned

* @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.
*
Expand Down
77 changes: 77 additions & 0 deletions tests/Classifai/Providers/Azure/ComputerVisionTest.php
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 );
}
}