12

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 640x320.

1

2 Answers 2

23

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);
6
  • I'm using this method however when I try and generate the cropped image then I just get the error: Image Editor Save Failed - any ideas?
    – Felix Eve
    Apr 17, 2013 at 8:22
  • Still works great in July of 2015 with Wordpress 4.1.5. Exactly what I needed to ensure small images scaled up for my posts' Featured Images. Jul 11, 2015 at 12:42
  • For those looking to support the crop position option, add the following code right before the return of the function: if( is_array( $crop ) ) { if( $crop[ 0 ] === 'left' ) { $s_x = 0; } else if( $crop[ 0 ] === 'right' ) { $s_x = $orig_w - $crop_w;} if( $crop[ 1 ] === 'top' ) { $s_y = 0; } else if( $crop[ 1 ] === 'bottom' ) { $s_y = $orig_h - $crop_h; } }
    – jackrugile
    Jul 24, 2015 at 21:43
  • Still works great April 2019 with Wordpress 5.1.1. I use it to force upscale images that are smaller than my custom desired add_image_size. Along with the Regenerate Thumbnails plugin.
    – Mtxz
    Apr 11, 2019 at 1:18
  • how to make it work with add_image_size function?
    – Gediminas
    Aug 16, 2019 at 11:53
0

the easiest way would be to add an image-size with 640x298, and use css to resize it. as it is just a minor scale up, the browser scaling should work quite fine.

unfortunately, all the plugins i know do not provide image upscaling, just generating of the smaller image sizes, so if you want to have the 688x320 on your server, you would have to edit one of the existing plugins.

if you have Imagick installed, you can alter your plugin in file video-thumbnails.php on line 325, generating a larger version of the thumbnail, using this code:

$image_big = new Imagick();
$image_big->setOption('jpeg:size', '688x344');
$image_big->readImage($new_thumbnail);

$upload = wp_upload_bits( basename( $new_thumbnail ), null, $image_big );

and let wordpress generate the smaller thumbnail from it (cutting the height). Be sure to make different Imagesizes for the different Videoplatforms in your Imagick Code!

2
  • I think I'll go with the CSS fix right now, it is really the easiest/fastest way to go on my situation. I also checked WP core files and it seems that the resizing function uses min(); to determine min width/height and there are no hooks to modify that, so it wont upscale my images on upload.
    – evaqas
    May 1, 2012 at 6:49
  • There is a hook you can use called "image_resize_dimensions" which will enable you rewrite the function without min().
    – levi
    Sep 6, 2012 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.