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\Hook;
11 use Friendica\Core\Logger;
12 use Friendica\Core\Renderer;
13 use Friendica\Core\Worker;
14 use Friendica\Database\DBA;
16 use Friendica\Model\Contact;
17 use Friendica\Model\Photo;
18 use Friendica\Network\HTTPException\NotFoundException;
20 define("CATAVATAR_SIZE", 256);
23 * Installs the addon hook
25 function catavatar_install()
27 Hook::register('avatar_lookup', 'addon/catavatar/catavatar.php', 'catavatar_lookup');
28 Hook::register('addon_settings', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings');
29 Hook::register('addon_settings_post', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings_post');
31 Logger::log('registered catavatar');
35 * Cat avatar user settings page
37 function catavatar_addon_settings(App $a, &$s)
43 $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/catavatar/');
44 $s .= Renderer::replaceMacros($t, [
45 '$postpost' => !empty($_POST['catavatar-morecat']) || !empty($_POST['catavatar-emailcat']),
47 '$uid' => local_user(),
48 '$usecat' => DI::l10n()->t('Use Cat as Avatar'),
49 '$morecat' => DI::l10n()->t('More Random Cat!'),
50 '$emailcat' => DI::l10n()->t('Reset to email Cat'),
51 '$seed' => DI::pConfig()->get(local_user(), 'catavatar', 'seed', false),
52 '$header' => DI::l10n()->t('Cat Avatar Settings'),
57 * Cat avatar user settings POST handle
59 function catavatar_addon_settings_post(App $a, &$s)
65 // delete the current cached cat avatar
66 $condition = ['uid' => local_user(), 'blocked' => false,
67 'account_expired' => false, 'account_removed' => false];
68 $user = DBA::selectFirst('user', ['email'], $condition);
70 $seed = DI::pConfig()->get(local_user(), 'catavatar', 'seed', md5(trim(strtolower($user['email']))));
72 if (!empty($_POST['catavatar-usecat'])) {
73 $url = DI::baseUrl()->get() . '/catavatar/' . local_user() . '?ts=' . time();
75 $self = DBA::selectFirst('contact', ['id'], ['uid' => local_user(), 'self' => true]);
76 if (!DBA::isResult($self)) {
77 notice(DI::l10n()->t("The cat hadn't found itself."));
81 Photo::importProfilePhoto($url, local_user(), $self['id']);
83 $condition = ['uid' => local_user(), 'contact-id' => $self['id']];
84 $photo = DBA::selectFirst('photo', ['resource-id'], $condition);
85 if (!DBA::isResult($photo)) {
86 notice(DI::l10n()->t('There was an error, the cat ran away.'));
90 DBA::update('photo', ['profile' => false], ['profile' => true, 'uid' => local_user()]);
92 $fields = ['profile' => true, 'album' => DI::l10n()->t('Profile Photos'), 'contact-id' => 0];
93 DBA::update('photo', $fields, ['uid' => local_user(), 'resource-id' => $photo['resource-id']]);
95 Photo::importProfilePhoto($url, local_user(), $self['id']);
97 Contact::updateSelfFromUserID(local_user(), true);
99 // Update global directory in background
100 $url = DI::baseUrl()->get() . '/profile/' . $a->user['nickname'];
101 if ($url && strlen(DI::config()->get('system', 'directory'))) {
102 Worker::add(PRIORITY_LOW, 'Directory', $url);
105 Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
107 info(DI::l10n()->t('Meow!'));
111 if (!empty($_POST['catavatar-morecat'])) {
112 DI::pConfig()->set(local_user(), 'catavatar', 'seed', time());
115 if (!empty($_POST['catavatar-emailcat'])) {
116 DI::pConfig()->delete(local_user(), 'catavatar', 'seed');
121 * Returns the URL to the cat avatar
126 function catavatar_lookup(App $a, &$b)
128 $user = DBA::selectFirst('user', ['uid'], ['email' => $b['email']]);
129 $url = DI::baseUrl()->get() . '/catavatar/' . $user['uid'];
132 case 300: $url .= "/4"; break;
133 case 80: $url .= "/5"; break;
134 case 47: $url .= "/6"; break;
138 $b['success'] = true;
141 function catavatar_module() {}
144 * Returns image for user id
146 * @throws NotFoundException
149 function catavatar_content(App $a)
151 if ($a->argc < 2 || $a->argc > 3) {
152 throw new NotFoundException(); // this should be catched on index and show default "not found" page.
155 $uid = intval($a->argv[1]);
159 $size = intval($a->argv[2]);
162 $condition = ['uid' => $uid,
163 'account_expired' => false, 'account_removed' => false];
164 $user = DBA::selectFirst('user', ['email'], $condition);
166 if ($user === false) {
167 throw new NotFoundException();
170 $seed = DI::pConfig()->get($uid, "catavatar", "seed", md5(trim(strtolower($user['email']))));
172 // ...Or start generation
175 // render the picture:
176 build_cat($seed, $size);
186 * ====================
187 * CAT-AVATAR-GENERATOR
188 * ====================
190 * @authors: Andreas Gohr, David Revoy
192 * This PHP is licensed under the short and simple permissive:
193 * [MIT License](https://en.wikipedia.org/wiki/MIT_License)
197 function build_cat($seed = '', $size = 0)
201 srand(hexdec(substr(md5($seed), 0, 6)));
204 // throw the dice for body parts
206 'body' => rand(1, 15),
207 'fur' => rand(1, 10),
208 'eyes' => rand(1, 15),
209 'mouth' => rand(1, 10),
210 'accessorie' => rand(1, 20)
214 $cat = @imagecreatetruecolor(CATAVATAR_SIZE, CATAVATAR_SIZE)
215 or die("GD image create failed");
216 $white = imagecolorallocate($cat, 255, 255, 255);
217 imagefill($cat, 0, 0, $white);
220 foreach ($parts as $part => $num) {
221 $file = dirname(__FILE__) . '/avatars/' . $part . '_' . $num . '.png';
223 $im = @imagecreatefrompng($file);
225 die('Failed to load ' . $file);
227 imageSaveAlpha($im, true);
228 imagecopy($cat, $im, 0, 0, 0, 0, CATAVATAR_SIZE, CATAVATAR_SIZE);
233 if ($size > 3 && $size < 7) {
246 $dest = imagecreatetruecolor($size, $size);
247 imagealphablending($dest, false);
248 imagesavealpha($dest, true);
249 imagecopyresampled($dest, $cat, 0, 0, 0, 0, $size, $size, CATAVATAR_SIZE, CATAVATAR_SIZE);
254 // restore random seed
259 header('Pragma: public');
260 header('Cache-Control: max-age=86400');
261 header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
262 header('Content-Type: image/jpg');
263 imagejpeg($cat, NULL, 90);