Skip Navigation

[Resolved] CRED form limit image width and height errors

This support ticket is created 4 years, 11 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 5 replies, has 2 voices.

Last updated by alinaB 4 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#1469917

CRED create and edit form. I am trying to limit, along with the image size in kilobytes, its width and height. I use the following code:

/**
 * This snippent tries to limit: image upload size, image upload width, image upload height.
 */

toolset_snippet_security_check() or die( 'Accesul diect nepermis' );
function md_validate_image_size_resolution( $field_data, $form_data ){

    $form_id = array( 266, 704 ); // add IDs of CRED forms

    //$target_size = 500000; // adjust the allowed size

    if ( in_array( $form_data['id'], $form_id ) ) 
    {

        // Split field data into field values and errors
        list( $fields,$errors ) = $field_data;
	//image in bytes allowed maximum dimension
        $target_allowed_size = 500000;
      //featured image allowed dimensions
      	$featured_image_min_width = 1920;
      	$featured_image_max_width = 2200;
        $featured_image_min_height = 600;
      	$featured_image_max_height = 1080;
      //gallery image allowed dimensions
      	$gallery_image_min_width = 960;
      	$gallery_image_max_width = 1600;
        $gallery_image_min_height = 540;
      	$gallery_image_max_height = 900;
      	$check =  getimagesize($fields['_featured_image']['value']) ;//tried also getimagesize($fields['_featured_image']['value'][''tmp_data])
        if($fields['_featured_image']['file_data']['size'] > $target_allowed_size || $width < $featured_image_min_width || $width > $featured_image_max_width ||  $height < $featured_image_min_height || $height > $featured_image_max_height)
        {          
          $fmsgdim='';$fmsgminw ='';$fmsgmaxw ='';$fmsgminh ='';$fmsgmaxh = '';
         if($fields['_featured_image']['file_data']['size'] > $target_allowed_size){
         $fmsgdim = "Your filesize is ". $fields['_featured_image']['file_data']['size'] / 1000 ." kB, bigger than the acceptex maximum size of " . $target_allowed_size/1000 . ' kB; <br/>';
         }
        if($check[0] < $featured_image_min_width )
        {
            $fmsgminw =  "Width too small (". check[0] ."). Minimum accepted: " . $featured_image_min_width .'px;  <br/>';
          	print_r($check);
        }
         if($check[0] > $featured_image_max_width )
        {
            $fmsgmaxw =  "Width too big (". check[0] ."). Maximum accepted: " . $featured_image_max_width .'px;  <br/>';
        }
        if($check[1] < $featured_image_min_height )
        {
            $fmsgminh =  "Height too small (". check[1] ."). Minum acceptat: " . $featured_image_min_height .'px;  <br/>';
        }
         if($check[1] > $featured_image_max_height )
        {
            $fmsgmaxh =  "Height too big (". check[1] ."). Minimum accepted: " . $featured_image_max_height .'px;  <br/>';
        }
            $errors['_featured_image'] =  $fmsgdim . $fmsgminw . $fmsgmaxw . $fmsgminh . $fmsgmaxh;
        }        

}

return array($fields,$errors);
  
}


add_action( 'cred_form_validate', 'md_validate_image_size_resolution', 10, 2 );

The image size check works well, it does not allow the saving to be performed if filesize is greater than the value I impose.

The width and height check, by contrary, do not perform well.
What seems to be malfunctioning is:

$check =  getimagesize($fields['_featured_image']['value']) ;

I have also tried:

$check =  getimagesize($fields['_featured_image']['value']['tmp_name']) ;
$check =  getimagesize($fields['_featured_image']['field_data']['tmp_name']) ;
$check =  getimagesize($fields['_featured_image']['tmp_name']) ;

and so on.

Please guide me. I was following https://toolset.com/forums/topic/ajax_upload_validate-for-repetitive-images/. Tried all variations. Reached the end of my options. I am sure I miss something here, but what?

#1470809

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please share the problem URL where you added the form as well as access details so that I can check what's going wrong
there.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1472659

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thank you for sharing the access details.

I've adjusted the code to "Custom Code" section with "toolset-restrict-images" as given under:
=> hidden link

function md_validate_image_size_resolution( $field_data, $form_data ){

    $form_id = array( 266, 704 ); // add IDs of CRED forms

    $target_size = 500000; // adjust the allowed size

    if ( in_array( $form_data['id'], $form_id ) ) {

        // Split field data into field values and errors
       list( $fields,$errors ) = $field_data;
	//image in bytes allowed maximum dimension
        $target_allowed_size = 500000;
      
      //featured image allowed dimensions
      	$featured_image_min_width = 1920;
      	$featured_image_max_width = 2200;
        $featured_image_min_height = 600;
      	$featured_image_max_height = 1080;
      
      
      //gallery image allowed dimensions
      	$gallery_image_min_width = 960;
      	$gallery_image_max_width = 1600;
        $gallery_image_min_height = 540;
      	$gallery_image_max_height = 900;
      
      
       $fimg = $fields['_featured_image']['file_data']['tmp_name'] ;
       
      	$check = getimagesize($fimg); 
      
        if($fields['_featured_image']['file_data']['size'] > $target_allowed_size || $width < $featured_image_min_width || $width > $featured_image_max_width ||  $height < $featured_image_min_height || $height > $featured_image_max_height)
        {          
          $fmsgdim='';$fmsgminw ='';$fmsgmaxw ='';$fmsgminh ='';$fmsgmaxh = '';
         if($fields['_featured_image']['file_data']['size'] > $target_allowed_size){
         $fmsgdim = "Dimensiunea fișierului este de ". $fields['_featured_image']['file_data']['size'] / 1000 ." kB, mai mare decât maximul acceptat de " . $target_allowed_size/1000 . ' kB; <br/>';
         }
          
          
        if($check[0] < $featured_image_min_width )
        {
            $fmsgminw =  "Lățime prea mică (". check[0] ."). Minum acceptat: " . $featured_image_min_width .'px;  <br/>';
          	echo $fimg;
        }
         if($check[0] > $featured_image_max_width )
        {
            $fmsgmaxw =  "Lățime prea mare. Maxim acceptat: " . $featured_image_max_width .'px;  <br/>';
        }
        if($check[1] < $featured_image_min_height )
        {
            $fmsgminh =  "Înălțime prea mică. Minum acceptat: " . $featured_image_min_height .'px;  <br/>';
        }
         if($check[1] > $featured_image_max_height )
        {
            $fmsgmaxh =  "Înălțime prea mare. Maxim acceptat: " . $featured_image_max_height .'px;  <br/>';
        }
            $errors['_featured_image'] =  $fmsgdim . $fmsgminw . $fmsgmaxw . $fmsgminh . $fmsgmaxh;
        }
        
      		
    if (isset($_POST['wpcf-imagine-galerie-foto-1']))
    {
        if($fields['wpcf-imagine-galerie-foto-1']['file_data']['size'] > $target_allowed_size)
        {
            $errors['wpcf-imagine-galerie-foto-1'] = "Dimensiune prea mare. Maxim acceptat: 500kB";
        }
    }
    if (isset($_POST['wpcf-imagine-galerie-foto-2']))
    {
        if($fields['wpcf-imagine-galerie-foto-2']['file_data']['size'] > $target_allowed_size)
        {
            $errors['wpcf-imagine-galerie-foto-2'] = "Dimensiune prea mare. Maxim acceptat: 500kB";
        }
    }

    if (isset($_POST['wpcf-imagine-galerie-foto-1']))
    {
        if($fields['wpcf-imagine-galerie-foto-3']['file_data']['size'] > $target_allowed_size)
        {
            $errors['wpcf-imagine-galerie-foto-3'] =  "Dimensiune prea mare. Maxim acceptat: 500kB";
        }
    }

    if (isset($_POST['wpcf-imagine-galerie-foto-4']))
    {
        if($fields['wpcf-imagine-galerie-foto-4']['file_data']['size'] > $target_allowed_size)
        {
            $errors['wpcf-imagine-galerie-foto-4'] =  "Dimensiune prea mare. Maxim acceptat: 500kB";
        }
    }

    if (isset($_POST['wpcf-imagine-galerie-foto-5']))
    {
        if($fields['wpcf-imagine-galerie-foto-5']['file_data']['size'] > $target_allowed_size)
        {
            $errors['wpcf-imagine-galerie-foto-5'] =  "Dimensiune prea mare. Maxim acceptat: 500kB";
        }
    }

    if (isset($_POST['wpcf-imagine-galerie-foto-6']))
    {
        if($fields['wpcf-imagine-galerie-foto-6']['file_data']['size'] > $target_allowed_size)
        {
            $errors['wpcf-imagine-galerie-foto-6'] =  "Dimensiune prea mare. Maxim acceptat: 500kB";
        }
    }
}

return array($fields,$errors);
  
}
add_action( 'cred_form_validate', 'md_validate_image_size_resolution', 10, 2 );

There were the following issues that I fixed:
- first, I've activated the snippet as it was inactive
- then, I've changed the hook name from cred_save_data to cred_form_validate.
- then, I've changed/adjusted the following line of code from:

    $fimg = $fields['_featured_image']['value'] ;

To

   $fimg = $fields['_featured_image']['file_data']['tmp_name'] ;

I can see now validation works for featured image. Can you please confirm.

#1474863

I confirm that it is working. Thank you!

#1474885

Just one last question: would it be possible (if not too difficult to implement) to clear the "to-be-uploaded" image from there if the submission fails? Because if a user tries the second time to upload the form, replacing the image in discussion, the image still shows there but the new error message is different?

#1474953

My issue is resolved now. Thank you!