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\Addon;
11 use Friendica\Core\Config;
12 use Friendica\Core\L10n;
13 use Friendica\Core\PConfig;
14 use Friendica\Core\Worker;
15 use Friendica\Database\DBA;
16 use Friendica\Database\DBM;
17 use Friendica\Model\Contact;
18 use Friendica\Model\Photo;
19 use Friendica\Network\HTTPException\NotFoundException;
21 define("CATAVATAR_SIZE", 256);
24 * Installs the addon hook
26 function catavatar_install()
28 Addon::registerHook('avatar_lookup', 'addon/catavatar/catavatar.php', 'catavatar_lookup');
29 Addon::registerHook('addon_settings', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings');
30 Addon::registerHook('addon_settings_post', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings_post');
32 logger('registered catavatar');
36 * Removes the addon hook
38 function catavatar_uninstall()
40 Addon::unregisterHook('avatar_lookup', 'addon/catavatar/catavatar.php', 'catavatar_lookup');
41 Addon::unregisterHook('addon_settings', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings');
42 Addon::unregisterHook('addon_settings_post', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings_post');
44 logger('unregistered catavatar');
48 * Cat avatar user settings page
50 function catavatar_addon_settings(App $a, &$s)
56 $t = get_markup_template('settings.tpl', 'addon/catavatar/');
57 $s .= replace_macros ($t, [
58 '$postpost' => !empty($_POST['catavatar-morecat']) || !empty($_POST['catavatar-emailcat']),
60 '$uid' => local_user(),
61 '$usecat' => L10n::t('Use Cat as Avatar'),
62 '$morecat' => L10n::t('More Random Cat!'),
63 '$emailcat' => L10n::t('Reset to email Cat'),
64 '$seed' => PConfig::get(local_user(), 'catavatar', 'seed', false),
65 '$header' => L10n::t('Cat Avatar Settings'),
70 * Cat avatar user settings POST handle
72 function catavatar_addon_settings_post(App $a, &$s)
78 // delete the current cached cat avatar
79 $condition = ['uid' => local_user(), 'blocked' => false,
80 'account_expired' => false, 'account_removed' => false];
81 $user = DBA::selectFirst('user', ['email'], $condition);
83 $seed = PConfig::get(local_user(), 'catavatar', 'seed', md5(trim(strtolower($user['email']))));
85 if (!empty($_POST['catavatar-usecat'])) {
86 $url = $a->get_baseurl() . '/catavatar/' . local_user() . '?ts=' . time();
88 $self = DBA::selectFirst('contact', ['id'], ['uid' => local_user(), 'self' => true]);
89 if (!DBM::is_result($self)) {
90 notice(L10n::t("The cat hadn't found itself."));
94 Photo::importProfilePhoto($url, local_user(), $self['id']);
96 $condition = ['uid' => local_user(), 'contact-id' => $self['id']];
97 $photo = DBA::selectFirst('photo', ['resource-id'], $condition);
98 if (!DBM::is_result($photo)) {
99 notice(L10n::t('There was an error, the cat ran away.'));
103 DBA::update('photo', ['profile' => false], ['profile' => true, 'uid' => local_user()]);
105 $fields = ['profile' => true, 'album' => L10n::t('Profile Photos'), 'contact-id' => 0];
106 DBA::update('photo', $fields, ['uid' => local_user(), 'resource-id' => $photo['resource-id']]);
108 Photo::importProfilePhoto($url, local_user(), $self['id']);
110 Contact::updateSelfFromUserID(local_user(), true);
112 // Update global directory in background
113 $url = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
114 if ($url && strlen(Config::get('system', 'directory'))) {
115 Worker::add(PRIORITY_LOW, 'Directory', $url);
118 Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
120 info(L10n::t('Meow!'));
124 if (!empty($_POST['catavatar-morecat'])) {
125 PConfig::set(local_user(), 'catavatar', 'seed', time());
128 if (!empty($_POST['catavatar-emailcat'])) {
129 PConfig::delete(local_user(), 'catavatar', 'seed');
134 * Returns the URL to the cat avatar
139 function catavatar_lookup(App $a, &$b)
141 $user = DBA::selectFirst('user', ['uid'], ['email' => $b['email']]);
142 $url = $a->get_baseurl() . '/catavatar/' . $user['uid'];
145 case 175: $url .= "/4"; break;
146 case 80: $url .= "/5"; break;
147 case 47: $url .= "/6"; break;
151 $b['success'] = true;
154 function catavatar_module() {}
157 * Returns image for user id
159 * @throws NotFoundException
162 function catavatar_content(App $a)
164 if ($a->argc < 2 || $a->argc > 3) {
165 throw new NotFoundException(); // this should be catched on index and show default "not found" page.
168 $uid = intval($a->argv[1]);
172 $size = intval($a->argv[2]);
175 $condition = ['uid' => $uid, 'blocked' => false,
176 'account_expired' => false, 'account_removed' => false];
177 $user = DBA::selectFirst('user', ['email'], $condition);
179 if ($user === false) {
180 throw new NotFoundException();
183 $seed = PConfig::get($uid, "catavatar", "seed", md5(trim(strtolower($user['email']))));
185 // ...Or start generation
188 // render the picture:
189 build_cat($seed, $size);
199 * ====================
200 * CAT-AVATAR-GENERATOR
201 * ====================
203 * @authors: Andreas Gohr, David Revoy
205 * This PHP is licensed under the short and simple permissive:
206 * [MIT License](https://en.wikipedia.org/wiki/MIT_License)
210 function build_cat($seed = '', $size = 0)
214 srand(hexdec(substr(md5($seed), 0, 6)));
217 // throw the dice for body parts
219 'body' => rand(1, 15),
220 'fur' => rand(1, 10),
221 'eyes' => rand(1, 15),
222 'mouth' => rand(1, 10),
223 'accessorie' => rand(1, 20)
227 $cat = @imagecreatetruecolor(CATAVATAR_SIZE, CATAVATAR_SIZE)
228 or die("GD image create failed");
229 $white = imagecolorallocate($cat, 255, 255, 255);
230 imagefill($cat, 0, 0, $white);
233 foreach ($parts as $part => $num) {
234 $file = dirname(__FILE__) . '/avatars/' . $part . '_' . $num . '.png';
236 $im = @imagecreatefrompng($file);
238 die('Failed to load ' . $file);
240 imageSaveAlpha($im, true);
241 imagecopy($cat, $im, 0, 0, 0, 0, CATAVATAR_SIZE, CATAVATAR_SIZE);
246 if ($size > 3 && $size < 7) {
259 $dest = imagecreatetruecolor($size, $size);
260 imagealphablending($dest, false);
261 imagesavealpha($dest, true);
262 imagecopyresampled($dest, $cat, 0, 0, 0, 0, $size, $size, CATAVATAR_SIZE, CATAVATAR_SIZE);
267 // restore random seed
272 header('Pragma: public');
273 header('Cache-Control: max-age=86400');
274 header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
275 header('Content-Type: image/jpg');
276 imagejpeg($cat, NULL, 90);