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]
- Issues with title-tag and document_title_parts
- How to change the markup WordPress inserts for post images
- Programmatically Set First Image as Featured
- Add action hook conditionally – only when home.php in use
- why doesnt is_home() work in functions.php
- WP 4.4. responsive loads normal image after loading the responsive image
- How can I tell if I’m on a login page? [duplicate]
- How to reduce original image quality on upload?
- In WooCommerce I need to modify the thankyou.php page [closed]
- Why get_header() or get_footer() does not run twice if called in the same php file?
- Add options to featured image
- Set image size on media and text block with a function
- Change parent theme file function in child themes functions.php
- Adding extra SVGs to TwentyNineteen child theme using class TwentyNineteen_SVG_Icons
- Create thumbnail on PDF upload with Gravity Forms
- Remove images from get_the_excerpt
- gform_after_submission content appears immediately after , not in post body [closed]
- Use Exact Image Size using add_image_size
- Insert width & height attributes to all images displayed on each page
- Increment price for Woocommerce Minicart [closed]
- how to change link of some wordpress pages
- Overwrite Parent Theme add_image_size in Child Theme
- Images uploaded using media uploader are appearing upside down
- How to add css class to image attached in all the posts?
- Proper way of using functions in action hook?
- Add custom image size not working
- How to preserve edits to Name or Slug of term when using wp_update_term on save?
- Get the 5 lastest Featured Posts from a category
- how to add an image to the footer?
- How to properly add function called by action-hooked function to functions.php?
- Getting Featured Image Caption to Only Show if Populated
- Can’t check if a post has thumbnail adding filter to get_post_metadata()
- Add function to every post?
- Post thumbnail not showing in WP admin area for custom post type
- Understanding and altering the structure of posted images
- Backstretch.js and thumbnail sizes to reduce loading time
- Passing values from a widget to a function within a plugin
- Publish/Update post is changing image links from file url to post url
- Featured Image Thumbnail Creation
- How to stop twenty fifteen theme from cropping featured images?
- Display images that are not in the content
- Replace a menu with widget or a custom template file programmatically
- Getting custom image size URL in functions
- Pass parameter to hooked function using custom page template
- 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
- Check if the image size is available and if not use ‘full’ image size
- Limit dimensions of “Full” size images
- Can set_post_thumbnail be used to remove a thumbnail?
- Need To Change WordPress Meta Tags… Includes wordpress shortcodes
- update_post_meta is Updating with two page id
- Custom Image Size in Insert Media
- 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
- 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?
- blank page with wp_get_attachment
- No compression occurs on my jpegs after adding jpeg_quality hook to my functions.php file
- Add custom fields after post/page title
- How to hook wp_mail to add a custom email as BCC for each sent email?
- Is mytheme_post_thumbnails() still necessary?
- Generate and upload screenshot as featured image for user-submitted post
- Create section wit form
- How can I able to exclude ‘Category’ and ‘Tag’ slug from the URL in WordPress without reloading to new site?
- No src and sizes attributes present on WordPress thumbnail images; the smallest image size is loaded irregardless of viewport size
