Right before image subsizes are created, there’s a filter called intermediate_image_sizes_advanced
that lets you modify the list of sizes.
Here’s code that you could expand on — right now it just removes the thumbnail as a proof of concept:
function remove_image_sizes_before_generation( $new_sizes, $image_meta, $attachment_id ) {
$width = $image_meta[0];
$height = $image_meta[1];
// ... Calculate aspect ratio and remove the image sizes that aren't relevant
unset( $new_sizes['thumbnail'] );
return $new_sizes;
}
add_filter( 'intermediate_image_sizes_advanced', 'remove_image_sizes_before_generation', 10 , 3 );
The nice thing about doing it this way is that the image will then “remember” what sizes it doesn’t have, even if they are registered sizes — for example, it won’t show up in drop-downs where you insert the image.
Related Posts:
- Missing feature image link function
- How to create thumbnails for PDF uploads?
- Generating Responsive Background Image Sizes in PHP
- Why are image thumbnails cropped proportionally (not per dimensions)?
- Why are image thumbnails cropped proportionally (not per dimensions)?
- Display height and width properties with the_post_thumbnail() or related function
- Removing the first image in content
- Function to auto-set a featured image that is already in use
- Change size and crop medium_large images
- wp_get_attachment_image function size argument not working if I also add a custom class
- Overwriting Parent Theme’s Image Sizes
- Can you put a condition on add_image_size() depending on the width of the uploaded image?
- show first 3 thumbnails of posts in different sizes [closed]
- Set spesific size of featured images
- Can’t generate thumbnail images
- Control the srcset much more (not all sizes in it each time)
- How to hide image-url if no attachment?
- How to add a new image size and apply it to posts only?
- Featured Image keeps cropping
- Custom image sizes showing in Classic Editor only when upladed directly to post
- Can You Set A Minimum Image Dimension For Resizing Images?
- Featured image on archive page based on post type
- Printing medium sized image
- Function the_content
- Full size image option removing problem
- Image width issue in IE [closed]
- Add image size if page template
- remove links from images using functions.php
- Is there a hook which fires after all thumbnails are generated?
- Does hooking into the same action multiple times drain memory?
- Issue with wp_get_attachment_image() and SVG file type
- Extract image from content and set it as the featured image
- Removing default image size list in Media Box
- How to display the_post_thumbnail if a post has one or otherwise display the first image in a post?
- How do I use element instead of tags in WordPress post content having webP support?
- Linking thumbnail to full size image
- Gallery images titles – get from post
- Add a Post Thumbnail to an RSS Feed with custom size
- How to add a rel attribute to images that contains their categories?
- Is it possible to be more page/post specific with admin_enqueue_script?
- Issue adding text after short description on product pages Woocommerce [closed]
- Customize “the_posts_pagination” and put list instead div
- Setting featured image with function, 1 part not working
- Best way to programatically add “rel” attributes to page and post images
- How to display a default image for post thumbnail using functions.php?
- How can you limit srcset on a single type of page?
- Gravity Forms – Using a Form to Pre-populate A Gravity Form [closed]
- Running a script before absolutely everything
- Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘greenday_head’ not found or invalid function name
- Twentyten Child Theme: header images display messed up in admin panel
- Trigger a custom function when option are saved in admin area
- How to use wp_trash_post (or wp_delete_post) for deleting a (or all) post(s) from custom post type?
- What did I do wrong in my functions code, that will not change the “Get New Password” text to “Send It”?
- Pass parameter to hooked function using custom page template
- It does not work concatenation in function.php
- Random Featured Image Based on Category
- Is my code correct to enqueue CSS on a specific page?
- Can’t properly set the_title add_filter to show short_URL
- How to retrieve the current post’s generated featured image size?
- Check if the image size is available and if not use ‘full’ image size
- Limit dimensions of “Full” size images
- How do I hook into the container of wp_nav_menu?
- Remove links from the_content when using filters wp_trim_excerpt
- Visual Composer creating own shortcodes with vc_map() to return simple Image
- Is there a way to prevent a function/method from being called outside a specific hook?
- Function not working inside of a function in functions.php
- Can set_post_thumbnail be used to remove a thumbnail?
- Adding code before post title with the_title produces weird results
- Woocommerce image sizes missing from Appearance › Customize but not declared by theme
- Need To Change WordPress Meta Tags… Includes wordpress shortcodes
- This code works, but the way I integrated it is breaking the media uploader. How can I integrate it properly?
- update_post_meta is Updating with two page id
- Custom Image Size in Insert Media
- Change custom featured image size in twentythirteen child theme
- How to conditionally add a widget via a hook in functions.php
- resizing of thumbnails not working
- get_image_tag() html output : empty src attribute
- How to make excerpt image be full size instead of thumbnail?
- include w_thumbnail_src in function?
- Need To Resize Images Exactly Without Losing Image Details
- How to add or delete default value theme options while activate and deactivate theme
- WordPress comment processing . Default unapproved comments detection before posting
- Warning at top of website & top menu gone
- how can I change all wordpress media file url to custom cdn url?
- Can I use ‘Featured Image’ as a hero image and a thumbnail?
- Save_post – Warning: Cannot modify header information
- How to use WP conditional tag in a functions.php with OCEANWP Theme?
- custom error message for empty username and password using authenticate filter not working
- Get post_author email for Zapier Integration
- blank page with wp_get_attachment
- WordPress hooks to call a function inside a construct
- WordPress undefined function error on using add_action() in functions.php
- No compression occurs on my jpegs after adding jpeg_quality hook to my functions.php file
- Update add_image_size
- save_post hook – headers already sent?
- Dynamically adding Captions to images
- Add custom fields after post/page title
- Force resize for all video content
- Unhook the comment form in Buddypress
- How to hook wp_mail to add a custom email as BCC for each sent email?