Merge pull request #583 from annando/cat-profile

Catavatar: The profile picture is now stored in a better way
This commit is contained in:
Tobias Diekershoff 2018-04-13 13:30:21 +02:00 committed by GitHub
commit ac0d42dcaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 57 deletions

View File

@ -13,6 +13,9 @@ use Friendica\Core\Worker;
use Friendica\Core\PConfig; use Friendica\Core\PConfig;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Friendica\Network\HTTPException\NotFoundException; use Friendica\Network\HTTPException\NotFoundException;
use Friendica\Model\Contact;
use Friendica\Model\Photo;
use Friendica\Database\DBM;
define("CATAVATAR_SIZE", 256); define("CATAVATAR_SIZE", 256);
@ -82,7 +85,7 @@ function catavatar_addon_settings_post(App $a, &$s)
); );
$seed = PConfig::get(local_user(), 'catavatar', 'seed', md5(trim(strtolower($user['email'])))); $seed = PConfig::get(local_user(), 'catavatar', 'seed', md5(trim(strtolower($user['email']))));
$imageurl = preg_replace('/[^A-Za-z0-9\._-]/', '', $seed); $imageurl = preg_replace('/[^A-Za-z0-9\._-]/', '', $seed);
$imageurl = substr($imageurl,0,35).''; $imageurl = substr($imageurl, 0, 35) . '';
$cachefile = get_cachefile($imageurl); $cachefile = get_cachefile($imageurl);
if ($cachefile != "" && file_exists($cachefile)) { if ($cachefile != "" && file_exists($cachefile)) {
unlink($cachefile); unlink($cachefile);
@ -90,42 +93,35 @@ function catavatar_addon_settings_post(App $a, &$s)
if (!empty($_POST['catavatar-usecat'])) { if (!empty($_POST['catavatar-usecat'])) {
$url = $a->get_baseurl() . '/catavatar/' . local_user(); $url = $a->get_baseurl() . '/catavatar/' . local_user() . '?ts=' . time();
// set the catavatar url as avatar url in contact and default profile $self = dba::selectFirst('contact', ['id'], ['uid' => local_user(), 'self' => true]);
// and set profile to 0 to current photo if (!DBM::is_result($self)) {
// I'm not sure it's the correct way to do this... notice(L10n::t("The cat hadn't found itself."));
$r = dba::update('contact', return;
['photo' => $url . '/4', 'thumb' => $url . '/5', 'micro' => $url . '/6', 'avatar-date' => DateTimeFormat::utcNow()], }
['uid' => local_user(), 'self' => 1]
); Photo::importProfilePhoto($url, local_user(), $self['id']);
if ($r===false) {
$condition = ['uid' => local_user(), 'contact-id' => $self['id']];
$photo = dba::selectFirst('photo', ['resource-id'], $condition);
if (!DBM::is_result($photo)) {
notice(L10n::t('There was an error, the cat ran away.')); notice(L10n::t('There was an error, the cat ran away.'));
return; return;
} }
$r = dba::update('profile', dba::update('photo', ['profile' => false], ['profile' => true, 'uid' => local_user()]);
['photo' => $url . '/4', 'thumb' => $url . '/5'],
['uid' => local_user(), 'is-default' => 1]
);
if ($r===false) {
notice(L10n::t('There was an error, the cat ran away.'));
return;
}
$r = dba::update('photo', $fields = ['profile' => true, 'album' => L10n::t('Profile Photos'), 'contact-id' => 0];
['profile' => 0], dba::update('photo', $fields, ['uid' => local_user(), 'resource-id' => $photo['resource-id']]);
['uid' => local_user(), 'profile' => 1]
);
if ($r === false) {
notice(L10n::t('There was an error, the cat ran away.'));
return;
}
Photo::importProfilePhoto($url, local_user(), $self['id']);
Contact::updateSelfFromUserID(local_user(), true);
// Update global directory in background // Update global directory in background
$url = $a->get_baseurl() . '/profile/' . $a->user['nickname']; $url = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
if ($url && strlen(Config::get('system','directory'))) { if ($url && strlen(Config::get('system', 'directory'))) {
Worker::add(PRIORITY_LOW, 'Directory', $url); Worker::add(PRIORITY_LOW, 'Directory', $url);
} }
@ -204,12 +200,12 @@ function catavatar_content(App $a)
throw new NotFoundException(); throw new NotFoundException();
} }
$seed = PConfig::get(local_user(), "catavatar", "seed", md5(trim(strtolower($user['email'])))); $seed = PConfig::get($uid, "catavatar", "seed", md5(trim(strtolower($user['email']))));
// from cat-avatar-generator.php // from cat-avatar-generator.php
$imageurl = $seed . "-" . $size; $imageurl = $seed . "-" . $size;
$imageurl = preg_replace('/[^A-Za-z0-9\._-]/', '', $imageurl); $imageurl = preg_replace('/[^A-Za-z0-9\._-]/', '', $imageurl);
$imageurl = substr($imageurl,0,35) . ''; $imageurl = substr($imageurl, 0, 35) . '';
$cachefile = get_cachefile($imageurl); $cachefile = get_cachefile($imageurl);
$cachetime = 604800; # 1 week (1 day = 86400) $cachetime = 604800; # 1 week (1 day = 86400)
@ -217,7 +213,7 @@ function catavatar_content(App $a)
if ($cachefile != "" && file_exists($cachefile) && (time() - $cachetime) < filemtime($cachefile)) { if ($cachefile != "" && file_exists($cachefile) && (time() - $cachetime) < filemtime($cachefile)) {
header('Pragma: public'); header('Pragma: public');
header('Cache-Control: max-age=86400'); header('Cache-Control: max-age=86400');
header('Expires: '. gmdate('D, d M Y H:i:s \G\M\T', time() + 86400)); header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
header('Content-Type: image/jpg'); header('Content-Type: image/jpg');
readfile($cachefile); readfile($cachefile);
exit(); exit();
@ -256,43 +252,53 @@ function catavatar_content(App $a)
* *
**/ **/
function build_cat($seed='', $size=0){ function build_cat($seed = '', $size = 0)
{
// init random seed // init random seed
if($seed) srand( hexdec(substr(md5($seed),0,6)) ); if ($seed) {
srand(hexdec(substr(md5($seed), 0, 6)));
}
// throw the dice for body parts // throw the dice for body parts
$parts = array( $parts = array(
'body' => rand(1,15), 'body' => rand(1, 15),
'fur' => rand(1,10), 'fur' => rand(1, 10),
'eyes' => rand(1,15), 'eyes' => rand(1, 15),
'mouth' => rand(1,10), 'mouth' => rand(1, 10),
'accessorie' => rand(1,20) 'accessorie' => rand(1, 20)
); );
// create backgound // create backgound
$cat = @imagecreatetruecolor(CATAVATAR_SIZE, CATAVATAR_SIZE) $cat = @imagecreatetruecolor(CATAVATAR_SIZE, CATAVATAR_SIZE)
or die("GD image create failed"); or die("GD image create failed");
$white = imagecolorallocate($cat, 255, 255, 255); $white = imagecolorallocate($cat, 255, 255, 255);
imagefill($cat,0,0,$white); imagefill($cat, 0, 0, $white);
// add parts // add parts
foreach($parts as $part => $num){ foreach ($parts as $part => $num){
$file = dirname(__FILE__).'/avatars/'.$part.'_'.$num.'.png'; $file = dirname(__FILE__) . '/avatars/' . $part . '_' . $num . '.png';
$im = @imagecreatefrompng($file); $im = @imagecreatefrompng($file);
if(!$im) die('Failed to load '.$file); if (!$im) {
die('Failed to load ' . $file);
}
imageSaveAlpha($im, true); imageSaveAlpha($im, true);
imagecopy($cat,$im,0,0,0,0,CATAVATAR_SIZE,CATAVATAR_SIZE); imagecopy($cat, $im, 0, 0, 0, 0, CATAVATAR_SIZE, CATAVATAR_SIZE);
imagedestroy($im); imagedestroy($im);
} }
// scale image // scale image
if ($size > 3 && $size < 7) { if ($size > 3 && $size < 7) {
switch($size) { switch ($size) {
case 4: $size = 175; break; case 4:
case 5: $size = 80; break; $size = 175;
case 6: $size = 48; break; break;
case 5:
$size = 80;
break;
case 6:
$size = 48;
break;
} }
$dest = imagecreatetruecolor($size, $size); $dest = imagecreatetruecolor($size, $size);
@ -310,10 +316,8 @@ function build_cat($seed='', $size=0){
header('Pragma: public'); header('Pragma: public');
header('Cache-Control: max-age=86400'); header('Cache-Control: max-age=86400');
header('Expires: '. gmdate('D, d M Y H:i:s \G\M\T', time() + 86400)); header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
header('Content-Type: image/jpg'); header('Content-Type: image/jpg');
imagejpeg($cat, NULL, 90); imagejpeg($cat, NULL, 90);
imagedestroy($cat); imagedestroy($cat);
} }

View File

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: \n" "Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-04-06 18:26+0200\n" "POT-Creation-Date: 2018-04-13 09:35+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -17,26 +17,34 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
#: catavatar.php:57 #: catavatar.php:60
msgid "Use Cat as Avatar" msgid "Use Cat as Avatar"
msgstr "" msgstr ""
#: catavatar.php:58 #: catavatar.php:61
msgid "More Random Cat!" msgid "More Random Cat!"
msgstr "" msgstr ""
#: catavatar.php:59 #: catavatar.php:62
msgid "Reset to email Cat" msgid "Reset to email Cat"
msgstr "" msgstr ""
#: catavatar.php:61 #: catavatar.php:64
msgid "Cat Avatar Settings" msgid "Cat Avatar Settings"
msgstr "" msgstr ""
#: catavatar.php:103 catavatar.php:112 catavatar.php:121 #: catavatar.php:100
msgid "The cat hadn't found itself."
msgstr ""
#: catavatar.php:109
msgid "There was an error, the cat ran away." msgid "There was an error, the cat ran away."
msgstr "" msgstr ""
#: catavatar.php:134 #: catavatar.php:115
msgid "Profile Photos"
msgstr ""
#: catavatar.php:130
msgid "Meow!" msgid "Meow!"
msgstr "" msgstr ""