Force Wordpress to create Thumbnails of same Size as the uploaded Image

image, resize, wordpress

Solution

Here is the solution you are looking for, to be placed in your functions.php file:

function thumbnail_upscale( $default, $orig_w, $orig_h, $new_w, $new_h, $crop ){
    if ( !$crop ) return null; // let the wordpress default function handle this

    $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 array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
}
add_filter( 'image_resize_dimensions', 'thumbnail_upscale', 10, 6 );

This is found here: https://wordpress.stackexchange.com/questions/50649/how-to-scale-up-featured-post-thumbnail

It will upscale images and generate thumbnails for all image sizes. The only downfall, which probably isn't a downfall, is that no matter what you'll get a separate file for each image size for all uploaded images... even if it's a small icon or something. Hope this helps.

Problem

AFAIK, Wordpress only generates Thumbnails (e.g., 'large') only when the desired image size is smaller (and not when the target size would be the same). I use `add_image_size( 'referenzen-big', 627, 490, true );` So if my customer uploads a file with 627x490px, the original Image will be used. But this is not desired, since this customed, in all good faith, uploads the images in the right size, and also highest possible .jpg-compression. This results in images being around 300kB in this case. A pratical, but technicall not flawless way would be to ask him to upload his images in 628x490, so 1px more width, forcing a rescale. Suggestions like this will not be accepted :-) So far, I've investigated the hooks responsible for image creation, and tracked the responsible function down; here's the last hook I found: image_make_intermediate_size() this is the function responsible for the actual resizing: image_resize_dimensions(). There's even a line saying: ``` // if the resulting image would be the same size or larger we don't want to resize it if ( $new_w >= $orig_w && $new_h >= $orig_h ) return false; ``` So how can I enable this "force resize" feature?

Original source