The situation is this: I’m using Video Thumbnails plugin to automatically get and set youtube/vimeo thumbnails as post featured image. The problem is that default youtube/vimeo thumbnail sizes are just a bit smaller than my theme main content width.

So what I need is to scale them up. If I go to the Media Library, I can edit each image manually, then set my exact width and WordPress scales it up just right (I dont mind that the quality is a bit worse). So is there a way that WP would do that automatically each time an image is uploaded?

This is my defined image size: add_image_size('post-full', 688, 320, true); Vimeo thumb size is 640×320.

2

You can use the native WordPress image_resize function to scale up images. WordPress provides a hook called “image_resize_dimensions” which you can use to overwrite the default cropping settings. Here is a modified function which will support scaling up:

function image_crop_dimensions($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 );
}

Now hook this function like so:

add_filter('image_resize_dimensions', 'image_crop_dimensions', 10, 6);

Once thats done, you can use the image_resize function to scale images up or down as required.

$cropped_image = image_resize($image_filepath, $width, $height, true);

Tags:

Leave a Reply

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