skip to Main Content

I put an avatar uploader on the site, I want it to be cut to 3 sizes (original, 32 x 32 and 96 x 96) after the image is uploaded. So I defined 2 sizes as below :

add_image_size( 'avatar_scale_32x32', 32, 32, false );
add_image_size( 'avatar_scale_96x96', 96, 96, false );

My site has other sizes that I have defined for other uploaders, Now I want to specify that if an image is uploaded from this uploader it will crop only the sizes that are related to it. name="wpua-file".

This is the code I use, but it cuts the image to all sizes, not the desired sizes.

function remove_default_img_sizes( $sizes ) {
    $targets = ['avatar_scale_32x32', 'avatar_scale_96x96'];
    foreach( $sizes as $size_index => $size ) {
        if ( $_FILES['wpua-file'] ) {
            if( in_array( $size, $targets ) ) {
                unset( $sizes[$size_index] );
            }
        }
    }
  return $sizes;
} add_filter( 'intermediate_image_sizes', 'remove_default_img_sizes', 10, 1 );

Where am I doing wrong?

2

Answers


  1. Chosen as BEST ANSWER

    The code I put in the question, if I upload an image from the target uploader, it creates all the sizes except the sizes inside the $targets array. If I want it to be the opposite, that is, it disables all sizes other than the sizes in the $targets array when uploading and creates only 2 sizes in the $targets array. in_array( $size, $targets ) If the sizes are other than the 2 sizes in the $targets array, it returns the false value, then 2 correct values ​​are returned according to the 2 sizes in the $targets array. That's why we say include values ​​that are false :

    function remove_default_img_sizes( $sizes ) {
        $targets = ['avatar_scale_32x32', 'avatar_scale_96x96'];
        foreach( $sizes as $size_index => $size ) {
            var_dump( in_array( $size, $targets ) );
            if ( $_FILES['wpua-file'] ) {
                if ( in_array( $size, $targets ) === false ) {
                    unset( $sizes[$size_index] );
                }
            }
        }
      return $sizes;
    } add_filter( 'intermediate_image_sizes', 'remove_default_img_sizes', 10, 1 );
    

  2. It seems like the issue lies in the condition where you are checking if $_FILES[‘wpua-file’] is set. The $_FILES superglobal is used to access uploaded file information, but it may not be set or available in the context you are using it (such as when you’re working with WordPress image sizes).

    To achieve the desired behavior, you can modify the approach by checking if the image is being uploaded via the avatar uploader based on the input name. Here’s the corrected code:

    
        function remove_default_img_sizes( $sizes ) {
            $targets = ['avatar_scale_32x32', 'avatar_scale_96x96'];
            $uploaded_file_name = 'wpua-file'; // Update this to the actual input name for your avatar uploader.
        
            // Check if the image is being uploaded via the avatar uploader.
            if ( isset( $_POST['action'] ) && $_POST['action'] === $uploaded_file_name ) {
        foreach ( $sizes as $size_index => $size ) {
            if ( in_array( $size, $targets ) ) {
        unset( $sizes[$size_index] );
            }
        }
            }
        
            return $sizes;
        }
        
        add_filter( 'intermediate_image_sizes', 'remove_default_img_sizes', 10, 1 );
    
    

    In this code, we check if the $_POST[‘action’] matches the name of your avatar uploader input ($uploaded_file_name). If it does, we proceed to unset the sizes that are not desired ($targets), and if not, we leave the sizes unchanged.

    Remember to replace ‘wpua-file’ in the code with the actual name attribute of your avatar uploader input field. This way, the image will be cropped only for the desired sizes when uploaded via the avatar uploader, and other uploaders will keep their defined sizes intact.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search