Skip to content

Commit

Permalink
Merge pull request #1288 from proditis/master
Browse files Browse the repository at this point in the history
Better avatar generation with support for future expansions
  • Loading branch information
proditis authored Nov 5, 2024
2 parents 1562ac4 + cddb55c commit 7b3c491
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 13 deletions.
3 changes: 2 additions & 1 deletion docs/Sysconfig-Keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
* `event_end_notification_title`: Title to be used for a notification when the event ends
* `event_end_notification_body`: The body that will be used to send a notification to all players when the event ends
* `plus_writeups`: Number to add to the headshots to allow for writeup activations (eg. a value of `2` means that the player can have `player_headshots+2` writeups active at most). A value of `0` means that the player can have only as many writeups active as its own number of headshots.

* `avatar_generator`: If set to `Identicon` it will use that instead of Robohash
* `avatar_robohash_set`: Choose the set for when robohash is configured
* `mail_verification_token_validity`: How long will the mail verification tokens be active for. Can take intervals supported by php and `INTERVAL`, eg. 10 day, meaning 10 days from now
* `password_reset_token_validity`: How long will the password reset tokens be active for. Can take intervals supported by php and `INTERVAL`, eg. 10 day, meaning 10 days from now

Expand Down
2 changes: 1 addition & 1 deletion frontend/commands/GeneratorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public function actionAvatar($active = true, $atomic = true)
$dst_img = \Yii::getAlias('@app/web/images/avatars/' . $player->profile->id . '.png');
if ($player->profile->avatar === 'default.png' || !$player->profile->avatar || !file_exists($dst_img)) {
echo "Generating " . $player->username . " profile avatar image.\n";
$robohash = new \app\models\Robohash($player->profile->id, 'set1');
$robohash = new \app\components\generators\AvatarGenerator($player->profile->id);
$image = $robohash->generate_image();
if (get_resource_type($image) === 'gd') {
imagepng($image, $dst_img);
Expand Down
42 changes: 42 additions & 0 deletions frontend/components/generators/AvatarGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace app\components\generators;

class AvatarGenerator
{
public $text;
public $color;
public $set;

// WxH
public $size;
public $width;
public $height;
public $cache;
public $image_dir;

private $hash_index = 0,
$hash_list = [];

const IMAGE_WIDTH = 300,
IMAGE_HEIGHT = 300;
private $generator;
public function __construct($text, $set = null, $color = null)
{
$this->text = $text;
$this->set = $set;
$this->color = $color;
$this->width = self::IMAGE_WIDTH;
$this->height = self::IMAGE_WIDTH;
if (\Yii::$app->sys->avatar_generator === 'Identicon') {
$this->generator = new \app\components\generators\Identicon($this->text, $this->set);
} else {
$this->generator = new \app\components\generators\Robohash($this->text, $this->set);
}
}

public function generate_image()
{
return $this->generator->generate_image();
}
}
27 changes: 27 additions & 0 deletions frontend/components/generators/Identicon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace app\components\generators;
class Identicon {
public $text;
public $color;
public $set;
public $width;
public $height;
const IMAGE_WIDTH = 300,
IMAGE_HEIGHT = 300;
private $model;
public function __construct($text, $set = null, $color = null)
{
$this->text = $text;
$this->set = $set;
$this->color = $color;
$this->width = self::IMAGE_WIDTH;
$this->height = self::IMAGE_WIDTH;
$this->model=new \Identicon\Identicon();
}

public function generate_image()
{
return $this->model->getImageResource($this->text,self::IMAGE_WIDTH,$this->color);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// https://github.com/hush2/php-robohash.git

// Extra sets and images from https://github.com/e1ven/Robohash.git
namespace app\models;
namespace app\components\generators;

use yii\base\Widget;
use yii\helpers\Html;
Expand Down Expand Up @@ -40,7 +40,8 @@ public function __construct($text,$set=null,$color=null)
{
$this->image_dir = \Yii::getAlias('@app/web/images/robohash/');
$this->text=$text;
$this->set=$set;
if($set===null)
$this->set=\Yii::$app->sys->avatar_robohash_set;
$this->color=$color;
$this->width = self::IMAGE_WIDTH;
$this->height = self::IMAGE_WIDTH;
Expand Down
5 changes: 2 additions & 3 deletions frontend/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
"yiisoft/yii2-bootstrap4": "^2.0",
"overals/yii2-whois": "~1.0.0",
"stripe/stripe-php": "^12.0",
"yiisoft/yii2-symfonymailer": "^4.0.0"
},
"require-dev": {
"yiisoft/yii2-symfonymailer": "^4.0.0",
"yzalis/identicon": "^2.0"
},
"config": {
"platform-check": false,
Expand Down
11 changes: 6 additions & 5 deletions frontend/models/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ public function generatePasswordResetToken()
{
if (($model = PlayerToken::findOne(['player_id' => $this->id, 'type' => 'password_reset'])) === null) {
$model = new PlayerToken();
$validity=(\Yii::$app->sys->password_reset_token_validity===false) ? '10 day':\Yii::$app->sys->password_reset_token_validity;
$validity = (\Yii::$app->sys->password_reset_token_validity === false) ? '10 day' : \Yii::$app->sys->password_reset_token_validity;
$model->player_id = $this->id;
$model->type = 'password_reset';
$model->expires_at = \Yii::$app->formatter->asDatetime(new \DateTime('NOW + '.$validity), 'php:Y-m-d H:i:s');
$model->expires_at = \Yii::$app->formatter->asDatetime(new \DateTime('NOW + ' . $validity), 'php:Y-m-d H:i:s');
$model->token = str_replace('_', '-', Yii::$app->security->generateRandomString(30));
$model->save();
}
Expand All @@ -208,10 +208,10 @@ public function generateEmailVerificationToken()
{
if (($model = PlayerToken::findOne(['player_id' => $this->id, 'type' => 'email_verification'])) === null) {
$model = new PlayerToken();
$validity=(\Yii::$app->sys->mail_verification_token_validity===false) ? '10 day':\Yii::$app->sys->mail_verification_token_validity;
$validity = (\Yii::$app->sys->mail_verification_token_validity === false) ? '10 day' : \Yii::$app->sys->mail_verification_token_validity;
$model->player_id = $this->id;
$model->type = 'email_verification';
$model->expires_at = \Yii::$app->formatter->asDatetime(new \DateTime('NOW + '.$validity), 'php:Y-m-d H:i:s');
$model->expires_at = \Yii::$app->formatter->asDatetime(new \DateTime('NOW + ' . $validity), 'php:Y-m-d H:i:s');
$model->token = str_replace('_', '-', Yii::$app->security->generateRandomString(30));
$model->save();
}
Expand Down Expand Up @@ -370,7 +370,8 @@ public function genAvatar()
}
if (file_exists($avatarPNG))
return;
$robohash = new \app\models\Robohash($_pID, 'set1');

$robohash = new \app\components\generators\AvatarGenerator($_pID);
$image = $robohash->generate_image();
if ((gettype($image) === "object" && get_class($image) === "GdImage") || ((int) phpversion() === 7 && gettype($image) === 'resource')) {
imagepng($image, $avatarPNG);
Expand Down
1 change: 0 additions & 1 deletion frontend/themes/material/profile/_profile_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use app\models\Country;
use app\models\Avatar;

$this->_fluid="-fluid";
?>
Expand Down

0 comments on commit 7b3c491

Please sign in to comment.