I am attempting to resize picture dimensions once I uploaded photos to the Media Library so I haven’t got extraneous sizes I do not want clogging up the uploads folder. I am defining particular sizes with add_image_size
, however I am unsure the right way to limit which sizes are used and I would love to do it primarily based on filename suffixes filename_words_suffix.jpg
.
As well as, I wish to scale featured photos by 50% whereas nonetheless with the ability to have an identifier so I can seize their urls with wp_get_attachment_image_src
.
The purpose of all of that is to make use of <img>
srcset
for normal shows and high-dpi shows however not have wasteful photos created and saved. I actually don’t know the place to start out, however assume it is going to use a hook
and a operate
, and may’t discover something when looking about the right way to goal uploads by filename and apply particular dimensions or scale by a %, however here is the logic:
add_image_size('portrait-preview', 360, 480, true);
add_image_size('hpi-half-res', 1400, 300, true);
...
add_action('some_hook', 'my_image_size_function');
operate my_image_size_function(){
// get filename
// strip extension with regex
// change underscores to house with str_replace (or with regex above and omit this step)
// extract filename suffix into variable $img_suffix with explode and array_pop
if ($img_suffix == 'hpi'){
// code to limit photos ending in 'hpi' to hpi-half-size dimensions
} else if {$img_suffix == 'portrait'}{
// code to limit photos ending in 'portrait' to portrait-preview dimensions
} else if {$image_suffix == 'featured'}{
// code to limit photos ending in 'featured' to 50% scale, dimensions range
// assign some identifier, like 'featured-half', so url will be fetched
} else {
// don't resize and solely hold the full-size picture
}
// -> do the resize (or not) and go the to media add/edit web page
}
I am additionally utilizing Allow Media Substitute and Regenerate Thumbnails plug-ins, but when there is a battle, I can work that out after the actual fact.