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 * Removes the addon hook
37 function catavatar_uninstall()
39 Hook::unregister('avatar_lookup', 'addon/catavatar/catavatar.php', 'catavatar_lookup');
40 Hook::unregister('addon_settings', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings');
41 Hook::unregister('addon_settings_post', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings_post');
43 Logger::log('unregistered catavatar');
47 * Cat avatar user settings page
49 function catavatar_addon_settings(App $a, &$s)
55 $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/catavatar/');
56 $s .= Renderer::replaceMacros($t, [
57 '$postpost' => !empty($_POST['catavatar-morecat']) || !empty($_POST['catavatar-emailcat']),
59 '$uid' => local_user(),
60 '$usecat' => DI::l10n()->t('Use Cat as Avatar'),
61 '$morecat' => DI::l10n()->t('More Random Cat!'),
62 '$emailcat' => DI::l10n()->t('Reset to email Cat'),
63 '$seed' => DI::pConfig()->get(local_user(), 'catavatar', 'seed', false),
64 '$header' => DI::l10n()->t('Cat Avatar Settings'),
69 * Cat avatar user settings POST handle
71 function catavatar_addon_settings_post(App $a, &$s)
77 // delete the current cached cat avatar
78 $condition = ['uid' => local_user(), 'blocked' => false,
79 'account_expired' => false, 'account_removed' => false];
80 $user = DBA::selectFirst('user', ['email'], $condition);
82 $seed = DI::pConfig()->get(local_user(), 'catavatar', 'seed', md5(trim(strtolower($user['email']))));
84 if (!empty($_POST['catavatar-usecat'])) {
85 $url = DI::baseUrl()->get() . '/catavatar/' . local_user() . '?ts=' . time();
87 $self = DBA::selectFirst('contact', ['id'], ['uid' => local_user(), 'self' => true]);
88 if (!DBA::isResult($self)) {
89 notice(DI::l10n()->t("The cat hadn't found itself."));
93 Photo::importProfilePhoto($url, local_user(), $self['id']);
95 $condition = ['uid' => local_user(), 'contact-id' => $self['id']];
96 $photo = DBA::selectFirst('photo', ['resource-id'], $condition);
97 if (!DBA::isResult($photo)) {
98 notice(DI::l10n()->t('There was an error, the cat ran away.'));
102 DBA::update('photo', ['profile' => false], ['profile' => true, 'uid' => local_user()]);
104 $fields = ['profile' => true, 'album' => DI::l10n()->t('Profile Photos'), 'contact-id' => 0];
105 DBA::update('photo', $fields, ['uid' => local_user(), 'resource-id' => $photo['resource-id']]);
107 Photo::importProfilePhoto($url, local_user(), $self['id']);
109 Contact::updateSelfFromUserID(local_user(), true);
111 // Update global directory in background
112 $url = DI::baseUrl()->get() . '/profile/' . $a->user['nickname'];
113 if ($url && strlen(DI::config()->get('system', 'directory'))) {
114 Worker::add(PRIORITY_LOW, 'Directory', $url);
117 Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
119 info(DI::l10n()->t('Meow!'));
123 if (!empty($_POST['catavatar-morecat'])) {
124 DI::pConfig()->set(local_user(), 'catavatar', 'seed', time());
127 if (!empty($_POST['catavatar-emailcat'])) {
128 DI::pConfig()->delete(local_user(), 'catavatar', 'seed');
133 * Returns the URL to the cat avatar
138 function catavatar_lookup(App $a, &$b)
140 $user = DBA::selectFirst('user', ['uid'], ['email' => $b['email']]);
141 $url = DI::baseUrl()->get() . '/catavatar/' . $user['uid'];
144 case 300: $url .= "/4"; break;
145 case 80: $url .= "/5"; break;
146 case 47: $url .= "/6"; break;
150 $b['success'] = true;
153 function catavatar_module() {}
156 * Returns image for user id
158 * @throws NotFoundException
161 function catavatar_content(App $a)
163 if ($a->argc < 2 || $a->argc > 3) {
164 throw new NotFoundException(); // this should be catched on index and show default "not found" page.
167 $uid = intval($a->argv[1]);
171 $size = intval($a->argv[2]);
174 $condition = ['uid' => $uid,
175 'account_expired' => false, 'account_removed' => false];
176 $user = DBA::selectFirst('user', ['email'], $condition);
178 if ($user === false) {
179 throw new NotFoundException();
182 $seed = DI::pConfig()->get($uid, "catavatar", "seed", md5(trim(strtolower($user['email']))));
184 // ...Or start generation
187 // render the picture:
188 build_cat($seed, $size);
198 * ====================
199 * CAT-AVATAR-GENERATOR
200 * ====================
202 * @authors: Andreas Gohr, David Revoy
204 * This PHP is licensed under the short and simple permissive:
205 * [MIT License](https://en.wikipedia.org/wiki/MIT_License)
209 function build_cat($seed = '', $size = 0)
213 srand(hexdec(substr(md5($seed), 0, 6)));
216 // throw the dice for body parts
218 'body' => rand(1, 15),
219 'fur' => rand(1, 10),
220 'eyes' => rand(1, 15),
221 'mouth' => rand(1, 10),
222 'accessorie' => rand(1, 20)
226 $cat = @imagecreatetruecolor(CATAVATAR_SIZE, CATAVATAR_SIZE)
227 or die("GD image create failed");
228 $white = imagecolorallocate($cat, 255, 255, 255);
229 imagefill($cat, 0, 0, $white);
232 foreach ($parts as $part => $num) {
233 $file = dirname(__FILE__) . '/avatars/' . $part . '_' . $num . '.png';
235 $im = @imagecreatefrompng($file);
237 die('Failed to load ' . $file);
239 imageSaveAlpha($im, true);
240 imagecopy($cat, $im, 0, 0, 0, 0, CATAVATAR_SIZE, CATAVATAR_SIZE);
245 if ($size > 3 && $size < 7) {
258 $dest = imagecreatetruecolor($size, $size);
259 imagealphablending($dest, false);
260 imagesavealpha($dest, true);
261 imagecopyresampled($dest, $cat, 0, 0, 0, 0, $size, $size, CATAVATAR_SIZE, CATAVATAR_SIZE);
266 // restore random seed
271 header('Pragma: public');
272 header('Cache-Control: max-age=86400');
273 header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
274 header('Content-Type: image/jpg');
275 imagejpeg($cat, NULL, 90);