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