<?php 

/*  Image sizes */
/* ------------------------------------ */ 


$image_sizes = array(
    ['panel', 1920, 1200, true],
    ['panel-cta', 1920, 400, true],
    ['panel-tall', 960, 1200, true],
    ['panel-tall-nocrop', 960, 1200, false],
    ['card-single', 370, 230, true],
    ['card-double', 740, 460, true],
    ['onboarding', 640, 1020, true],
    ['gallery-thumbnail', 392, 222, true],
    // ['gallery-thumbnail', 786, 444, true],
    ['gallery-large', 1024, 2400, false],
);


foreach( $image_sizes as $image_size ) {
    add_image_size( $image_size[0],$image_size[1],$image_size[2],$image_size[3] );
}



/*  Thumbnail upscale - forces images to fit correct aspect ratio */
/* ------------------------------------ */ 
function media_cb_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', 'media_cb_thumbnail_upscale', 10, 6 );


/*  Filter Gallery */
/* ------------------------------------ */ 

add_filter('post_gallery', 'my_post_gallery', 10, 2);
function my_post_gallery($output, $attr) {
    global $post;

    if (isset($attr['orderby'])) {
        $attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
        if (!$attr['orderby'])
            unset($attr['orderby']);
    }

    extract(shortcode_atts(array(
        'order' => 'ASC',
        'orderby' => 'menu_order ID',
        'id' => $post->ID,
        'itemtag' => 'dl',
        'icontag' => 'dt',
        'captiontag' => 'dd',
        'columns' => 3,
        'size' => 'thumbnail',
        'include' => '',
        'exclude' => ''
    ), $attr));

    $id = intval($id);
    if ('RAND' == $order) $orderby = 'none';

    if (!empty($include)) {
        $include = preg_replace('/[^0-9,]+/', '', $include);
        $_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));

        $attachments = array();
        foreach ($_attachments as $key => $val) {
            $attachments[$val->ID] = $_attachments[$key];
        }
    }

    if (empty($attachments)) return '';

    // Here's your actual output, you may customize it to your need
    $output = "<div class=\"gallery-grid\">\n";

    // Now you loop through each attachment
    foreach ($attachments as $id => $attachment) {
        // Fetch the thumbnail (or full image, it's up to you)
//      $img = wp_get_attachment_image_src($id, 'medium');
//      $img = wp_get_attachment_image_src($id, 'my-custom-image-size');
        $img = wp_get_attachment_image_src($id, 'full');

        $output .= "<a href=\"{$img[0]}\">\n";
        $output .= "<img src=\"{$img[0]}\"  alt=\"\" />\n";
        $output .= "</a>\n";
    }

    $output .= "</div>\n";

    return $output;
}