In a project I'll be working on soon there will be a need to generate avatars. The generation process will be one of those where the user can select different heads, hairstyles, clothing, etc. Some items will also be unavailable at first and will have to be earned or purchased.
I already have a fair idea on how to do this, but since it is a nontrivial amount of code, it would be nice to see working examples of code. Ideally I could just take a script and integrate into my page, but just gathering ideas from other people would be good too.
I'll be working with PHP, but examples in other languages are welcome too.
Added: To clarify, I don't mean a random avatar generator (or one that generates an avatar based on some hash value). A random avatar generator is subtly different from what I have intented. In a random avatar generator the programmer-artist has a much greater say of what goes where. He can carefully pick out pieces that will not conflict with each other, and he can discard those that give him trouble.
In my case the avatar generator is more like this. The user chooses which head to use, which hairstyle to apply, which piece of clothing to use, etc. There are way more pieces there, with artists adding new ones every once in a while. It's much harder to test how pieces will or won't fit together. Sometimes more advanced blending is required (like a hat would have a part of it in front of hair, and part of it behind the hair). Etc.