Catavatar: The profile picture is now stored in a better way #583

Merged
annando merged 5 commits from cat-profile into develop 2018-04-13 13:30:21 +02:00
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);
@ -90,38 +93,31 @@ 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'];
@ -204,7 +200,7 @@ 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;
@ -256,10 +252,12 @@ 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(
@ -281,7 +279,9 @@ function build_cat($seed='', $size=0){
$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);
@ -290,9 +290,15 @@ function build_cat($seed='', $size=0){
// 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);
@ -315,5 +321,3 @@ function build_cat($seed='', $size=0){
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 ""