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\Model\Profile;
19 use Friendica\Network\HTTPException\NotFoundException;
21 define("CATAVATAR_SIZE", 256);
24 * Installs the addon hook
26 function catavatar_install()
28 Hook::register('avatar_lookup', __FILE__, 'catavatar_lookup');
29 Hook::register('addon_settings', __FILE__, 'catavatar_addon_settings');
30 Hook::register('addon_settings_post', __FILE__, 'catavatar_addon_settings_post');
32 Logger::notice('registered catavatar');
36 * Cat avatar user settings page
38 function catavatar_addon_settings(App $a, array &$data)
44 $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/catavatar/');
45 $html = Renderer::replaceMacros($t, [
47 '$uid' => local_user(),
48 '$setrandomize' => DI::l10n()->t('Set default profile avatar or randomize the cat.'),
53 'title' => DI::l10n()->t('Cat Avatar Settings'),
56 'catavatar-usecat' => DI::l10n()->t('Use Cat as Avatar'),
57 'catavatar-morecat' => DI::l10n()->t('Another random Cat!'),
58 'catavatar-emailcat' => DI::pConfig()->get(local_user(), 'catavatar', 'seed', false) ? DI::l10n()->t('Reset to email Cat') : null,
64 * Cat avatar user settings POST handle
66 function catavatar_addon_settings_post(App $a, &$s)
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 Profile::publishUpdate(local_user());
102 info(DI::l10n()->t('Meow!'));
106 if (!empty($_POST['catavatar-morecat'])) {
107 DI::pConfig()->set(local_user(), 'catavatar', 'seed', time());
110 if (!empty($_POST['catavatar-emailcat'])) {
111 DI::pConfig()->delete(local_user(), 'catavatar', 'seed');
116 * Returns the URL to the cat avatar
121 function catavatar_lookup(App $a, array &$b)
123 $user = DBA::selectFirst('user', ['uid'], ['email' => $b['email']]);
124 if (DBA::isResult($user)) {
125 $url = DI::baseUrl()->get() . '/catavatar/' . $user['uid'];
127 $url = DI::baseUrl()->get() . '/catavatar/' . md5(trim(strtolower($b['email'])));
131 case 300: $url .= "/4"; break;
132 case 80: $url .= "/5"; break;
133 case 48: $url .= "/6"; break;
137 $b['success'] = true;
141 * This is a statement rather than an actual function definition. The simple
142 * existence of this method is checked to figure out if the addon offers a
145 function catavatar_module() {}
148 * Returns image for user id
150 * @throws NotFoundException
153 function catavatar_content(App $a)
155 if (DI::args()->getArgc() < 2 || DI::args()->getArgc() > 3) {
156 throw new NotFoundException(); // this should be catched on index and show default "not found" page.
159 if (is_numeric(DI::args()->getArgv()[1])) {
160 $uid = intval(DI::args()->getArgv()[1]);
161 $condition = ['uid' => $uid,
162 'account_expired' => false, 'account_removed' => false];
163 $user = DBA::selectFirst('user', ['email'], $condition);
165 if ($user === false) {
166 throw new NotFoundException();
169 $seed = DI::pConfig()->get($uid, "catavatar", "seed", md5(trim(strtolower($user['email']))));
170 } elseif (!empty(DI::args()->getArgv()[1])) {
171 $seed = DI::args()->getArgv()[1];
173 throw new NotFoundException();
177 if (DI::args()->getArgc() == 3) {
178 $size = intval(DI::args()->getArgv()[2]);
181 // ...Or start generation
184 // render the picture:
185 build_cat($seed, $size);
193 * ====================
194 * CAT-AVATAR-GENERATOR
195 * ====================
197 * @authors: Andreas Gohr, David Revoy
199 * This PHP is licensed under the short and simple permissive:
200 * [MIT License](https://en.wikipedia.org/wiki/MIT_License)
204 function build_cat($seed = '', $size = 0)
208 srand(hexdec(substr(md5($seed), 0, 6)));
211 // throw the dice for body parts
213 'body' => rand(1, 15),
214 'fur' => rand(1, 10),
215 'eyes' => rand(1, 15),
216 'mouth' => rand(1, 10),
217 'accessorie' => rand(1, 20)
221 $cat = @imagecreatetruecolor(CATAVATAR_SIZE, CATAVATAR_SIZE)
222 or die("GD image create failed");
223 $white = imagecolorallocate($cat, 255, 255, 255);
224 imagefill($cat, 0, 0, $white);
227 foreach ($parts as $part => $num) {
228 $file = dirname(__FILE__) . '/avatars/' . $part . '_' . $num . '.png';
230 $im = @imagecreatefrompng($file);
232 die('Failed to load ' . $file);
234 imageSaveAlpha($im, true);
235 imagecopy($cat, $im, 0, 0, 0, 0, CATAVATAR_SIZE, CATAVATAR_SIZE);
240 if ($size > 3 && $size < 7) {
253 $dest = imagecreatetruecolor($size, $size);
254 imagealphablending($dest, false);
255 imagesavealpha($dest, true);
256 imagecopyresampled($dest, $cat, 0, 0, 0, 0, $size, $size, CATAVATAR_SIZE, CATAVATAR_SIZE);
261 // restore random seed
266 header('Pragma: public');
267 header('Cache-Control: max-age=86400');
268 header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
269 header('Content-Type: image/jpg');
270 imagejpeg($cat, NULL, 90);