Skip Navigation

[Résolu] Image Size Validation for Multiple Images on Form

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:

The issue here is that the user wanted to Validate the image field sizes.

Solution:

The correct solution for this can be seen below.

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;
}
This support ticket is created Il y a 6 années et 3 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Ce sujet contient 2 réponses, a 2 voix.

Dernière mise à jour par julieP Il y a 6 années et 3 mois.

Assisté par: Shane.

Auteur
Publications
#1083580

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

Thank you

#1083888

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi Julie,

Thank you for contacting our support forum.

What you need to do is to add another conditional for each image fields.
Like this

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.");
        }
    }
  }

  if (in_array($form_data['id'], $forms ) && isset($_FILES['wpcf-image-2']['tmp_name']) ) {
    $check2 = getimagesize( $_FILES['wpcf-image-2']['tmp_name'] );
    if ( $check2 !== false ) {
        // check the dimensions only, independent of the orientation.
        // if the height is greater than the width, switch the values for comparison.
        $width = ($check2[0] >= $check2[1]) ? $check2[0] : $check2[1];
        $height = ($check2[1] < $check2[0]) ? $check2[1] : $check2[0];
        if ( $width > $target_max_width || $height > $target_max_height ) {
            $errors['image-2'] = __("Your image exceeds the permissible dimensions. Please resize and try again.");
        }
    }
  }
 if (in_array($form_data['id'], $forms ) && isset($_FILES['wpcf-image-3']['tmp_name']) ) {
    $check3 = getimagesize( $_FILES['wpcf-image-3']['tmp_name'] );
    if ( $check3 !== false ) {
        // check the dimensions only, independent of the orientation.
        // if the height is greater than the width, switch the values for comparison.
        $width = ($check3[0] >= $check3[1]) ? $check3[0] : $check3[1];
        $height = ($check3[1] < $check3[0]) ? $check3[1] : $check3[0];
        if ( $width > $target_max_width || $height > $target_max_height ) {
            $errors['image-3'] = __("Your image exceeds the permissible dimensions. Please resize and try again.");
        }
    }
  }
  $field_data = array($fields,$errors);
  //return result
  return $field_data;
}

Please try this and let me know.

Thanks,
Shane

#1084470

Works like a dream - thank you!