Skip to content

Instantly share code, notes, and snippets.

@alvaromartinezmajado
Last active October 18, 2016 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alvaromartinezmajado/1a7d5ecae65e85412649ffc1108355e5 to your computer and use it in GitHub Desktop.
Save alvaromartinezmajado/1a7d5ecae65e85412649ffc1108355e5 to your computer and use it in GitHub Desktop.
Forces wordpress crop to upscalle an image if necessary to match all registered sizes
<?php
/**
* Allows wordpress crop system to upscale images if needed
* See http://wordpress.stackexchange.com/a/64953
*/
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 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment