BACK TO BLOG OVERVIEW

New feature to users' avatars

In this post I will explain a little change, which is very important when it comes to displaying users` avatars.

    Currently we had only one possibility to display user's default avatar. To do it, we needed to include some static file, if user didn't have default image. It was doable by:

{{ url static_file="_img/user_blank_`$width`x`$height`.png" }}

where $width and $height are defined variables with number values needed to get image by its resolution.

    The above approach is not good because It forces us to create many, same images but differing only in size and name. If we wanted to have two diffrent default user's avatars for diffrent resolutions, we needed to create two files, for example:

- user_blank_120x120.jpg - which is default user's avatar in 120x120 resolution

- user_blank_60x60.jpg - which is default user's avatar in 60x60 resolution

And what if you wanted to have also other resolutions?

 

The improvement

    Here comes the new improvement, which let's you have more control over the users' avatars in a way how to display them. From now on It lets you read and crop images also from themes directory. You can pass 3rd argument with default avatar path, for example:

{{ $user->image($height, $width, 'fit', '_img/user-thumb.jpg') }} 

 

It means that file user-thumb.jpg from current theme (e.g themes/publication_1/theme_1/_img/user-thumb.jpg) will be taken and cropped by given $height and $width.

If user has profile image, then to simply display it use (like it was before):

{{ $user->image($height, $width) }}

Benefits:

  • displaying user image's logic is simpler
  • let's you read and resize images from the current theme directory
  • images for each resolution are not required anymore (user_blank_120x120.jpg, user_blank_60x60.jpg etc)
 
 

Example snippet:

The above snippet displays user image if user uploaded one, else it will display default user image from current theme directory and it will be automatically cropped.

Note that the old approach with including static file has not been deprecated. It can be still used as an alternative.


 
BACK TO TOP