3 * Name: Bird Avatar Generator
4 * Description: Generate a default avatar based on David Revoy's bird-avatar-generator https://www.peppercarrot.com/extras/html/2019_bird-generator/index.php
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\Database\DBA;
15 use Friendica\Model\Contact;
16 use Friendica\Model\Photo;
17 use Friendica\Model\Profile;
18 use Friendica\Network\HTTPException\NotFoundException;
20 define("BIRDAVATAR_SIZE", 256);
23 * Installs the addon hook
25 function birdavatar_install()
27 Hook::register('avatar_lookup', __FILE__, 'birdavatar_lookup');
28 Hook::register('addon_settings', __FILE__, 'birdavatar_addon_settings');
29 Hook::register('addon_settings_post', __FILE__, 'birdavatar_addon_settings_post');
31 Logger::info('registered birdavatar');
35 * Bird avatar user settings page
37 function birdavatar_addon_settings(App $a, array &$data)
39 if (!DI::userSession()->getLocalUserId()) {
43 $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/birdavatar/');
44 $html = Renderer::replaceMacros($t, [
46 '$uid' => DI::userSession()->getLocalUserId(),
47 '$setrandomize' => DI::l10n()->t('Set default profile avatar or randomize the bird.'),
51 'addon' => 'birdavar',
52 'title' => DI::l10n()->t('Bird Avatar Settings'),
55 'birdavatar-usebird' => DI::l10n()->t('Use Bird as Avatar'),
56 'birdavatar-morebird' => DI::l10n()->t('More Random Bird!'),
57 'birdavatar-emailbird' => DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'birdavatar', 'seed', false) ? DI::l10n()->t('Reset to email Bird') : null,
63 * Bird avatar user settings POST handle
65 function birdavatar_addon_settings_post(App $a, &$s)
67 if (!DI::userSession()->getLocalUserId()) {
71 if (!empty($_POST['birdavatar-usebird'])) {
72 $url = DI::baseUrl()->get() . '/birdavatar/' . DI::userSession()->getLocalUserId() . '?ts=' . time();
74 $self = DBA::selectFirst('contact', ['id'], ['uid' => DI::userSession()->getLocalUserId(), 'self' => true]);
75 if (!DBA::isResult($self)) {
76 DI::sysmsg()->addNotice(DI::l10n()->t("The bird has not found itself."));
80 Photo::importProfilePhoto($url, DI::userSession()->getLocalUserId(), $self['id']);
82 $condition = ['uid' => DI::userSession()->getLocalUserId(), 'contact-id' => $self['id']];
83 $photo = DBA::selectFirst('photo', ['resource-id'], $condition);
84 if (!DBA::isResult($photo)) {
85 DI::sysmsg()->addNotice(DI::l10n()->t('There was an error, the bird flew away.'));
89 DBA::update('photo', ['profile' => false], ['profile' => true, 'uid' => DI::userSession()->getLocalUserId()]);
91 $fields = ['profile' => true, 'album' => DI::l10n()->t('Profile Photos'), 'contact-id' => 0];
92 DBA::update('photo', $fields, ['uid' => DI::userSession()->getLocalUserId(), 'resource-id' => $photo['resource-id']]);
94 Photo::importProfilePhoto($url, DI::userSession()->getLocalUserId(), $self['id']);
96 Contact::updateSelfFromUserID(DI::userSession()->getLocalUserId(), true);
98 // Update global directory in background
99 Profile::publishUpdate(DI::userSession()->getLocalUserId());
101 DI::sysmsg()->addInfo(DI::l10n()->t('Meow!'));
105 if (!empty($_POST['birdavatar-morebird'])) {
106 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'birdavatar', 'seed', time());
109 if (!empty($_POST['birdavatar-emailbird'])) {
110 DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'birdavatar', 'seed');
115 * Returns the URL to the bird avatar
120 function birdavatar_lookup(App $a, array &$b)
122 $user = DBA::selectFirst('user', ['uid'], ['email' => $b['email']]);
123 if (DBA::isResult($user)) {
124 $url = DI::baseUrl()->get() . '/birdavatar/' . $user['uid'];
126 $url = DI::baseUrl()->get() . '/birdavatar/' . md5(trim(strtolower($b['email'])));
129 switch ($b['size']) {
130 case 300: $url .= "/4"; break;
131 case 80: $url .= "/5"; break;
132 case 48: $url .= "/6"; break;
136 $b['success'] = true;
140 * This is a statement rather than an actual function definition. The simple
141 * existence of this method is checked to figure out if the addon offers a
144 function birdavatar_module() {}
147 * Returns image for user id
149 * @throws NotFoundException
152 function birdavatar_content(App $a)
154 if (DI::args()->getArgc() < 2 || DI::args()->getArgc() > 3) {
155 throw new NotFoundException(); // this should be catched on index and show default "not found" page.
158 if (is_numeric(DI::args()->getArgv()[1])) {
159 $uid = intval(DI::args()->getArgv()[1]);
160 $condition = ['uid' => $uid,
161 'account_expired' => false, 'account_removed' => false];
162 $user = DBA::selectFirst('user', ['email'], $condition);
164 if ($user === false) {
165 throw new NotFoundException();
168 $seed = DI::pConfig()->get($uid, "birdavatar", "seed", md5(trim(strtolower($user['email']))));
169 } elseif (!empty(DI::args()->getArgv()[1])) {
170 $seed = DI::args()->getArgv()[1];
172 throw new NotFoundException();
176 if (DI::args()->getArgc() == 3) {
177 $size = intval(DI::args()->getArgv()[2]);
183 // render the picture:
184 build_bird($seed, $size);
192 * ====================
193 * BIRD-AVATAR-GENERATOR
194 * ====================
196 * @authors: Andreas Gohr, David Revoy
198 * This PHP is licensed under the short and simple permissive:
199 * [MIT License](https://en.wikipedia.org/wiki/MIT_License)
203 function build_bird($seed = '', $size = 0)
207 srand(hexdec(substr(md5($seed), 0, 6)));
210 // throw the dice for body parts
213 'hoop' => rand(1,10),
218 'accessorie' => rand(1,20)
222 $bird = @imagecreatetruecolor(BIRDAVATAR_SIZE, BIRDAVATAR_SIZE)
223 or die("GD image create failed");
224 $white = imagecolorallocate($bird, 255, 255, 255);
225 imagefill($bird, 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($bird, $im, 0, 0, 0, 0, BIRDAVATAR_SIZE, BIRDAVATAR_SIZE);
241 if ($size > 3 && $size < 7) {
254 $dest = imagecreatetruecolor($size, $size) or die("GD image create failed");
255 imagealphablending($dest, false);
256 imagesavealpha($dest, true);
257 imagecopyresampled($dest, $bird, 0, 0, 0, 0, $size, $size, BIRDAVATAR_SIZE, BIRDAVATAR_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($bird, null, 90);