Thank you for the patience. Given that you have the constraints of 150*150 for your image , I don't see the user being able to upload an image that is larger that 5mb.
However here is the validation code to check if the user has uploaded an image based on your dimensions.
add_action('cred_form_validate', 'validate_image_dimensions',10,2);
function validate_image_dimensions($error_fields, $form_data) {
$forms = array( 1234 );
// Field data are field values and errors
list($fields,$errors)=$error_fields;
if (in_array($form_data['id'], $forms ) && isset($_FILES['wpcf-image']['tmp_name']) ) {
$target_max_width = 150;
$target_max_height = 150;
$check = getimagesize( $_FILES['wpcf-image']['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'] = __("Your image exceeds the permissible dimensions. Please resize and try again.");
}
}
}
$field_data = array($fields,$errors);
//return result
return $field_data;
}
Add this to your Toolset custom code section in Toolset->Settings->Custom Code and ensure that you've activated it.
Please change all instances of 'wpcf-image' to your image field slug keeping the 'wpcf-' prefix attached to the slug. Also where you see 1234 replace this with the ID of your form.
1. Apologies, I think my requirement caused a confusion. In my requirement, the image dimensions should be min height 150 and min width 150. The code what you have provided validates and controls to max 150 height and max 150 width.
2. Tried adding the images with file size more than 5 MB and it is allowing. Could you please suggest me?
Let me know in case if there is any issue in adding code to validate the file size.
add_action('cred_form_validate', 'validate_image_dimensions',10,2);
function validate_image_dimensions($error_fields, $form_data) {
$forms = array( 1234 );
// Field data are field values and errors
list($fields,$errors)=$error_fields;
if (in_array($form_data['id'], $forms ) && isset($_FILES['wpcf-image']['tmp_name']) ) {
$target_min_width = 150;
$target_min_height = 150;
$check = getimagesize( $_FILES['wpcf-image']['tmp_name'] );
$file_size_uploaded=$fields['wpcf-image']['field_data']['size'];
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_min_width || $height < $target_min_height ) {
$errors['image'] = __("Your image doesn't meet the minimum dimension requirements of 150*150 pixels");
}elseif($file_size_uploaded > 6000000){
$errors['image'] = __("Your Filesize is greater than 5mb please resize and try again.");
}
}
}
$field_data = array($fields,$errors);
//return result
return $field_data;
}
2. Tried adding the images with file size more than 5 MB and it is allowing. Could you please suggest me?
Let me know in case if there is any issue in adding code to validate the file size.
This was under the impression that you wanted the maximum to be 150*150
Thank you, i've made some updates to the code and this one should work now.
// Put the code of your snippet below this comment.
add_action('cred_form_validate', 'validate_image_dimensions',10,2);
function validate_image_dimensions($error_fields, $form_data) {
$forms = array( 144 );
// Field data are field values and errors
list($fields,$errors)=$error_fields;
if (in_array($form_data['id'], $forms ) && isset($_FILES['wpcf-profile-photo']['tmp_name']) ) {
$target_min_width = 150;
$target_min_height = 150;
$check = getimagesize( $_FILES['wpcf-profile-photo']['tmp_name'] );
$file_size_uploaded=$fields['wpcf-profile-photo']['file_data']['size'];
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_min_width || $height < $target_min_height ) {
$errors['profile-photo'] = __("Your image doesn't meet the minimum dimension requirements of 150*150 pixels");
}elseif($file_size_uploaded > 6000000){
$errors['profile-photo'] = __("Your Filesize is greater than 5mb please resize and try again.");
}
}
}
$field_data = array($fields,$errors);
//return result
return $field_data;
}
.
It shouldn't allow any images where their dimensions are smaller than 150*150 or if the filesize is greater than 5mb.
Please let me know if there are any issues with this code.