How to perfectly upscale Image in WordPress


While Working on my Wallpaper WordPress themes, It was not an easy task to create perfect image size on the go but generating a perfect image all the time for small image into big image was a difficult task.

A little bit hack and understanding of WordPress resize function was posted on stock exchange to solve this issue. If you want to upscale your image before they are being crop properly then copy paste the following code into your functions.php file


function binary_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', 'binary_thumbnail_upscale', 10, 6 );

Enjoy.  but before that let me allow to explain its limitation. This hack will even upscale an image like 16x16px favicon to the maximum available size either defined in your WordPress media settings or using


add_image_size('homepage', 320, 240, true);

If you have any query related to this function, do not hesitate to place your view as a comment




Suggested Similar Articles

6 Brilliant Comments - Join Discussion Now!

  1. vino says:

    Good explantion with source code to perfectly upscale images in WordPress. And a effective point is this is even helping to upscaling favicon . Thanks for your time in sharing this great tips to your audience

    • rakesh says:

      Hi Vino, Joining an affiliate program is so easy but proper tricks and tips are equally useful to promote their product and now a days it is an art as well as a good science. Hope you know both of them :)

  2. Warad says:

    So lets say,i want to width is 640 px and height is 340 px, which file I have to edit?

    This custom function I added to my wordpress theme function.php right?

  3. ezra says:

    for me this not work, i past code function in functions.php files i add:
    add_image_size( ‘thumb_754_544′, 754, 544, true ); for crop image to 754×544 i upload an image with resolution 400×400 but cropped image not appear, have any ideea why?

    • rakesh says:

      Hi Erza, As you have used TRUE boolean constant inside your add_image_size and as per codex when true is used then the function is forced to crop image from the center of the image. Second to use the cropped image you are requested to use ‘thumb_754_544′

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>