# Setting specific image size for specific form upload file field

When you upload an image through a form's "upload file" field type to the Wordpress media library - Wordpress automatically generates three copies with three different sizes by default.

Now I know how to tweak those sizes globally.

My Question Is:

Is it possible to set different sizes for specific fields?

For Example:

I have a form with two "upload file" fields. when the user uploads an image for the first field I want wordpress to generate only one image with "medium" size (or 300*300 max).

And when he uploads an image for the second field I want wordpress to generate only one image with the "large" size (or 640*640 max).

Any ideas? Thanks.

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. • Great answer. my problem is i use "formidablepro" plugin for the form and i do attach the file to a post (the form creates a new post) so i'm not really sure how to implement it... – Ben Mar 1 '16 at 14:11 • Ok so where are you running this code? On a formidable pro hook? Or...? There should be a$post_id of some sort available to you if so... that would allow you to do media_handle_upload($name,$post_id). I'd need more info to help further... – Adam Mar 1 '16 at 14:49
• Thanks. i will play with that and let you know. Thank you very very much! – Ben Mar 2 '16 at 10:25