Hi,
I was using this function for validation of the featured image in the Toolset Forms. The form is not used so often, so I don't know when it actually started to fail. What could be causing the problem shown on the attached image?
function validate_featured_image_size( $field_data, $form_data ){
$form_id = array( 6352,6358 ); // add IDs of CRED forms
$target_width = 260; // Edit
$target_height = 330; // Edit
if ( in_array( $form_data['id'], $form_id ) ) {
// Split field data into field values and errors
list( $fields,$errors ) = $field_data;
$check = getimagesize( $fields['_featured_image']['value'] );
if ( $check !== false ) {
$width = $check[0];
$height = $check[1];
if ( $width != $target_width || $height != $target_height ) {
$errors['_featured_image'] = "Obrázek nemá uvedené rozměry";
}
}
$field_data = array($fields,$errors);
}
return $field_data;
}
add_action( 'cred_form_validate', 'validate_featured_image_size', 10, 2 );
Hello there,
The issue seems to be that the PHP function 'getimagesize' expects the param to be an actual object and in the error log you shared this parameter is empty.
I deduct this happens when you try to submit the form without the featured image, then getimagesize will try to check the size of null and error out.
Can you please try to adjust the code a little bit to look like this:
function validate_featured_image_size( $field_data, $form_data ){
$form_id = array( 6352,6358 ); // add IDs of CRED forms
$target_width = 260; // Edit
$target_height = 330; // Edit
if ( in_array( $form_data['id'], $form_id ) ) {
// Split field data into field values and errors
list( $fields,$errors ) = $field_data;
$check = getimagesize( $fields['_featured_image']['value'] );
if ( empty($fields['_featured_image']['value']) === false ) {
$width = $check[0];
$height = $check[1];
if ( $width != $target_width || $height != $target_height ) {
$errors['_featured_image'] = "Obrázek nemá uvedené rozměry";
}
}
$field_data = array($fields,$errors);
}
return $field_data;
}
add_action( 'cred_form_validate', 'validate_featured_image_size', 10, 2 );
I added a check to see if the field _featured_image is set before checking for the size.
Give it a try and tell us the results.
Best,
Hi Mateus,
yes, I can confirm, that it happens when I try to submit the form without the featured image. I tried the adjusted code, but there still seems to be some problem.
Hey there,
I put the check a bit to late, please try the following code, it should resolve the issue:
function validate_featured_image_size( $field_data, $form_data ){
$form_id = array( 6352,6358 ); // add IDs of CRED forms
$target_width = 260; // Edit
$target_height = 330; // Edit
if ( in_array( $form_data['id'], $form_id ) ) {
// Split field data into field values and errors
list( $fields,$errors ) = $field_data;
if ( !empty($fields['_featured_image']['value']) ) {
$check = getimagesize( $fields['_featured_image']['value'] );
if ( $check !== false ) {
$width = $check[0];
$height = $check[1];
if ( $width != $target_width || $height != $target_height ) {
$errors['_featured_image'] = "Obrázek nemá uvedené rozměry";
}
}
}
$field_data = array($fields,$errors);
}
return $field_data;
}
add_action( 'cred_form_validate', 'validate_featured_image_size', 10, 2 );
Thank you.
Hi Mateus,
perfect, it solved my problem. Thank you a lot!