Skip to content

Commit

Permalink
Update Next-Gen gallery compatibility for AVIF support (#846)
Browse files Browse the repository at this point in the history
Co-authored-by: Mathieu Lamiot <[email protected]>
  • Loading branch information
remyperona and MathieuLamiot authored Mar 21, 2024
1 parent a11a7c4 commit 0100d20
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 53 deletions.
4 changes: 2 additions & 2 deletions inc/3rd-party/nextgen-gallery/classes/Bulk/NGG.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function get_optimized_media_ids_without_format( $format ) {
) );

$wpdb->flush();
unset( $ngg_table, $data_table, $webp_suffix );
unset( $ngg_table, $data_table, $suffix );

$data = [
'ids' => [],
Expand All @@ -169,7 +169,7 @@ public function get_optimized_media_ids_without_format( $format ) {
$backup_path = get_imagify_ngg_attachment_backup_path( $file_path );

if ( ! $this->filesystem->exists( $backup_path ) ) {
// No backup, no WebP.
// No backup, no next-gen.
$data['errors']['no_backup'][] = $file_id;
continue;
}
Expand Down
119 changes: 68 additions & 51 deletions inc/3rd-party/nextgen-gallery/inc/common/attachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,74 +150,80 @@ function _imagify_ngg_media_library_imported_image_data( $image, $attachment ) {
}

/**
* WebP for the full size.
* Next-gen for the full size.
* Look for an existing copy locally:
* - if it exists, copy it (and its optimization data),
* - if not, add it to the optimization queue.
*/
$add_full_webp = $wp_media->is_image() && get_imagify_option( 'convert_to_webp' );
$add_full_nextgen = $wp_media->is_image();

if ( $add_full_webp ) {
// It's a supported image and WebP conversion is enabled.
$wp_full_path_webp = false;
$webp_size_name = 'full' . $wp_process::WEBP_SUFFIX;
$wp_webp_data = $wp_data->get_size_data( $webp_size_name );
if ( $add_full_nextgen ) {
$formats = [
'avif' => $wp_process::AVIF_SUFFIX,
'webp' => $wp_process::WEBP_SUFFIX,
];

// Get the path to the WebP image if it exists.
$wp_full_path_webp = $wp_process->get_fullsize_file()->get_path_to_webp();
foreach ( $formats as $extension => $suffix ) {
$wp_full_path_nextgen = false;
$nextgen_size_name = 'full' . $suffix;
$wp_nextgen_data = $wp_data->get_size_data( $nextgen_size_name );

if ( $wp_full_path_webp && ! $filesystem->exists( $wp_full_path_webp ) ) {
$wp_full_path_webp = false;
}
// Get the path to the next-gen image if it exists.
$wp_full_path_nextgen = $wp_process->get_fullsize_file()->get_path_to_nextgen( $extension );

if ( $wp_full_path_nextgen && ! $filesystem->exists( $wp_full_path_nextgen ) ) {
$wp_full_path_nextgen = false;
}

if ( $wp_full_path_webp ) {
// We know we have a WebP version. Make sure we have the right data.
$wp_webp_data['success'] = true;
if ( $wp_full_path_nextgen ) {
// We know we have a next-gen version. Make sure we have the right data.
$wp_nextgen_data['success'] = true;

if ( empty( $wp_webp_data['original_size'] ) ) {
// The WebP data is missing.
$full_size_weight = $wp_full_size_data['original_size'];
if ( empty( $wp_nextgen_data['original_size'] ) ) {
// The next-gen data is missing.
$full_size_weight = $wp_full_size_data['original_size'];

if ( ! $full_size_weight && $wp_backup_path ) {
// For some reason we don't have the original file weight, but we can get it from the backup file.
$full_size_weight = $filesystem->size( $wp_backup_path );
if ( ! $full_size_weight && $wp_backup_path ) {
// For some reason we don't have the original file weight, but we can get it from the backup file.
$full_size_weight = $filesystem->size( $wp_backup_path );

if ( $full_size_weight ) {
$wp_webp_data['original_size'] = $full_size_weight;
if ( $full_size_weight ) {
$wp_nextgen_data['original_size'] = $full_size_weight;
}
}
}
}

if ( ! empty( $wp_webp_data['original_size'] ) && empty( $wp_webp_data['optimized_size'] ) ) {
// The WebP file size.
$wp_webp_data['optimized_size'] = $filesystem->size( $wp_full_path_webp );
}
if ( ! empty( $wp_nextgen_data['original_size'] ) && empty( $wp_nextgen_data['optimized_size'] ) ) {
// The next-gen file size.
$wp_nextgen_data['optimized_size'] = $filesystem->size( $wp_full_path_nextgen );
}

if ( empty( $wp_webp_data['original_size'] ) || empty( $wp_webp_data['optimized_size'] ) ) {
// We must have both original and optimized sizes.
$wp_webp_data = [];
if ( empty( $wp_nextgen_data['original_size'] ) || empty( $wp_nextgen_data['optimized_size'] ) ) {
// We must have both original and optimized sizes.
$wp_nextgen_data = [];
}
}
}

if ( $wp_full_path_webp && $wp_webp_data ) {
// We have the file and the data.
// Copy the file.
$ngg_full_file = new File( $ngg_media->get_raw_fullsize_path() );
$ngg_full_path_webp = $ngg_full_file->get_path_to_webp(); // Destination.
if ( $wp_full_path_nextgen && $wp_nextgen_data ) {
// We have the file and the data.
// Copy the file.
$ngg_full_file = new File( $ngg_media->get_raw_fullsize_path() );
$ngg_full_path_nextgen = $ngg_full_file->get_path_to_nextgen( $extension ); // Destination.

if ( $ngg_full_path_webp ) {
$copied = $filesystem->copy( $wp_full_path_webp, $ngg_full_path_webp, true );
if ( $ngg_full_path_nextgen ) {
$copied = $filesystem->copy( $wp_full_path_nextgen, $ngg_full_path_nextgen, true );

if ( $copied ) {
// Success.
$filesystem->chmod_file( $ngg_full_path_webp );
$add_full_webp = false;
if ( $copied ) {
// Success.
$filesystem->chmod_file( $ngg_full_path_nextgen );
$add_full_nextgen = false;
}
}
}

if ( ! $add_full_webp ) {
// The WebP file has been successfully copied: now, copy the data.
$ngg_process->get_data()->update_size_optimization_data( $webp_size_name, $wp_webp_data );
if ( ! $add_full_nextgen ) {
// The next-gen file has been successfully copied: now, copy the data.
$ngg_process->get_data()->update_size_optimization_data( $nextgen_size_name, $wp_nextgen_data );
}
}
}
}
Expand All @@ -226,9 +232,20 @@ function _imagify_ngg_media_library_imported_image_data( $image, $attachment ) {
$sizes = $ngg_media->get_media_files();
unset( $sizes['full'] );

if ( $add_full_webp ) {
// We could not use a local WebP copy: ask for a new one.
$sizes[ $webp_size_name ] = [];
if ( $add_full_nextgen ) {
// We could not use a local next-gen copy: ask for a new one.

$formats = imagify_nextgen_images_formats();

foreach ( $formats as $format ) {
if ( 'webp' === $format ) {
$suffix = $wp_process::WEBP_SUFFIX;
} elseif ( 'avif' === $format ) {
$suffix = $wp_process::AVIF_SUFFIX;
}

$sizes[ 'full' . $suffix ] = [];
}
}

if ( ! $sizes ) {
Expand Down Expand Up @@ -282,7 +299,7 @@ function imagify_ngg_cleanup_after_media_deletion( $image_id, $image ) {

/**
* The backup file has already been deleted by NGG.
* Delete the WebP versions and the optimization data.
* Delete the next-gen versions and the optimization data.
*/
$process->delete_nextgen_files();

Expand Down

0 comments on commit 0100d20

Please sign in to comment.