You can do this on the display level via
- hook_preprocess_field() (thx Clive in the comments, give him a thumbs up!), OR
- a custom formatter, OR
- a custom Display suite field
hook_preprocess_field() on the image field is the easiest.
You have the full user entity available and can use information from other fields to alter the image.
The hook is fired every time theme('field', ..) is fired, so you need to figure out if this is the correct field.
EDIT: Instead of a detailed tutorial, here is what I usually do:
- Install devel
- Create your custom module with an implementation of hook_preprocess_field(&$variables).
- Do dpm($variables) inside that hook implementation to show the function argument (sometimes you need to refresh the page twice to see the result).
- Manipulate the part of $variables that you are interested in.
With a custom formatter, you can define your own way to display e.g. the image field, while having the data from other node fields available.
Then on the user display configuration, you can choose this formatter to display the image.
With the custom DS field, you can introduce a "field" - that is, a positionable element on the entity display. This field can use data from multiple data fields on the node entity. E.g. you first check if there is an explicitly uploaded image, and if not, you show an image depending on the gender (male, female, transgender / don't say).
You can also define a form to configure display options. E.g. you could make the image style configurable.
- Enable Display suite
- Implement hook_ds_fields_info() in a custom module, using e.g. DS_FIELD_TYPE_FUNCTION.
- Provide the function to generate the html or render array (some examples)
- Configure your user to display with a Display suite layout.
- Enable your custom field.
There are more details to this. But I the main purpose of this answer is to point you in a direction.
The weakness of all these options is that you have to hardcode the field names, or at least one of them. If you want to reuse this on another site with other field names, you have to change the field names in the code.