Hi. I have a problem with featured image cropping. I edited td_config.php like this:
td_api_thumb::add('td_696x385',
array(
'name' => 'td_696x385',
'width' => 696,
'height' => 385,
'crop' => array('center', 'top'),
'post_format_icon_size' => 'normal',
'used_on' => array(
'Module 14, 17, 18, MX8', 'Block 13, 18, 20', 'Slide - 2 columns'
)
)
);
I regenerated all thumbnails, but “portrait” images like this aren’t cropped. I also noticed that sometimes images are not cropped exactly from the top, some “heads” are cut, but at the moment I don’t have an active link.
I’m investigating because in a local environment crop of higher images seems to work, but the cropping is not made from top. I made a screenshot. Thanks!
Maybe it was my fault. Because Thumbnail Upscale Plugin was causing problems with other plugins, I added some code to handle upscale of smaller images, but it didn’t take account of crop position. Here is the correct code for future reference.
/* Thumbnail upscale
/* ------------------------------------ */
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 );
if( is_array( $crop ) ) {
if( $crop[ 0 ] === 'left' ) {
$s_x = 0;
} else if( $crop[ 0 ] === 'right' ) {
$s_x = $orig_w - $crop_w;
}
if( $crop[ 1 ] === 'top' ) {
$s_y = 0;
} else if( $crop[ 1 ] === 'bottom' ) {
$s_y = $orig_h - $crop_h;
}
}
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 );