I've used/tweaked the hook described in this thread https://toolset.com/forums/topic/featured-image-size-validation-in-form/ (that was assigned to Christian) to show an error message to user if they upload an image that exceeds certain dimensions. I have multiple images on the form (wpcf-image-1, wpcf-image-2, wpcf-image-3), how do I amend my code to include these additional images please?
add_action('cred_form_ajax_upload_validate', 'toolset_max_img_size_restriction_521_529_528',10,2);
function toolset_max_img_size_restriction_521_529_528($error_fields, $form_data) {
$forms = array( 521,529,528 );
// Field data are field values and errors
list($fields,$errors)=$error_fields;
if (in_array($form_data['id'], $forms ) && isset($_FILES['wpcf-image-1']['tmp_name']) ) {
$target_max_width = 300;
$target_max_height = 300;
$check = getimagesize( $_FILES['wpcf-image-1']['tmp_name'] );
if ( $check !== false ) {
// check the dimensions only, independent of the orientation.
// if the height is greater than the width, switch the values for comparison.
$width = ($check[0] >= $check[1]) ? $check[0] : $check[1];
$height = ($check[1] < $check[0]) ? $check[1] : $check[0];
if ( $width > $target_max_width || $height > $target_max_height ) {
$errors['image-1'] = __("Your image exceeds the permissible dimensions. Please resize and try again.");
}
}
}
$field_data = array($fields,$errors);
//return result
return $field_data;
}