By default, if an image you upload to WordPress is smaller than a defined thumbnail size (e.g., your theme defines a 400×400 thumbnail but the uploaded image is only 300×300), WordPress will not upscale that image. This is a deliberate design choice to prevent pixelation. However, in some grid-based theme designs, this results in uneven image heights, which can ruin the layout. This article shows how to force WordPress to upscale smaller images and crop them to the requested dimensions.
Using the image_resize_dimensions Filter
We can hook into image_resize_dimensions to override the thumbnail generation logic. The key is to tell WordPress that even if the source is smaller than the target, it should still perform the crop operation.
add_filter('image_resize_dimensions', function ($payload, $orig_w, $orig_h, $dest_w, $dest_h, $crop)
{
if (!$crop) {
return $payload; // Only interfere if cropping is enabled
}
if ($orig_w >= $dest_w && $orig_h >= $dest_h) {
return $payload; // Use default behavior if image is large enough
}
// Force rescaling for smaller images
$aspect_ratio = $orig_w / $orig_h;
$new_w = $dest_w;
$new_h = $dest_h;
if ($new_w / $new_h > $aspect_ratio) {
$new_h = round($new_w / $aspect_ratio);
} else {
$new_w = round($new_h * $aspect_ratio);
}
$s_x = floor(($new_w - $dest_w) / 2);
$s_y = floor(($new_h - $dest_h) / 2);
return [0, 0, (int)$s_x, (int)$s_y, (int)$dest_w, (int)$dest_h, (int)$new_w, (int)$new_h];
}, 10, 6);
Regenerating Thumbnails
Note that this filter only applies to images uploaded after the code is added. For existing images, you will need to use a plugin like “Regenerate Thumbnails” to process them again. With the filter active, the plugin will now be able to upscale and crop those previously “too small” images.
Pros and Cons
- Pros: Perfect layout alignment in grids and sliders. No more “broken” designs due to small client uploads.
- Cons: Potential pixelation or blurriness if the source image is significantly smaller than the target size. Use this with caution and educate users on minimum image requirements.
