Skip to content

Commit

Permalink
Closes #794 display the smaller size on image library data (#810)
Browse files Browse the repository at this point in the history
Co-authored-by: Gaël Robin <[email protected]>
Co-authored-by: Michael Lee <[email protected]>
Co-authored-by: Rémy Perona <[email protected]>
Co-authored-by: Rémy Perona <[email protected]>
Co-authored-by: WordPress Fan <[email protected]>
Co-authored-by: Mathieu Lamiot <[email protected]>
  • Loading branch information
7 people authored Mar 20, 2024
1 parent a92dd2c commit 3b16931
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 33 deletions.
4 changes: 0 additions & 4 deletions classes/Bulk/Bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
62 changes: 39 additions & 23 deletions classes/Optimization/Data/AbstractData.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,36 +281,44 @@ 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
* @author Grégory Viguier
*
* @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 ) {
Expand All @@ -320,18 +328,21 @@ 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;
}
}

if ( ! $filepath ) {
// No WebP? The full size then.
// No Nextgen? The full size then.
$filepath = $media->get_fullsize_path();
}

Expand Down Expand Up @@ -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 );
}
Expand Down
6 changes: 3 additions & 3 deletions classes/Optimization/Data/DataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,18 @@ 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
* @author Grégory Viguier
*
* @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 );


/** ----------------------------------------------------------------------------------------- */
Expand Down
6 changes: 3 additions & 3 deletions classes/Optimization/Data/Noop.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,18 @@ 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
* @author Grégory Viguier
*
* @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;
}

Expand Down

0 comments on commit 3b16931

Please sign in to comment.