Hi @ejamo,
I’m sorry for my late reply, I’ll check immediately this issue and let you know if I can release a fix.
Thank you for reporting.
C.
Hi @ejamo,
I’m finally back in the office, can you tell me which code are you using in the custom_images_filename method, so that I can replicate your issue?
Thank you
C.
Thread Starter
Mario
(@ejamo)
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
Hi @ejamo,
everything worked in my tests, maybe the image resolution is involved, can you send me at [email protected] a test image that causes the issue on your server?
Thank you
C.
Thread Starter
Mario
(@ejamo)
Hi, thank you for the tests you have done.
I don’t think it depends on the single photo, because we have tried dozens of them and unfortunately the problem is always the same.
I have to say that until a few versions ago everything was working correctly, so I don’t know what might have changed or if a conflict with other plugins might have occurred (although to be honest we use very few of them).
Could it be a problem related to the priority with which operations are executed? Any other ideas on what to investigate?
Thank you again