Mecha CMS

Mecha CMS blog and documentation.

Custom Comment Avatar Image

Table of Contents
  1. Hard-Coded Avatar Property 
  2. Local Avatars 

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
email: email@domain.com
type: Markdown
status: 1
avatar: path/to/avatar.png
...

Lorem ipsum dolor sit amet.

Local Avatars 

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

.\
└── lot\
    └── asset\
        └── png\
            └── -avatar
                ├── 7328fddefd53de471baeb6e2b764f78a.png ← email@domain.com
                ├── 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(LOT . DS . '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;
});