From 3b16931ce8c291d1838c0ec76727c76017eaad76 Mon Sep 17 00:00:00 2001 From: Opeyemi Ibrahim Date: Wed, 20 Mar 2024 08:56:28 +0000 Subject: [PATCH] Closes #794 display the smaller size on image library data (#810) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Gaël Robin Co-authored-by: Michael Lee <38788055+jeawhanlee@users.noreply.github.com> Co-authored-by: Rémy Perona Co-authored-by: Rémy Perona Co-authored-by: WordPress Fan <146129302+wordpressfan@users.noreply.github.com> Co-authored-by: Mathieu Lamiot --- classes/Bulk/Bulk.php | 4 -- classes/Optimization/Data/AbstractData.php | 62 +++++++++++++-------- classes/Optimization/Data/DataInterface.php | 6 +- classes/Optimization/Data/Noop.php | 6 +- 4 files changed, 45 insertions(+), 33 deletions(-) diff --git a/classes/Bulk/Bulk.php b/classes/Bulk/Bulk.php index e049dc122..c9d07e137 100644 --- a/classes/Bulk/Bulk.php +++ b/classes/Bulk/Bulk.php @@ -583,10 +583,6 @@ public function bulk_get_stats_callback() { * @param array $old_value The old option value. * @param array $value The new option value. * - * Please note that the convert_to_avif new value is a checkbox, - * so it equals 1 when it's set otherwise it's not set. - * That's why we need to use empty function when checking its value. - * * @return void */ public function maybe_generate_missing_nextgen( $old_value, $value ) { diff --git a/classes/Optimization/Data/AbstractData.php b/classes/Optimization/Data/AbstractData.php index 3c30b4424..df58bc882 100644 --- a/classes/Optimization/Data/AbstractData.php +++ b/classes/Optimization/Data/AbstractData.php @@ -281,7 +281,7 @@ public function get_original_size( $human_format = true, $decimals = 2 ) { /** * Get the file size of the full size file. - * If the WebP size is available, it is used. + * If the Nextgen size is available, it is used. * * @since 1.9 * @access public @@ -289,28 +289,36 @@ public function get_original_size( $human_format = true, $decimals = 2 ) { * * @param bool $human_format True to display the image human format size (1Mb). * @param int $decimals Precision of number of decimal places. - * @param bool $use_webp Use the WebP size if available. + * @param bool $use_nextgen Use the Nextgen size if available. * @return string|int */ - public function get_optimized_size( $human_format = true, $decimals = 2, $use_webp = true ) { + public function get_optimized_size( $human_format = true, $decimals = 2, $use_nextgen = true ) { if ( ! $this->is_valid() ) { return $human_format ? imagify_size_format( 0, $decimals ) : 0; } - $data = $this->get_optimization_data(); - $media = $this->get_media(); - - if ( $use_webp ) { - $process_class_name = imagify_get_optimization_process_class_name( $media->get_context() ); - $webp_size_name = 'full' . constant( $process_class_name . '::WEBP_SUFFIX' ); - } - - if ( $use_webp && ! empty( $data['sizes'][ $webp_size_name ]['optimized_size'] ) ) { - $size = (int) $data['sizes'][ $webp_size_name ]['optimized_size']; + $data = $this->get_optimization_data(); + $media = $this->get_media(); + $format = 'webp'; + + $process_class_name = imagify_get_optimization_process_class_name( $media->get_context() ); + $nextgen_avif_size_name = 'full' . constant( $process_class_name . '::AVIF_SUFFIX' ); + $nextgen_webp_size_name = 'full' . constant( $process_class_name . '::WEBP_SUFFIX' ); + + $size = 0; + + if ( $use_nextgen ) { + /**Checking for success status before size, some cases the response is false + * because the image is already compressed, or we have a connection timed out + * */ + $size = ! empty( $data['sizes'][ $nextgen_webp_size_name ] ) && $data['sizes'][ $nextgen_webp_size_name ]['success'] ? + (int) $data['sizes'][ $nextgen_webp_size_name ]['optimized_size'] : 0; + if ( ! empty( $data['sizes'][ $nextgen_avif_size_name ]['optimized_size'] ) && + $data['sizes'][ $nextgen_avif_size_name ] ) { + $size = (int) $data['sizes'][ $nextgen_avif_size_name ]['optimized_size']; + } } elseif ( ! empty( $data['sizes']['full']['optimized_size'] ) ) { $size = (int) $data['sizes']['full']['optimized_size']; - } else { - $size = 0; } if ( $size ) { @@ -320,10 +328,13 @@ public function get_optimized_size( $human_format = true, $decimals = 2, $use_we // If nothing in the database, try to get the info from the file. $filepath = false; - if ( $use_webp && ! empty( $data['sizes'][ $webp_size_name ]['success'] ) ) { - // Try with the WebP file first. + if ( $use_nextgen ) { + if ( ! empty( $data['sizes'][ $nextgen_avif_size_name ]['success'] ) ) { + $format = 'avif'; + } + // Try with the Nextgen file first. $filepath = $media->get_raw_fullsize_path(); - $filepath = $filepath ? imagify_path_to_webp( $filepath ) : false; + $filepath = $filepath ? imagify_path_to_nextgen( $filepath, $format ) : false; if ( ! $filepath || ! $this->filesystem->exists( $filepath ) ) { $filepath = false; @@ -331,7 +342,7 @@ public function get_optimized_size( $human_format = true, $decimals = 2, $use_we } if ( ! $filepath ) { - // No WebP? The full size then. + // No Nextgen? The full size then. $filepath = $media->get_fullsize_path(); } @@ -418,15 +429,20 @@ public function get_saving_percent() { } $process_class_name = imagify_get_optimization_process_class_name( $this->get_media()->get_context() ); - $webp_size_name = 'full' . constant( $process_class_name . '::WEBP_SUFFIX' ); + $nextgen_webp_size_name = 'full' . constant( $process_class_name . '::WEBP_SUFFIX' ); + $nextgen_avif_size_name = 'full' . constant( $process_class_name . '::AVIF_SUFFIX' ); - $percent = $this->get_size_data( $webp_size_name, 'percent' ); + $percent = $this->get_size_data( $nextgen_avif_size_name, 'percent' ); + // Check for webp version if avif is not found. if ( ! $percent ) { - $percent = $this->get_size_data( 'full', 'percent' ); + $percent = $this->get_size_data( $nextgen_webp_size_name, 'percent' ); } - $percent = $percent ? $percent : 0; + if ( ! $percent ) { + $percent = $this->get_size_data( 'full', 'percent' ); + } + $percent = $percent ?: 0; return round( (float) $percent, 2 ); } diff --git a/classes/Optimization/Data/DataInterface.php b/classes/Optimization/Data/DataInterface.php index 3a6aa0768..26f4a7280 100644 --- a/classes/Optimization/Data/DataInterface.php +++ b/classes/Optimization/Data/DataInterface.php @@ -208,7 +208,7 @@ public function get_original_size( $human_format = true, $decimals = 2 ); /** * Get the file size of the full size file. - * If the WebP size is available, it is used. + * If the Nextgen size is available, it is used. * * @since 1.9 * @access public @@ -216,10 +216,10 @@ public function get_original_size( $human_format = true, $decimals = 2 ); * * @param bool $human_format True to display the image human format size (1Mb). * @param int $decimals Precision of number of decimal places. - * @param bool $use_webp Use the WebP size if available. + * @param bool $use_nextgen Use the Nextgen size if available. * @return string|int */ - public function get_optimized_size( $human_format = true, $decimals = 2, $use_webp = true ); + public function get_optimized_size( $human_format = true, $decimals = 2, $use_nextgen = true ); /** ----------------------------------------------------------------------------------------- */ diff --git a/classes/Optimization/Data/Noop.php b/classes/Optimization/Data/Noop.php index f134e268b..efa04181a 100644 --- a/classes/Optimization/Data/Noop.php +++ b/classes/Optimization/Data/Noop.php @@ -208,7 +208,7 @@ public function get_original_size( $human_format = true, $decimals = 2 ) { /** * Get the file size of the full size file. - * If the WebP size is available, it is used. + * If the Nextgen size is available, it is used. * * @since 1.9 * @access public @@ -216,10 +216,10 @@ public function get_original_size( $human_format = true, $decimals = 2 ) { * * @param bool $human_format True to display the image human format size (1Mb). * @param int $decimals Precision of number of decimal places. - * @param bool $use_webp Use the WebP size if available. + * @param bool $use_nextgen Use the Nextgen size if available. * @return string|int */ - public function get_optimized_size( $human_format = true, $decimals = 2, $use_webp = true ) { + public function get_optimized_size( $human_format = true, $decimals = 2, $use_nextgen = true ) { return $human_format ? imagify_size_format( 0, $decimals ) : 0; }