Skip to content

Commit

Permalink
remove generate sizes function
Browse files Browse the repository at this point in the history
we don't need to generate sizes anymore
  • Loading branch information
jdub233 committed Aug 10, 2024
1 parent 28c10f6 commit bac1d69
Showing 1 changed file with 0 additions and 73 deletions.
73 changes: 0 additions & 73 deletions src/filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

Expand Down

0 comments on commit bac1d69

Please sign in to comment.