Rename image uploads with width in filename

I’ve managed to do it with the filter image_make_intermediate_size.

Probably all the path/filename.extension dismembering and remaking could be optimized or made in a single stroke fashion, but alas, I’ll let that to the reader:

// The filter runs when resizing an image to make a thumbnail or intermediate size.
add_filter( 'image_make_intermediate_size', 'rename_intermediates_wpse_82193' );

function rename_intermediates_wpse_82193( $image ) 
{
    // Split the $image path into directory/extension/name
    $info = pathinfo($image);
    $dir = $info['dirname'] . "https://wordpress.stackexchange.com/";
    $ext="." . $info['extension'];
    $name = wp_basename( $image, "$ext" );

    // Build our new image name
    $name_prefix = substr( $name, 0, strrpos( $name, '-' ) );
    $size_extension = substr( $name, strrpos( $name, '-' ) + 1 );
    $new_name = $dir . $size_extension . '-' . $name_prefix . $ext;

    // Rename the intermediate size
    $did_it = rename( $image, $new_name );

    // Renaming successful, return new name
    if( $did_it )
        return $new_name;

    return $image;
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)