File path or url to the uploads folder to use when creating thumbnails

Hi, some time ago I had a function written for me that creates thumbnail images of the fileuploads. See below, I now need the thumbnails to go into the same folder as the uploads I can’t work out how to do it
Thanks :slight_smile:

/*create thumbnails*/
add_action( 'gform_after_submission', 'iss_gf_flash_after_submission', 10, 2 );
function iss_gf_flash_after_submission($entry, $form) {
  //Walk through the form fields and find any file upload fields
  foreach ($form['fields'] as $field) {
    if ($field->type == 'fileupload') {
      //See if an image was submitted with this entry
      if (isset($entry[$field->id])) {
        $fileurl = $entry[$field->id];

				// Get the path to the upload directory.
				$wp_upload_dir = wp_upload_dir();

				//Gravity forms often uses its own upload folder, so we're going to grab whatever location that is
				$parts = explode('uploads/', $entry[$field->id]);
				$filepath = $wp_upload_dir['basedir'].'/'.$parts[1];
				$fileurl = $wp_upload_dir['baseurl']. '/'.$parts[1];
				$filename = basename( $filepath );
				$ext = pathinfo($filename, PATHINFO_EXTENSION);
				$filesuffix = 'thumb';

				$dest_path = $wp_upload_dir['basedir'].'/gravity_forms/thumbs';
				$max_w = 170;
				$max_h = 170;
				$crop = false;

				if (strtolower($ext) == 'png' || strtolower($ext) == 'jpg' || strtolower($ext) == 'jpeg' || strtolower($ext) == 'gif') {
						//Create a custom thumbnail in the GF thumbs directory with suffix '-thumb'
					$thumbname = $wp_upload_dir['basedir'].'/gravity_forms/thumbs/'.str_replace('.'.$ext, '', $filename).'-thumb.'.strtolower($ext);

					$image = wp_get_image_editor( $filepath );
					var_dump($image);
					if ( ! is_wp_error( $image ) ) {
					    $image->resize( $max_w, $max_h, $crop );
					    $image->save( $thumbname );
					}
					
					} else if(strtolower($ext) == 'psd' || strtolower($ext) == 'tif' || strtolower($ext) == 'tiff') {
					$thumbname = $wp_upload_dir['basedir'].'/gravity_forms/thumbs/'.str_replace('.'.$ext, '', $filename).'-thumb.jpg';

					$image = wp_get_image_editor( $filepath );
					var_dump($image);
					if ( ! is_wp_error( $image ) ) {
					    $image->resize( $max_w, $max_h, $crop );
					    $image->save( $thumbname );
				}
		
				
				} 

      }
    }
  }
};

You can use this to get the current upload path for your form:

$form_upload_root = GF_Field_FileUpload::get_upload_root_info( $form['id'] );

That would return an array like this:

(
    [path] => /var/www/public_html/wp-content/uploads/gravity_forms/3607-a558b73f9c31ba912c47174193fa3d03/2022/09/
    [url] => http://example.com/wp-content/uploads/gravity_forms/3607-a558b73f9c31ba912c47174193fa3d03/2022/09/
    [y] => 2022
    [m] => 09
)

So you can update your snippet to use $form_upload_root['path'] to store your thumbnails.

1 Like

Thanks so much Samual I’ll give it a go