Mecha CMS

Code snippet collection for developers.

Custom Comment Avatar Image

Replace the default comment’s Gravatar image with your own comment avatar image.

Requires comment extension.

Hard-Coded Avatar Property 

Add an avatar property to the comment file to replace the automatic avatar image generated by the Gravatar service:

author: Taufik Nurrohman
type: Markdown
status: 1
avatar: path/to/avatar.png

Lorem ipsum dolor sit amet.

Local Avatars 

Using email address as the avatar ID, you can create local avatar for each users by storing them in the .\lot\asset folder.

└── asset\
    └── png\
        └── -avatar
            ├── 7328fddefd53de471baeb6e2b764f78a.png ←
            ├── ac1514317918b7810594431832d3adae.png ← …
            └── 459b9511a7f650ebd327889c45cc4e9b.png ← …

The hook:

Hook::set('comment.avatar', function($avatar) {
    if (!$this->email) { // No email data, skip…
        return $content;
    $id = md5($this->email);
    if ($path = File::exist(ASSET . DS . 'png' . DS . '-avatar' . DS . $id . '.png')) {
        return To::URL($path) . '?v=' . File::open($path)->update;
    // Local avatar image does not exist, return the initial value…
    return $avatar;