Force Small WordPress Images to Be Upscaled and Cropped to the Required Thumbnail Size

By default, WordPress can crop images down to a smaller size when the original image is larger than the target size. But if the original image is smaller than the requested thumbnail size, WordPress will not upscale it automatically.

That behavior can be a problem in themes with strict layout requirements, because undersized images may break the visual grid and create an inconsistent experience.

In some projects what really matters is that the front end always outputs an image with the required dimensions and aspect ratio, even if that means the image is enlarged and becomes a bit softer.

Upscale and crop small WordPress thumbnails to a fixed size

To make WordPress upscale small images and crop them to the requested dimensions, add the following code to the theme’s functions.php file.

add_filter( 'image_resize_dimensions', function ( $default, $orig_w, $orig_h, $new_w, $new_h, $crop ) {
    if ( ! $crop ) {
        return null; // Let the default WordPress behavior handle it.
    }

    $aspect_ratio = $orig_w / $orig_h;
    $size_ratio   = max( $new_w / $orig_w, $new_h / $orig_h );

    $crop_w = round( $new_w / $size_ratio );
    $crop_h = round( $new_h / $size_ratio );

    $s_x = floor( ( $orig_w - $crop_w ) / 2 );
    $s_y = floor( ( $orig_h - $crop_h ) / 2 );

    return [ 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h ];
}, 10, 6 );

A small drawback of this method and a better long-term idea

This approach does have one drawback: even if you only need a small image, WordPress may still generate an enlarged thumbnail file, which consumes some extra disk space.

In practice, storage is usually cheap enough that this tradeoff is acceptable compared with the layout stability it provides.

A more elegant long-term solution would be on-demand resizing and cropping, where thumbnails are not generated unless they are actually needed, and where the front end receives exactly the size it requests. I have not yet found the ideal implementation for that, but it would avoid the extra generated files entirely.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *