Skip Navigation

[Resolved] set the minimum resolution of image upload

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.

This topic contains 5 replies, has 3 voices.

Last updated by janO-3 6 years, 4 months ago.

Assigned support staff: Waqas.

Author
Posts
#265171

I am trying to: set the minimum resolution of image upload

I visited this URL: not provided

I expected to see: error message when user upload image less than 300x300pixels

Instead, I got: "user can upload less than 300x300 pixels image"

#265274

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

You can use cred_form_validate filter of CRED API, as below:

add_filter('cred_form_validate_99', 'cred_image_resolution_validation',10,2);
function cred_image_resolution_validation($field_data, $form_data)
{
    // Field data are field values and errors 
    list($fields,$errors)=$field_data;
  
    //Run the rest of code for this CRED ONLY and IF the file is upload type and size are set.
    if (( 99 == $form_data['id']) && (isset($_FILES['wpcf-the-file-field']['tmp_name']))) {
        $check = getimagesize($_FILES["wpcf-the-file-field"]["tmp_name"]);
		
		if($check !== false) {
			$width = $check[0];
			$height = $check[1];
			
			if($width < 300 && $height < 300) {
				$errors['the-file-field'] = 'Sorry, too small image.';
			}
		} else {
			$errors['the-file-field'] = 'Sorry this is not a valid image file.';   
		}
    }

    return array($fields,$errors);
}

Please add this code in your theme's functions.php and adjust accordingly. Specially the "99" should be replaced with your CRED Form's ID, while "wpcf-the-file-field" should be replaced with the actual field name.

You can find more information about CRED API hooks at https://toolset.com/documentation/user-guides/cred-api/#cfv

And find information about PHP's getimagesize() at hidden link

Please let me know if I can help you with anything related.

#268701

Hi, I am busy recently, but I try the code and it is not working.

in my cred form, I use the field below:

<div class="cred-field cred-field-post-featured-image" id="add-post-featured-image-width">
<div class="cred-label">????圖片*</div>
[cred_field field="post-featured-image" post="post" value="" urlparam=""]
</div>

so, "wpcf-the-file-field" should be replaced by "post-featured-image" , right?

Then, how about "the-file-field" , should I replace it to sth?

#268774

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

For $_FILES["wpcf-the-file-field"] it should have a prefix of "wpcf-", while for references in CRED and errors array, it is fine without that.

Anyhow, can you please send me temporary access to your site, with URL to your view, cred form and relevant page? So I can check for the issue. Also the functions.php should be writable, to make changes in the relevant filter function.

I have enabled your next reply as Private, please input all information in that area.

#269071

Thanks! the code is working!

#301639

Thank you for helping answer this application question. One question on this RESOLVED post:

add_filter('cred_form_validate_99', 'cred_image_resolution_validation',10,2);

What is the 10,2 reference? Thanks.