Setting specific image size for specific form upload file field

Here’s an example of how to unset the standard WordPress sizes or any size for that matter assuming you know the image size name and of course you can simply inpect the $sizes array to determine what to unset…

function wpse219360_disable_intermediate_image_sizes($sizes) {  

    if ( isset($_FILES['file_input_1']) && $_FILES['file_input_1']['error'] != UPLOAD_ERR_NO_FILE ) {
        unset($sizes['thumbnail']);  
        unset($sizes['medium']);  
        unset($sizes['large']);  
    }

}  

add_filter('intermediate_image_sizes_advanced', 'wpse219360_disable_intermediate_image_sizes' );  

So assuming you are uploading files from a custom form and assuming that you are NOT attaching the images to a post you’d want to call media_handle_upload() for each of your respective form fields in your logic.

Your form processing logic might look like this…

foreach(array('custom_file_1', 'custom_file_2') as $input_name) {

    switch ($input_name) {

        case 'custom_file_1':

            add_filter('intermediate_image_sizes_advanced', function($sizes) {
                return array('medium');
            });

            break;

        case 'custom_file_2':

            add_filter('intermediate_image_sizes_advanced', function($sizes) {
                return array('large');
            });

            break;

    }


    /**
     * Pass the $input_name to media_handle_upload() which internally takes 
     * the given $input_name and looks for the corresponding value on the 
     * $_FILES superglobal...
     *
     * Pass 0 as the second parameter if you are not attaching the upload to
     * a given post, if you are attaching it to a post, then you must pass the
     * post's ID in which you want to attach the image too
     */
    $result = media_handle_upload($input_name, 0);

}

The example above assumes you already have a handle on your form data.

At the point in which you wish to upload your files, we iterate over an array of prefined input names that we are expecting from our form, in this case as an example I am using the input names custom_file_1 and custom_file_2

These would correspond to your form HTML, for example:

<input type="file" name="custom_file_1"/>
<input type="file" name="custom_file_2"/>

Note #1

The foreach loop can be improved, made simpler, however for the case of this example I’ve left it more verbose in hope that it better explains what is going on internally.

So to keep it DRY…

$inputs = array(
    'custom_file_1' => 'medium',
    'custom_file_2' => 'large',
);

foreach($inputs as $name => $size) {

    add_filter('intermediate_image_sizes_advanced', function($sizes) {
        return array($inputs[$name]);
    });

    $result = media_handle_upload($name, 0);

}

Note #2

You may need to re-hook the original images sizes onto intermediate_image_sizes_advanced filter after (outside) of the foreach loop to ensure that any subsequent calls to media_handle_upload() within the same request that originate from you and or from somewhere else do not have their expected generated image sizes modified.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)