<?php
/*-----------------------------------------------------------------------------------*/
/* IMAGES SIZES AND MARKUPS 
/*-----------------------------------------------------------------------------------*/ 

add_filter( 'image_resize_dimensions', 'kktfwp_image_resize_upscale', 10, 6 );

if ( ! function_exists( 'kktfwp_image_resize_upscale' ) ) {
	function kktfwp_image_resize_upscale( $default, $orig_w, $orig_h, $new_w, $new_h, $crop ) {
		if ( !$crop ) return null;

		$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 );
	}
}	


// Filter for portfolio and attachments
add_filter( 'wp_get_attachment_image_attributes', 'kktfwp_portfolio_img_attr', 10, 2 );

if ( ! function_exists( 'kktfwp_portfolio_img_attr' ) ) {	
	function kktfwp_portfolio_img_attr( $attr, $attachment ) {
		
	$action = empty( $_REQUEST['action'] ) ? '' : $_REQUEST['action'];	
	if ( is_admin() && !doing_action('wp_ajax_nopriv_kktfwp_more_projects_ajax') && !doing_action('wp_ajax_kktfwp_more_projects_ajax') )return $attr;
		
	if ( ( isset( $attr['data-lazy-load'] ) && (int)$attr['data-lazy-load'] === 1 ) ) {
		if ( isset( $attr['srcset'] ) ) {
			$attr['data-srcset'] = $attr['srcset'];
			$attr['srcset'] = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== 1w'; 
			
			if ( get_post_meta( get_the_ID(), '_grid_type_value', true ) !== 'Portrait' ) {
				$attr['data-sizes'] = 'auto'; 
			}
		} else {
			$attr['srcset'] = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== 1w';
			$attr['data-srcset'] = $attr['src'];
			
			if ( get_post_meta( get_the_ID(), '_grid_type_value', true ) !== 'Portrait' ) {
				$attr['data-sizes'] = 'auto'; 
			}
		}
	}
	
	if ( $full_size = wp_get_attachment_metadata( $attachment->ID ) ) {
		$attr['data-full-size'] = $full_size['width'].'x'.$full_size['height'];	
	}

	return $attr; 
	}
}