How do I access a user image? How can I access a user image in Drupal 8? I am trying to use this code, but I am getting an error.

$account = \Drupal\user\Entity\User::load($q-&gt;uid); 
$name = $account-&gt;getUsername();        
$picture = $account-&gt;getUser_picture();
Use the following snippet to get the user image.

$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()-&gt;id());
$picture = $user-&gt;get('user_picture')-&gt;entity-&gt;url();


If you want to get the default image when the user account doesn't have an image associated, use the following code.

$field = \Drupal\field\Entity\FieldConfig::loadByName('user', 'user', 'user_picture');
$default_image = $field-&gt;getSetting('default_image');
$file = \Drupal::entityManager()-&gt;loadEntityByUuid('file', $default_image['uuid']);
$picture = file_create_url($file-&gt;getFileUri());