×

How to perfectly upscale Image in WordPress

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

[code]

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 );

[/code]

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

[code]

add_image_size(‘homepage’, 320, 240, true);

[/code]

Original Work is here at http://alxmedia.se/code/2013/10/thumbnail-upscale-correct-crop-in-wordpress/

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

Related Posts

3 thoughts on “How to perfectly upscale Image in WordPress”

Comments are closed.