Created
August 2, 2019 13:42
-
-
Save Garconis/9ea92ed9a412eba86735ceb4ebccc32a to your computer and use it in GitHub Desktop.
WooCommerce | Thumbnail upscale & correct crop in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Upscale images that are otherwise too small for thumbnail cropping */ | |
function gc_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', 'gc_thumbnail_upscale', 10, 6 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment