WordPress : two sets of add_image_size(); depending on image width?

I just started to optimize the scaling of the images i upload on my wordpress (via Media tool). After a bit of research i tried to add this code i’ve found in my functions.php file and it’s working just fine :

add_theme_support( ‘post-thumbnails’ );

add_action( ‘after_setup_theme’, ‘aw_custom_add_image_sizes’ );
function aw_custom_add_image_sizes() {

add_image_size( ‘og’, 500, 9999 );
add_image_size( ‘xxl’, 400, 9999 );
add_image_size( ‘xl’, 300, 9999 );
add_image_size( ‘l’, 250, 9999 );
add_image_size( ‘m’, 200, 9999 );
add_image_size( ‘s’, 150, 9999 );
add_image_size( ‘xs’, 100, 9999 );
add_image_size( ‘xxs’, 50, 9999 );
add_image_size( ‘xxs’, 40, 9999 );
add_image_size( ‘xxs’, 30, 9999 );
add_image_size( ‘xxs’, 20, 9999 );
add_image_size( ‘xxs’, 10, 9999 );
}

I also figured out the srcset part.

But now i’m wondering if there is a way to have two different sets of ” add_image_size depending on the width of the image that is being uploaded ? What i’m looking for is something like :

1 – If the image i tried to upload is more than 150px wide, then use this set :

add_image_size( ‘small’, 150, 9999 );
add_image_size( ‘medium’, 100, 9999 );
add_image_size( ‘medium’, 80, 9999 );
etc.

2 – If the image’s width is less or equal to 150px, use that set :

add_image_size( ‘small’, 150, 9999 );
add_image_size( ‘medium’, 100, 9999 );
add_image_size( ‘medium’, 80, 9999 );
etc.

The goal i’m trying to achieve is obviously to avoid very small files on images that are destined to be big. As i’m a beginner in coding this might not be the right way to do it and i’m open to any suggestion !

Thank you

Read more here:: WordPress : two sets of add_image_size(); depending on image width?

Leave a Reply

Your email address will not be published. Required fields are marked *