Limit file size and image dimensions in Gravity Forms upload fields

Update 31.05.2013

I’ve put together a plugin to do what the snippet above used to do. I believe it’s far more bug free than its predecessor so please use it instead. Plugin should work with Gravity Forms 1.6.2+ so no guaranties for earlier versions!

gravity-forms-upload-rules-01

gravity-forms-upload-rules-02

Download Gravity Forms Upload Rules

Original Post

There are already a couple of projects that needed file upload functionality (image upload to be specific) so we needed a solution for file size and image dimensions limitations. To be honest I was quite surprised when I found out GravityForms isn’t offering such functionality by far. It’s offering great hooks though.

function public_news_image_validation( $valid_obj ) {
  if ( empty( $_FILES ) ) return $valid_obj;
  $form_id = $valid_obj['form']['id'];
  $formFields = &$valid_obj['form']['fields'];
  $page_number = GFFormDisplay::get_source_page( $form_id );
  foreach ( $formFields as &$field ) {
  if ( $page_number > 0 && $field["pageNumber"] != $page_number ) continue; //paged form scenario - only validate fields that are on current page
    if ( in_array( 
        ( $field_type = RGFormsModel::get_input_type($field) ), 
        array( 'fileupload', 'post_image' ) 
      ) 
    ) {

      $field_id = $field['id'];
      $input_name = 'input_' . $field_id;

      if ( $field['failed_validation'] ) return $valid_obj;
      $file_info = RGFormsModel::get_temp_filename( $form_id, $input_name );
      $temp_filepath = RGFormsModel::get_upload_path($form_id) . "/tmp/" . $file_info["temp_filename"];

      if ( file_exists( $temp_filepath ) ) {
        $bytes = filesize( $temp_filepath );
        $dims = @getimagesize( $temp_filepath );
      }
      elseif ( isset( $_FILES[$input_name] ) && !empty( $_FILES[$input_name] ) )  {
        $bytes = $_FILES[$input_name]['size'];
        $dims = @getimagesize( $_FILES[$input_name]['tmp_name'] );
      } 
      else return $valid_obj;
      $max_size = 1024*300; // 300kb
      if ( $bytes > $max_size ) {

        unset( RGFormsModel::$uploaded_files[$form_id][$input_name] );
        unset( $_FILES[$input_name] );
        $field['failed_validation'] = true;
        $field['validation_message'] = sprintf( __( 'File upload size (%s) exceeded', 'gravityforms' ), $max_size/1024 . 'kb' );
        $valid_obj['is_valid'] = false;
        return $valid_obj;
      }
      if ( ! empty( $dims ) && is_array( $dims ) ) {
        list( $up_width, $up_height ) = $dims;
        if ( min( $up_width, $up_height ) < 300 ) { //smallest image size should be at least 300px

          unset( RGFormsModel::$uploaded_files[$form_id][$input_name] );
          unset( $_FILES[$input_name] );
          $field['failed_validation'] = true;
          $field['validation_message'] = sprintf( __( 'Image should be at least %s', 'gravityforms' ), '300x300 (px)' );
          $valid_obj['is_valid'] = false;
          //return $valid_obj;
        }
      }
    }
  }
  return $valid_obj;
}

add_action( 'gform_validation_3', 'public_news_image_validation' );

EDIT: Added paged forms compatibility.

24 Responses to “Limit file size and image dimensions in Gravity Forms upload fields”

  1. ricbax

    What file does this get edited in?

    Reply
    • zlatev

      Your theme’s functions.php. Also notice the action name gform_validation_3. Change number to your form id or just use the global one – gform_validation for all forms.

      Reply
  2. foxydot

    This is EXACTLY what I needed. You so rock my world today. After three all nighters in a row, I was dreading working out this one piece of the puzzle and you may have saved me.

    Reply
  3. Mark Grothman

    Any idea on how to get this to work on a custom field input? It is a File Upload input but it has to be a custom field so we can assign it a custom field name. Can’t seem to get it to function. Thanks!

    Reply
  4. Mark Grothman

    I realized it is based on fileupload type isn’t working, only post_image. Has nothing to do with the custom field.

    Reply
  5. Andy Jonathan

    Could you please modify the code so that only .jpg , .png, .gif files can be uploaded?

    don’t want users to be able to upload anything else other than images! safety and security issue..

    many thanks
    Andy

    Reply
  6. Michael

    Thank you!

    Reply
  7. Kyle

    Thanks so much for this. Much appreciated.

    Reply
  8. aimkbe

    you are AWESOME!!!!!

    Reply
  9. pablo

    hi zlatev, can i explain me how do i use this function, because i add this code into functions.php and change to global var “gform_validation” but nothing happened, i have to call the function in some place in my wordpress? thank you in advance!

    Reply
  10. gayatriom

    Yes. Same here. I added it to function.php and doesn’t see any effect.

    Reply
    • zlatev

      Sorry for not being able to provide a further support on this code. It’s not supposed to just work for everyone without editing. Its purpose is to give the right direction to accomplish this kind of validation.

      Reply
  11. marxcarl

    Thanks for sharing this, exactly what I was looking for. Nicely coded, very easy to understand.

    Reply
  12. Dasha (@dashaluna)

    Hello, Thanks for the great code! Clean and easy to understand, exactly what I was looking for.

    I’m a bit confused about the $max_size = 1024*300; //300kb maths. Can we have $max_size = 300 without the dimensions as they aren’t checked for the max size, or does it have to be the width x height? Could you explain why.

    Many thanks,
    Dasha

    Reply
    • zlatev

      $max_size needs to be in bytes and 300kb = 1024 bytes x 300.

      Reply
  13. Gurung

    Hey @zzlatev, thanks for the awesome piece of code everything works fantastic. I gotta tiny issue though. When a bigger size image is rejected a nice notification appears “File upload size (1024kb) exceeded”. When I browse and select another picture in the same field in an attempt to replace the rejected image with another resized image and then finally hit the next button (my form is paged). The notification still remains the same and it wont accept the new image. Kinldy suggest changes if any in the code.

    I generally do a copy paste so am pretty clueless. Please help, Kindly.

    Reply
  14. warhursttw10

    I have been using this function for quite some time now and it for the most part works great.

    I know you probably aren’t still maintaining it or updating when errors are spotted but I figured I’d share what happens in my scenario. I have multiple input fields and when the user uploads multiple files that are too big the first field displays an error message as it should and is cleared of its file. The remaining fields still hold their uploaded files and display an error message with a delete button next to them. When you click “delete” to remove the files the field says “upload file” again as if it is empty, however if you hit submit the field throws an error again and the file is back in the field. So in essence I can get the function to work for one upload field but when there are multiple in a row only the first one works.

    If anyone has thoughts on this please share.

    Reply
  15. Gurung

    I explained an issue earlier with the previous hack. This plugin seems to have taken taken care of that ( although I had to necessarily set the image dimensions to conditional and provide specific dimensions as it would still repeat the same error on image resubmit when only the FileSize Limit is set ). But the issue is solved anyways so who cares. I am sure there are tons and tons of people who will benefit for this solution.

    A million thanks to @zzlatev for being such a gentleman and provide the hack/plugin for free. But people, be nice and donate. Make his time worth.

    Reply
    • zlatev

      @Gurung Glad to hear your issue is resolved. Hopefully it will resolve others’ too.

      The sooner I got some feedback I would be able to deliver more stable release.

      Reply
  16. Kyle

    I’ve used this in the past and really have appreciated the great work you did with this code. Just wanted to give you a heads up that GF just released a new multi-file upload field in the recent 1.8 beta release that will need an update for this. I’m trying to adapt it now and will post any results.

    Reply
  17. Vahid

    Hi.

    Please make your plugin 5.6 and 7.0 compatible

    Reply
  18. dan

    Thanks, your plugin seems to work well as of wordpress 4.7.3

    Reply

Leave a Reply to pablo Cancel reply