Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 810
Sebastian Schmid Avatar asked Nov 19 '12 18:11

Sebastian Schmid


People also ask

How do I fix my WordPress thumbnail?

Using Facebook Debug Tool to Clear the Cache The Facebook debug tool is the easiest way to troubleshoot Facebook thumbnail issues. Simply copy the URL of your WordPress post and paste it in the debugger tool. After that click on the Scrape Again button, and Facebook will update the thumbnail for your post.


1 Answers

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.

like image 82
Daniel C Avatar answered Oct 19 '22 04:10

Daniel C