Skip to content

Instantly share code, notes, and snippets.

@moskalukigor
Created July 16, 2018 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moskalukigor/86708286462fc108db159455a675e8f3 to your computer and use it in GitHub Desktop.
Save moskalukigor/86708286462fc108db159455a675e8f3 to your computer and use it in GitHub Desktop.
Gravity forms image size ( width , height ) validation
<?php
function limit_file_upload_size( $validation_result ) {
$images = json_decode(json_decode('"'.$_POST['gform_uploaded_files'].'"', true));
$form = $validation_result['form'];
foreach( $form['fields'] as &$field ){
$is_hidden = RGFormsModel::is_field_hidden( $form, $field, array() );
if( $is_hidden )
continue;
if($field["id"] == "19")
{
foreach($images->input_19 as $image)
{
$input_name = 'input_' . $field['id'];
$temp_location = RGFormsModel::get_upload_path($form['id']) . '/tmp/' . $image->temp_filename;
if ( file_exists( $temp_location ) )
$filesize = getimagesize( $temp_location );
$minimum = array('width' => '768', 'height' => '500');
$width= $filesize[0];
$height =$filesize[1];
if ($width < $minimum['width'] )
{
$validation_result['is_valid'] = false;
$field['failed_validation'] = true;
$field['validation_message'] = "Image dimensions are too small. Minimum width is {$minimum['width']}px. Uploaded image width is $width px. ( {$image->uploaded_filename} )";
}
elseif ($height < $minimum['height'])
{
$validation_result['is_valid'] = false;
$field['failed_validation'] = true;
$field['validation_message'] = "Image dimensions are too small. Minimum height is {$minimum['height']}px. Uploaded image height is $height px. ( {$image->uploaded_filename} )";
}
}
}
}
$validation_result['form'] = $form;
return $validation_result;
}
add_filter('gform_validation_1', 'limit_file_upload_size');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment