upload or auto setup feature photo by user avatar

In general avatars can not be a replacement to attachments unless you have a pluging which forces all avatars to be uploaded via the wordpress upload process to the site (or in other words, be included in the media library). Avatars can be any URL while attachments have an id in your DB. Therefor your attempt to treat the avatar as an attachment can not succeed.

The only way to make it work is if you will first fetch the avatar image from whatever service it is located on, and create an attachment out of it. Then you can treat it in the way your code implies you intended to do.