Working with User Avatars

The following requires Concrete CMS 5.7.5.7 or greater.

All user accounts in Concrete can have a profile picture associated with them. We call this picture a "User Avatar." By default, this picture is set via standard controls in the Dashboard:

Just click the user avatar image – upload one from your local machine, and your public profile, conversation messages and more will display your user avatar. It's easy to add this type of user avatar functionality to your own custom code.

Displaying User Avatars

Displaying user avatar's using custom PHP code is easy. First, retrieve a UserInfo object, and on that object, call the following:

$avatar = $ui->getUserAvatar();

Then, simply call output() on the object:

print $avatar->output();

output() takes care of printing an image tag, determining the avatar's location, etc...

Getting the path to an Avatar

Need the path to an avatar's image, instead of a full image tag? Use

<img class="custom-avatar" src="<?=$avatar->getPath()?>">

instead.

Determine whether a user has an avatar

if ($user->hasAvatar()) {
    print 'User has avatar.';
}

Updating User Avatars Programmatically

Setting or Changing the Image

Concrete uses the Imagine image handling library for its image manipulation, and the image that you're going to use as the avatar needs to be loaded into it. First, load an uploaded image into Imagine:

$image = \Image::open($_FILES['avatar']['tmp_name']);

This creates the Image object.

Next, let's say you want to resize the avatar down to the standard width and height:

$image = $image->thumbnail(
    new \Imagine\Image\Box(
        \Config::get('concrete.icons.user_avatar.width'),
        \Config::get('concrete.icons.user_avatar.height')
    )
);

That will return a resized image resource. This we can pass to our UserInfo method:

$user->updateUserAvatar($image);

That's it! The avatar will be updated.

Removing the Avatar

It's easy to remove a user avatar from a UserInfo record. Just use the update() method written about earlier:

$user->update(array('uHasAvatar' => 0));

Advanced: Creating an Alternate Service for Avatar Delivery

In addition to improving some of the behind-the-scenes user APIs in Concrete, version 5.7.5.4 introduced the ability to provide a completely separate system of avatar delivery for users. Historically, avatars and user accounts in Concrete have been tightly coupled: if you wanted to show a picture next to a user account, you had to use the built-in method of storing pictures against Concrete accounts. Some minor modifications were introduced to allow for a Gravatar-based fallback – but things were still tightly coupled. With the changes introduced in Concrete version 5.7.5.4, it's much easier to do the following with custom code:

  • Display images from alternate authentication providers as user avatars
  • Use a Facebook image as a user avatar
  • When allowing login from concretecms.org, use that profile picture
  • More...

In fact, this very documentation site employs a custom avatar service: when users login with their concretecms.org account, their avatars are displayed along with those users – even though a user never uploaded an avatar file to this particular website. Here's how it works:

Create a Custom Avatar Service

You're going to be creating a completely custom avatar service, which conforms to the Concrete\Core\User\Avatar\AvatarServiceInterface This service needs to implement the following methods

public function userHasAvatar(UserInfo $ui);
public function getAvatar(UserInfo $ui);
public function removeAvatar(UserInfo $ui);

The service is responsible for determing whether an avatar exists, retrieving that avatar object, and removing the avatar.

Additionally, the getAvatar method needs to return an instance of the Concrete\Core\User\Avatar\AvatarInterface class. This class needs to implement the following methods:

public function getPath();
public function output();

That's it! Finally, you'll need to tell Concrete to use a separate avatar service, rather than the built-in standard avatar service. You can do this from within a custom package's on_start() method or from within your site's application/bootstrap/app.php file:

\Core::bindShared('user/avatar', function() {
    return \Core::make('My\Custom\AvatarService');
});

where \My\Custom\AvatarService implements the AvatarServiceInterface described above.

This will return you an object of the Concrete\Core\User\Avatar\AvatarInterface interface.