From bac1d69a3af634bdf4f84653df25a30162e8bb32 Mon Sep 17 00:00:00 2001 From: Jonathan Williams Date: Sat, 10 Aug 2024 19:30:56 -0400 Subject: [PATCH] remove generate sizes function we don't need to generate sizes anymore --- src/filters.php | 73 ------------------------------------------------- 1 file changed, 73 deletions(-) diff --git a/src/filters.php b/src/filters.php index cee67b5..6259b03 100644 --- a/src/filters.php +++ b/src/filters.php @@ -82,79 +82,6 @@ function( $file ) { return $editors; } ); - -/** - * Generate metadata for image sizes without creating the actual resized images. - * - * This function gets the registered image sizes and calculates the filename for each size - * using the WordPress convention for size annotation. It then adds this information to the attachment metadata. - * - * This function is designed to be added to the wp_handle_upload_prefilter in order to restore the metadata that - * would have been generated. Becuase the sizes are being suppressed on upload, the actual resized images - * are not being created, but we still need to add the metadata for the sizes to the attachment. This function - * should have the effect of pre-generating the metadata for the sizes that would have been created. - * - * @param array $metadata The attachment metadata. - * @param int $attachment_id The ID of the attachment. - * - * @return array The modified attachment metadata, with added sizes. - */ -function generate_metadata_sizes( $metadata, $attachment_id ) { - // Get the registered image sizes. - $sizes = wp_get_registered_image_subsizes(); - - // Get the pathinfo for the original file. - $pathinfo = pathinfo( $metadata['file'] ); - - // Get the mime type for the original file. - $mime_type = get_post_mime_type( $attachment_id ); - - // Get the original image dimensions. - $original_width = $metadata['width']; - $original_height = $metadata['height']; - - // Calculate the aspect ratio of the original image. - $aspect_ratio = $original_width / $original_height; - - // Recalculate the sizes that would have been generated and add them to the metadata. - foreach ( $sizes as $size => $size_data ) { - // Determine the new dimensions based on the aspect ratio, taking into account that either width or height may be 0. - if ( $size_data['width'] == 0 ) { - // Scale based on height only. - $new_height = $size_data['height']; - $new_width = (int) ( $new_height * $aspect_ratio ); - } elseif ( $size_data['height'] == 0 ) { - // Scale based on width only. - $new_width = $size_data['width']; - $new_height = (int) ( $new_width / $aspect_ratio ); - } else { - // Scale based on both dimensions. - $scale = min( $size_data['width'] / $original_width, $size_data['height'] / $original_height ); - $new_width = (int) ( $original_width * $scale ); - $new_height = (int) ( $original_height * $scale ); - } - - // Clamp the dimensions to the original image size if the new size is larger. - // Basically, we don't want to scale up the image. - if ( $new_width > $original_width || $new_height > $original_height ) { - $new_width = $original_width; - $new_height = $original_height; - } - - // Calculate the new filename by adding the size to the original filename using the WordPress convention. - $new_filename = $pathinfo['filename'] . '-' . $new_width . 'x' . $new_height . '.' . $pathinfo['extension']; - - // Add the new size to the metadata. - $metadata['sizes'][ $size ] = array( - 'file' => $new_filename, - 'width' => $new_width, - 'height' => $new_height, - 'mime-type' => $mime_type, - ); - } - return $metadata; -} - // Disable the big image threshold, we don't want WordPress to do any resizing at all. add_filter( 'big_image_size_threshold', '__return_false' );