Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 11 additions & 9 deletions modules/images/dominant-color/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,34 +258,36 @@ function dominant_color_set_image_editors() {
* @return array|WP_Error Array with the dominant color and has transparency values or WP_Error on error.
*/
function _dominant_color_get_dominant_color_data( $attachment_id ) {
if ( ! wp_attachment_is_image( $attachment_id ) ) {
$mime_type = get_post_mime_type( $attachment_id );
if ( 'application/pdf' === $mime_type ) {
return new WP_Error( 'no_image_found', __( 'Unable to load image.', 'performance-lab' ) );
}
$file = wp_get_attachment_file_path( $attachment_id );
if ( ! $file ) {
$file = get_attached_file( $attachment_id );
}
add_filter( 'wp_image_editors', 'dominant_color_set_image_editors' );
$editor = wp_get_image_editor( $file );
$editor = wp_get_image_editor(
$file,
array(
'methods' => array(
'get_dominant_color',
'has_transparency',
),
)
);
remove_filter( 'wp_image_editors', 'dominant_color_set_image_editors' );

if ( is_wp_error( $editor ) ) {
return $editor;
}

if ( ! method_exists( $editor, 'has_transparency' ) ) {
return new WP_Error( 'unable_to_find_method', __( 'Unable to find has_transparency method', 'performance-lab' ) );
}
$has_transparency = $editor->has_transparency();
if ( is_wp_error( $has_transparency ) ) {
return $has_transparency;
}
$dominant_color_data['has_transparency'] = $has_transparency;

if ( ! method_exists( $editor, 'get_dominant_color' ) ) {
return new WP_Error( 'unable_to_find_method', __( 'Unable to find get_dominant_color method', 'performance-lab' ) );
}

$dominant_color = $editor->get_dominant_color();
if ( is_wp_error( $dominant_color ) ) {
return $dominant_color;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,5 @@ public function test_get_dominant_color_none_images( $image_path ) {
$dominant_color_data = _dominant_color_get_dominant_color_data( $attachment_id );

$this->assertWPError( $dominant_color_data );
$this->assertStringContainsString( 'no_image_found', $dominant_color_data->get_error_code() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,5 @@ public function test_get_dominant_color_none_images( $image_path ) {
$dominant_color_data = _dominant_color_get_dominant_color_data( $attachment_id );

$this->assertWPError( $dominant_color_data );
$this->assertStringContainsString( 'no_image_found', $dominant_color_data->get_error_code() );
}
}