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\Session;
14 use Friendica\Core\Worker;
15 use Friendica\Database\DBA;
17 use Friendica\Model\Contact;
18 use Friendica\Model\Photo;
19 use Friendica\Model\Profile;
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', __FILE__, 'catavatar_lookup');
30 Hook::register('addon_settings', __FILE__, 'catavatar_addon_settings');
31 Hook::register('addon_settings_post', __FILE__, 'catavatar_addon_settings_post');
33 Logger::notice('registered catavatar');
37 * Cat avatar user settings page
39 function catavatar_addon_settings(App $a, array &$data)
41 if (!Session::getLocalUser()) {
45 $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/catavatar/');
46 $html = Renderer::replaceMacros($t, [
48 '$uid' => Session::getLocalUser(),
49 '$setrandomize' => DI::l10n()->t('Set default profile avatar or randomize the cat.'),
54 'title' => DI::l10n()->t('Cat Avatar Settings'),
57 'catavatar-usecat' => DI::l10n()->t('Use Cat as Avatar'),
58 'catavatar-morecat' => DI::l10n()->t('Another random Cat!'),
59 'catavatar-emailcat' => DI::pConfig()->get(Session::getLocalUser(), 'catavatar', 'seed', false) ? DI::l10n()->t('Reset to email Cat') : null,
65 * Cat avatar user settings POST handle
67 function catavatar_addon_settings_post(App $a, &$s)
69 if (!Session::getLocalUser()) {
73 if (!empty($_POST['catavatar-usecat'])) {
74 $url = DI::baseUrl()->get() . '/catavatar/' . Session::getLocalUser() . '?ts=' . time();
76 $self = DBA::selectFirst('contact', ['id'], ['uid' => Session::getLocalUser(), 'self' => true]);
77 if (!DBA::isResult($self)) {
78 DI::sysmsg()->addNotice(DI::l10n()->t("The cat hadn't found itself."));
82 Photo::importProfilePhoto($url, Session::getLocalUser(), $self['id']);
84 $condition = ['uid' => Session::getLocalUser(), 'contact-id' => $self['id']];
85 $photo = DBA::selectFirst('photo', ['resource-id'], $condition);
86 if (!DBA::isResult($photo)) {
87 DI::sysmsg()->addNotice(DI::l10n()->t('There was an error, the cat ran away.'));
91 DBA::update('photo', ['profile' => false], ['profile' => true, 'uid' => Session::getLocalUser()]);
93 $fields = ['profile' => true, 'album' => DI::l10n()->t('Profile Photos'), 'contact-id' => 0];
94 DBA::update('photo', $fields, ['uid' => Session::getLocalUser(), 'resource-id' => $photo['resource-id']]);
96 Photo::importProfilePhoto($url, Session::getLocalUser(), $self['id']);
98 Contact::updateSelfFromUserID(Session::getLocalUser(), true);
100 // Update global directory in background
101 Profile::publishUpdate(Session::getLocalUser());
103 DI::sysmsg()->addInfo(DI::l10n()->t('Meow!'));
107 if (!empty($_POST['catavatar-morecat'])) {
108 DI::pConfig()->set(Session::getLocalUser(), 'catavatar', 'seed', time());
111 if (!empty($_POST['catavatar-emailcat'])) {
112 DI::pConfig()->delete(Session::getLocalUser(), 'catavatar', 'seed');
117 * Returns the URL to the cat avatar
122 function catavatar_lookup(App $a, array &$b)
124 $user = DBA::selectFirst('user', ['uid'], ['email' => $b['email']]);
125 if (DBA::isResult($user)) {
126 $url = DI::baseUrl()->get() . '/catavatar/' . $user['uid'];
128 $url = DI::baseUrl()->get() . '/catavatar/' . md5(trim(strtolower($b['email'])));
132 case 300: $url .= "/4"; break;
133 case 80: $url .= "/5"; break;
134 case 48: $url .= "/6"; break;
138 $b['success'] = true;
142 * This is a statement rather than an actual function definition. The simple
143 * existence of this method is checked to figure out if the addon offers a
146 function catavatar_module() {}
149 * Returns image for user id
151 * @throws NotFoundException
154 function catavatar_content(App $a)
156 if (DI::args()->getArgc() < 2 || DI::args()->getArgc() > 3) {
157 throw new NotFoundException(); // this should be catched on index and show default "not found" page.
160 if (is_numeric(DI::args()->getArgv()[1])) {
161 $uid = intval(DI::args()->getArgv()[1]);
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']))));
171 } elseif (!empty(DI::args()->getArgv()[1])) {
172 $seed = DI::args()->getArgv()[1];
174 throw new NotFoundException();
178 if (DI::args()->getArgc() == 3) {
179 $size = intval(DI::args()->getArgv()[2]);
182 // ...Or start generation
185 // render the picture:
186 build_cat($seed, $size);
194 * ====================
195 * CAT-AVATAR-GENERATOR
196 * ====================
198 * @authors: Andreas Gohr, David Revoy
200 * This PHP is licensed under the short and simple permissive:
201 * [MIT License](https://en.wikipedia.org/wiki/MIT_License)
205 function build_cat($seed = '', $size = 0)
209 srand(hexdec(substr(md5($seed), 0, 6)));
212 // throw the dice for body parts
214 'body' => rand(1, 15),
215 'fur' => rand(1, 10),
216 'eyes' => rand(1, 15),
217 'mouth' => rand(1, 10),
218 'accessorie' => rand(1, 20)
222 $cat = @imagecreatetruecolor(CATAVATAR_SIZE, CATAVATAR_SIZE)
223 or die("GD image create failed");
224 $white = imagecolorallocate($cat, 255, 255, 255);
225 imagefill($cat, 0, 0, $white);
228 foreach ($parts as $part => $num) {
229 $file = dirname(__FILE__) . '/avatars/' . $part . '_' . $num . '.png';
231 $im = @imagecreatefrompng($file);
233 die('Failed to load ' . $file);
235 imageSaveAlpha($im, true);
236 imagecopy($cat, $im, 0, 0, 0, 0, CATAVATAR_SIZE, CATAVATAR_SIZE);
241 if ($size > 3 && $size < 7) {
254 $dest = imagecreatetruecolor($size, $size);
255 imagealphablending($dest, false);
256 imagesavealpha($dest, true);
257 imagecopyresampled($dest, $cat, 0, 0, 0, 0, $size, $size, CATAVATAR_SIZE, CATAVATAR_SIZE);
262 // restore random seed
267 header('Pragma: public');
268 header('Cache-Control: max-age=86400');
269 header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
270 header('Content-Type: image/jpg');
271 imagejpeg($cat, NULL, 90);