Skip to content

Instantly share code, notes, and snippets.

@fridaynext
Created August 16, 2016 01:43
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 fridaynext/6417368b89f93a7fe25eafa905ebe4c4 to your computer and use it in GitHub Desktop.
Save fridaynext/6417368b89f93a7fe25eafa905ebe4c4 to your computer and use it in GitHub Desktop.
<?php
//resize images uploaded to Gravity Form 1.
add_action("gform_after_submission_1", "gf_resize_images", 10, 2);
function gf_resize_images($entry, $form)
{
if( $url = pathinfo( $entry[18] ) ) {
$image = wp_get_image_editor($url);
if ( ! is_wp_error( $image ) )
{
//replace 640,480 with desired dimensions. false indicates not to crop image.
$image->resize( 1000,750,false );
$image->set_quality();
$image->save($url);
}
}
if( $url = pathinfo( $entry[21] ) ) {
$image = wp_get_image_editor($url);
if ( ! is_wp_error( $image ) )
{
//replace 640,480 with desired dimensions. false indicates not to crop image.
$image->resize( 1000,750,false );
$image->set_quality();
$image->save($url);
}
}
if( $url = pathinfo( $entry[22] ) ) {
$image = wp_get_image_editor($url);
if ( ! is_wp_error( $image ) )
{
//replace 640,480 with desired dimensions. false indicates not to crop image.
$image->resize( 1000,750,false );
$image->set_quality();
$image->save($url);
}
}
}
@hughc
Copy link

hughc commented Oct 19, 2017

Thanks for this, but it didn't work for me - the values in my $entry object were URLs, so pathinfo() didn't work. Here's my refactored version, that does a generic job based off form and field names (no need o remember ids):

//resize images uploaded to Gravity Forms
function setup_image_resizing() {
    $form_id_map = array(
        'business' => "User Profile Editor (Business)",
        'farmer' =>'User Profile Editor (Farmer)',
    );
    if (class_exists('RGFormsModel')) {
        foreach ($form_id_map as $user_type => $form_name) {
            $form_id = RGFormsModel::get_form_id($form_name);
            add_action("gform_after_submission_" . $form_id, "gf_resize_profile_images", 10, 2);
            add_filter("gform_upload_path_" . $form_id, "change_upload_path", 10, 2);
        }
    }
}
add_action("init", "setup_image_resizing");


function gf_resize_profile_images($entry, $form)
{   
    $field_map = array(
        "Profile Image" => array(
            "width" => 300,
            "height" => 300,
            "hard_crop" => true,
        ),
    );

    // iterate our target fields
    foreach ($field_map as $field_name => $values) {
        // and compare to all fields in the $form definition passed in
        foreach ($form["fields"] as $field_index => $field) {
            // lookup based on label
            if ($field->label == $field_name && $field->type == 'fileupload') {
                if( $url =  $entry[$field->id]) {
                    $path = get_home_path() . parse_url($url)['path'] ;
                    $image = wp_get_image_editor($path);
                    if ( ! is_wp_error( $image ) )
                    {
                        $image->resize( $values['width'],$values['height'], $values['hard_crop'] );
                        $image->set_quality();
                        $image->save($path);
                    } else {
                        // an error occured
                    }
                }
                
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment