Skip to content

Instantly share code, notes, and snippets.

@raidenz
Last active December 29, 2016 09:34
Show Gist options
  • Save raidenz/6c69fd0a8fac93265e63139075e6f2fa to your computer and use it in GitHub Desktop.
Save raidenz/6c69fd0a8fac93265e63139075e6f2fa to your computer and use it in GitHub Desktop.
wp upscale
<?php
/* Thumbnail upscale
** source
** http://alxmedia.se/code/2013/10/thumbnail-upscale-correct-crop-in-wordpress/
** ------------------------------------ */
function alx_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', 'alx_thumbnail_upscale', 10, 6 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment