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() parameter four ($crop = true) is ignored: the_post_thumbnail() returns scaled image(s) instead of hard cropped
- Pass parameter to hooked function using custom page template
- It does not work concatenation in function.php
- Override the WordPress core function wp_referer_field
- Random Featured Image Based on Category
- How to return hook data when multiple parameters are present?
- Hook into a function without a hook?
- What is the text that appears beside the page titles in the list of pages in the WP admin?
- How to edit the Tags within the image file URLs?
- 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
- Way to store featured image width and height in two separate variables?
- How to add_filter html template to middle of content
- Filter yoast canonical add_action priority [closed]
- (Woocommerce) Order by price when entering specific category
- srcset and custom field get_post_meta and wp_get_attachment_url
- 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?
- preg_replace not removed “class”
- Auto log in hook is requiring a page refresh
- is there a way to remove featured image from blog page and single page
- Possible to hook into Media Library preview File column and use a custom image?
- Function not working inside of a function in functions.php
- What are the meta fields for an attachment?
- Can set_post_thumbnail be used to remove a thumbnail?
- Can’t locate custom image sizes defined by child theme
- Adding code before post title with the_title produces weird results
- Add back in child theme what the parent theme removed with remove_action
- Writing a function to detect an event
- Woocommerce image sizes missing from Appearance › Customize but not declared by theme
- How to properly set custom logo size?
- Add attribute only to first image of every post via functions.php
- Need To Change WordPress Meta Tags… Includes wordpress shortcodes
- Need to convert image url to a Base_64 data url with wordpress function..
- How to Add Shortcode to html img code?
- This code works, but the way I integrated it is breaking the media uploader. How can I integrate it properly?
- WordPress wraps span tags into p tags
- Else/If Statement to Display Photo Descriptions
- Changing where my author box is printed
- Is it possible to use add_filter in an included file in the child theme’s functions.php?
- Insert Content Before div#main from the functions.php File
- Function to insert missing image size attributes into img tags
- Add Adsense code between job listings – wp job manager plugin
- Image Crop Not Working
- Use existing image sizes for WooCommerce
- update_post_meta is Updating with two page id
- Problem in using Customizer
- Replace admin header logo with an image
- wp_get_attachment_image_src returns image on main domain, false on subdomain
- Why I can’t load my JavaScripts using an hook inside my functions.php file?
- Retrieve First Image Function
- Show Featured Image in else statement
- Custom Image Size in Insert Media
- is_home() not working in functions.php file
- 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
- Prevent creating multiple image resizes in twentytwelve template
- How to place a div inside a function that creates a div
- Conditional function for excluding first image from content, not working
- Add_image_size not generating correct size
- Using add_image_size in functions.php to get original uploaded file
- get_image_tag() html output : empty src attribute
- How to make excerpt image be full size instead of thumbnail?
- show all the posts thumbnails
- Changing the text of Upload/Insert on Posts and Pages Screen
- struggling with syntax for the_post_thumbnail();
- include w_thumbnail_src in function?
- Filter for replacing the WP_query object for a given category