Different body image backgrounds on different pages, posts and categories

You need to add the classes for the corresponding cases.

For pages/posts:

$css = "body.page-id-**post_id** { background-image: url('$bg_image'); }";

For categories:

$css = "body.category-**category_id** { background-image: url('$bg_image'); }";

You can retrieve post_id with $post->ID. For the category you need to check whether the page is_archive() and to append the corresponding ID or category name.