Skip to content

Instantly share code, notes, and snippets.

@swinggraphics
Last active February 3, 2025 02:52
Show Gist options
  • Save swinggraphics/1b66e0d58ea893c152ca25e369836814 to your computer and use it in GitHub Desktop.
Save swinggraphics/1b66e0d58ea893c152ca25e369836814 to your computer and use it in GitHub Desktop.
Use thumbnail settings as minimum size
add_filter( 'wp_constrain_dimensions', 'mcoc_constrain_dimensions', 11, 5 );
function mcoc_constrain_dimensions( $dimensions, $current_width, $current_height, $max_width, $max_height ) {
// Stop if desired size is not post thumbnail
if ( ( $max_width != get_option( 'thumbnail_size_w' ) ) || ( $max_height != get_option( 'thumbnail_size_h' ) ) ) return $dimensions;
// Ensure cropped width is at least post thumbnail width
if ( $dimensions[0] >= $max_width ) return $dimensions;
if ( $current_height > $max_height ) {
return [
$max_width,
$max_width * $current_height / $current_width
];
}
return $dimensions;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment