3 * Name: Cat Avatar Generator
4 * Description: Generate a default avatar based on David Revoy's cat-avatar-generator https://framagit.org/Deevad/cat-avatar-generator
6 * Author: Fabio <https://kirgroup.com/profile/fabrixxm>
10 use Friendica\Core\Config;
11 use Friendica\Core\Hook;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Logger;
14 use Friendica\Core\PConfig;
15 use Friendica\Core\Renderer;
16 use Friendica\Core\Worker;
17 use Friendica\Database\DBA;
18 use Friendica\Model\Contact;
19 use Friendica\Model\Photo;
20 use Friendica\Network\HTTPException\NotFoundException;
22 define("CATAVATAR_SIZE", 256);
25 * Installs the addon hook
27 function catavatar_install()
29 Hook::register('avatar_lookup', 'addon/catavatar/catavatar.php', 'catavatar_lookup');
30 Hook::register('addon_settings', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings');
31 Hook::register('addon_settings_post', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings_post');
33 Logger::log('registered catavatar');
37 * Removes the addon hook
39 function catavatar_uninstall()
41 Hook::unregister('avatar_lookup', 'addon/catavatar/catavatar.php', 'catavatar_lookup');
42 Hook::unregister('addon_settings', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings');
43 Hook::unregister('addon_settings_post', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings_post');
45 Logger::log('unregistered catavatar');
49 * Cat avatar user settings page
51 function catavatar_addon_settings(App $a, &$s)
57 $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/catavatar/');
58 $s .= Renderer::replaceMacros($t, [
59 '$postpost' => !empty($_POST['catavatar-morecat']) || !empty($_POST['catavatar-emailcat']),
61 '$uid' => local_user(),
62 '$usecat' => L10n::t('Use Cat as Avatar'),
63 '$morecat' => L10n::t('More Random Cat!'),
64 '$emailcat' => L10n::t('Reset to email Cat'),
65 '$seed' => PConfig::get(local_user(), 'catavatar', 'seed', false),
66 '$header' => L10n::t('Cat Avatar Settings'),
71 * Cat avatar user settings POST handle
73 function catavatar_addon_settings_post(App $a, &$s)
79 // delete the current cached cat avatar
80 $condition = ['uid' => local_user(), 'blocked' => false,
81 'account_expired' => false, 'account_removed' => false];
82 $user = DBA::selectFirst('user', ['email'], $condition);
84 $seed = PConfig::get(local_user(), 'catavatar', 'seed', md5(trim(strtolower($user['email']))));
86 if (!empty($_POST['catavatar-usecat'])) {
87 $url = $a->getBaseURL() . '/catavatar/' . local_user() . '?ts=' . time();
89 $self = DBA::selectFirst('contact', ['id'], ['uid' => local_user(), 'self' => true]);
90 if (!DBA::isResult($self)) {
91 notice(L10n::t("The cat hadn't found itself."));
95 Photo::importProfilePhoto($url, local_user(), $self['id']);
97 $condition = ['uid' => local_user(), 'contact-id' => $self['id']];
98 $photo = DBA::selectFirst('photo', ['resource-id'], $condition);
99 if (!DBA::isResult($photo)) {
100 notice(L10n::t('There was an error, the cat ran away.'));
104 DBA::update('photo', ['profile' => false], ['profile' => true, 'uid' => local_user()]);
106 $fields = ['profile' => true, 'album' => L10n::t('Profile Photos'), 'contact-id' => 0];
107 DBA::update('photo', $fields, ['uid' => local_user(), 'resource-id' => $photo['resource-id']]);
109 Photo::importProfilePhoto($url, local_user(), $self['id']);
111 Contact::updateSelfFromUserID(local_user(), true);
113 // Update global directory in background
114 $url = $a->getBaseURL() . '/profile/' . $a->user['nickname'];
115 if ($url && strlen(Config::get('system', 'directory'))) {
116 Worker::add(PRIORITY_LOW, 'Directory', $url);
119 Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
121 info(L10n::t('Meow!'));
125 if (!empty($_POST['catavatar-morecat'])) {
126 PConfig::set(local_user(), 'catavatar', 'seed', time());
129 if (!empty($_POST['catavatar-emailcat'])) {
130 PConfig::delete(local_user(), 'catavatar', 'seed');
135 * Returns the URL to the cat avatar
140 function catavatar_lookup(App $a, &$b)
142 $user = DBA::selectFirst('user', ['uid'], ['email' => $b['email']]);
143 $url = $a->getBaseURL() . '/catavatar/' . $user['uid'];
146 case 300: $url .= "/4"; break;
147 case 80: $url .= "/5"; break;
148 case 47: $url .= "/6"; break;
152 $b['success'] = true;
155 function catavatar_module() {}
158 * Returns image for user id
160 * @throws NotFoundException
163 function catavatar_content(App $a)
165 if ($a->argc < 2 || $a->argc > 3) {
166 throw new NotFoundException(); // this should be catched on index and show default "not found" page.
169 $uid = intval($a->argv[1]);
173 $size = intval($a->argv[2]);
176 $condition = ['uid' => $uid,
177 'account_expired' => false, 'account_removed' => false];
178 $user = DBA::selectFirst('user', ['email'], $condition);
180 if ($user === false) {
181 throw new NotFoundException();
184 $seed = PConfig::get($uid, "catavatar", "seed", md5(trim(strtolower($user['email']))));
186 // ...Or start generation
189 // render the picture:
190 build_cat($seed, $size);
200 * ====================
201 * CAT-AVATAR-GENERATOR
202 * ====================
204 * @authors: Andreas Gohr, David Revoy
206 * This PHP is licensed under the short and simple permissive:
207 * [MIT License](https://en.wikipedia.org/wiki/MIT_License)
211 function build_cat($seed = '', $size = 0)
215 srand(hexdec(substr(md5($seed), 0, 6)));
218 // throw the dice for body parts
220 'body' => rand(1, 15),
221 'fur' => rand(1, 10),
222 'eyes' => rand(1, 15),
223 'mouth' => rand(1, 10),
224 'accessorie' => rand(1, 20)
228 $cat = @imagecreatetruecolor(CATAVATAR_SIZE, CATAVATAR_SIZE)
229 or die("GD image create failed");
230 $white = imagecolorallocate($cat, 255, 255, 255);
231 imagefill($cat, 0, 0, $white);
234 foreach ($parts as $part => $num) {
235 $file = dirname(__FILE__) . '/avatars/' . $part . '_' . $num . '.png';
237 $im = @imagecreatefrompng($file);
239 die('Failed to load ' . $file);
241 imageSaveAlpha($im, true);
242 imagecopy($cat, $im, 0, 0, 0, 0, CATAVATAR_SIZE, CATAVATAR_SIZE);
247 if ($size > 3 && $size < 7) {
260 $dest = imagecreatetruecolor($size, $size);
261 imagealphablending($dest, false);
262 imagesavealpha($dest, true);
263 imagecopyresampled($dest, $cat, 0, 0, 0, 0, $size, $size, CATAVATAR_SIZE, CATAVATAR_SIZE);
268 // restore random seed
273 header('Pragma: public');
274 header('Cache-Control: max-age=86400');
275 header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
276 header('Content-Type: image/jpg');
277 imagejpeg($cat, NULL, 90);