Skip Navigation

[Resolved] Set maximum size of featured image when uploading via front end (cred form)

This support ticket is created 5 years, 3 months ago. 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.

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

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by pascalH 5 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#1357443
Knipsel.JPG

Hello Nigel

I notice that users of the website sometimes upload far too small images, as a result, the pages do not look good.
So I tried to set a minimum height and width for the featured image, so that an image that is too small is rejected.
As help I used the ticket below, I have adjusted the ID number in the code to the ID number of the form of my site, but it doesn't work in some way.

The Form ID number is: 349
The minimum width should be: 500 px x 300 px

Would you like to take a look and help me?

https://toolset.com/forums/topic/form-with-image-resolution-check/

The code I add to functions.php

function validate_featured_image_size( $field_data, $form_data ){

$form_id = array( 349 ); // add IDs of CRED forms

$target_width = 500; // Edit
$target_height = 300; // 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'] = "Image wrong size";
}
}

$field_data = array($fields,$errors);
}

return $field_data;
}
add_action( 'cred_form_validate', 'validate_featured_image_size', 10, 2 );

#1357683

Hello,

I suggest you try these:
1) Edit the post form, in section "Settings", enable option "Use the WordPress Media Library manager for image, video, audio, or file fields"
2) Follow our document to use another filter hook "cred_form_ajax_upload_validate" to do the image validation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate

See the solution of another similar thread here:
https://toolset.com/forums/topic/featured-image-size-validation-in-form/

For your reference.

#1360049

My issue is resolved now. Thank you!