Snippet · Mecha CMS

Code snippet collection for developers.

Custom Comment Avatar Image

Author: Taufik Nurrohman · 419 Views

Tweet

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 email address as the avatar ID, you can create local avatars 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, $lot) {
    if (!isset($lot['email'])) { // No email data, skip…
        return $content;
    }
    $id = md5($lot['email']);
    if ($path = File::exist(ASSET . DS . 'png' . DS . '-avatar' . DS . $id . '.png')) {
        return To::url($path) . '?v=' . File::T($path);
    }
    // Local avatar image does not exist, return the initial value…
    return $avatar;
});

0 Comments

No comments yet.