Hi, don’t worry for delay.
The following code create these thumbnails when we load a picture with “-hd” prefix, i.e. colosseum-hd.jpg
- colosseum-hd.jpg (original file)
- colosseum.jpg (large)
- colosseum-medium.jpg (medium)
- colosseum-thumbnail.jpg (thumbnail)
add_filter( 'image_make_intermediate_size', 'custom_images_filename' );
function custom_images_filename( $image ) {
? ? // Info file
? ? $info = pathinfo($image);
? ? $dir = $info['dirname'] . '/';
? ? $ext = '.' . $info['extension'];
? ? $name = wp_basename( $image, "$ext" );
? ? $hd= '-hd';
? ? // If image is High Definition remove prefix
? ? if(strpos($name,$hd) !== false) {
? ? ? ? $name = str_replace($hd,'',$name);
? ? // If image is Low Definition don't change filename but add '-hd' to "large" dimension
? ? } else {
? ? ? ? $force_prefix = 'yes';
? ? }
? ? // Get image information
? ? $img = wp_get_image_editor( $image );
? ? $img_size = $img->get_size();
? ? // Image prefix for builtin sizes
? ? $widths = [];
? ? $size_based_img_name_prefix = '';
? ? foreach ( get_intermediate_image_sizes() as $_size ) {
? ? ? ? $width = get_option( "{$_size}_size_w" );
? ? ? ? if ( ! isset( $widths[ $width ] ) ) {
? ? ? ? ? ? $widths[ $width ] ?= $_size;
? ? ? ? }
? ? }
? ? if ( array_key_exists( $img_size[ 'width' ], $widths ) ) {
? ? ? ? if($widths[ $img_size['width'] ]!='large' || $force_prefix=='yes') $size_based_img_name_prefix = '-'.$widths[ $img_size['width'] ];
? ? ? ? $name_prefix = substr( $name, 0, strrpos( $name, '-' ) );
? ? } else {
? ? ? ? $name_prefix = $name;
? ? }
? ? // Build our new image name
? ? $new_name = $dir . $name_prefix .$size_based_img_name_prefix . $ext;
? ? // Rename the intermediate size
? ? $did_it = rename( $image, $new_name );
? ? // Renaming successful, return new name
? ? if( $did_it ) {
? ? ? return $new_name;
? ? } else {
? ? ? return $image;
? ? }
}
The plugin works fine with filenames without “-hd” (both in renaming them and in performing the replace within the content of posts) while with files containing “-hd” it only renames them but does not perform the replace within posts.
Thank you