Browse Source

change identicon library to jidenticon

main
ghost 9 months ago
parent
commit
0186d8705b
  1. 1
      README.md
  2. 3
      composer.json
  3. 8
      src/config/app.php.example
  4. 222
      src/library/icon.php
  5. 4
      src/public/explore.php
  6. 38
      src/public/file.php
  7. 4
      src/public/search.php
  8. 2
      src/public/top.php

1
README.md

@ -278,6 +278,7 @@ See also: [SQLite tree](https://github.com/YGGverse/YGGo/tree/sqliteway) @@ -278,6 +278,7 @@ See also: [SQLite tree](https://github.com/YGGverse/YGGo/tree/sqliteway)
* Engine sources [MIT License](https://github.com/YGGverse/YGGo/blob/main/LICENSE)
* Home page animation by [alvarotrigo](https://codepen.io/alvarotrigo/pen/GRvYNax)
* CLI logo by [patorjk.com](https://patorjk.com/software/taag/#p=display&f=Slant&t=YGGo!)
* [Identicons](https://github.com/dmester/jdenticon-php)
#### Feedback

3
composer.json

@ -7,7 +7,8 @@ @@ -7,7 +7,8 @@
"yggverse/parser": ">=0.1.0",
"yggverse/cache": ">=0.3.0",
"symfony/dom-crawler": "^6.3",
"symfony/css-selector": "^6.3"
"symfony/css-selector": "^6.3",
"jdenticon/jdenticon": "^1.0"
},
"license": "MIT",
"autoload": {

8
src/config/app.php.example

@ -55,14 +55,6 @@ define('WEBSITE_DOMAIN', ''); @@ -55,14 +55,6 @@ define('WEBSITE_DOMAIN', '');
*/
define('WEBSITE_PAGINATION_SEARCH_PAGE_RESULTS_LIMIT', 100);
/*
* Save ident icons to the static webp cache (placed in storage/cache) to prevent CPU overload
*
* or false - to generate every time on request
*
*/
define('WEBSITE_IDENTICON_IMAGE_CACHE', true);
// Database
define('DB_HOST', 'localhost');
define('DB_PORT', 3306);

222
src/library/icon.php

@ -1,222 +0,0 @@ @@ -1,222 +0,0 @@
<?php
final class Icon {
private $_width;
private $_height;
private $_hash;
private $_spriteZ = 800;
public function generateImageResource($hash, $width, $height, $filter = false, $radius = 0) {
$this->_width = $width;
$this->_height = $height;
$this->_hash = $hash;
$csh = hexdec(substr($this->_hash, 0, 1)); // corner sprite shape
$ssh = hexdec(substr($this->_hash, 1, 1)); // side sprite shape
$xsh = hexdec(substr($this->_hash, 2, 1)) & 7; // center sprite shape
$cro = hexdec(substr($this->_hash, 3, 1)) & 3; // corner sprite rotation
$sro = hexdec(substr($this->_hash, 4, 1)) & 3; // side sprite rotation
$xbg = hexdec(substr($this->_hash, 5, 1)) % 2; // center sprite background
$cfr = hexdec(substr($this->_hash, 6, 2));
$cfg = hexdec(substr($this->_hash, 8, 2));
$cfb = hexdec(substr($this->_hash, 10, 2));
$sfr = hexdec(substr($this->_hash, 12, 2));
$sfg = hexdec(substr($this->_hash, 14, 2));
$sfb = hexdec(substr($this->_hash, 16, 2));
$identicon = imagecreatetruecolor($this->_spriteZ * 3, $this->_spriteZ * 3);
if (function_exists('imageantialias')) {
imageantialias($identicon, TRUE);
}
$bg = imagecolorallocate($identicon, 255, 255, 255);
imagefilledrectangle($identicon, 0, 0, $this->_spriteZ, $this->_spriteZ, $bg);
$corner = $this->_getSprite($csh, $cfr, $cfg, $cfb, $cro);
imagecopy($identicon, $corner, 0, 0, 0, 0, $this->_spriteZ, $this->_spriteZ);
$corner = imagerotate($corner, 90, $bg);
imagecopy($identicon, $corner, 0, $this->_spriteZ * 2, 0, 0, $this->_spriteZ, $this->_spriteZ);
$corner = imagerotate($corner, 90, $bg);
imagecopy($identicon, $corner, $this->_spriteZ * 2, $this->_spriteZ * 2, 0, 0, $this->_spriteZ, $this->_spriteZ);
$corner = imagerotate($corner, 90, $bg);
imagecopy($identicon, $corner, $this->_spriteZ * 2, 0, 0, 0, $this->_spriteZ, $this->_spriteZ);
$side = $this->_getSprite($ssh, $sfr, $sfg, $sfb, $sro);
imagecopy($identicon, $side, $this->_spriteZ, 0, 0, 0, $this->_spriteZ, $this->_spriteZ);
$side = imagerotate($side, 90, $bg);
imagecopy($identicon, $side, 0, $this->_spriteZ, 0, 0, $this->_spriteZ, $this->_spriteZ);
$side = imagerotate($side, 90, $bg);
imagecopy($identicon, $side, $this->_spriteZ, $this->_spriteZ * 2, 0, 0, $this->_spriteZ, $this->_spriteZ);
$side = imagerotate($side, 90, $bg);
imagecopy($identicon, $side, $this->_spriteZ * 2, $this->_spriteZ, 0, 0, $this->_spriteZ, $this->_spriteZ);
$center = $this->_getCenter($xsh, $cfr, $cfg, $cfb, $sfr, $sfg, $sfb, $xbg);
imagecopy($identicon, $center, $this->_spriteZ, $this->_spriteZ, 0, 0, $this->_spriteZ, $this->_spriteZ);
imagecolortransparent($identicon, $bg);
$resized = imagecreatetruecolor($this->_width, $this->_height);
if (function_exists('imageantialias')) {
imageantialias($resized, TRUE);
}
$bg = imagecolorallocate($resized, 255, 255, 255);
imagefilledrectangle($resized, 0, 0, $this->_width, $this->_height, $bg);
imagecopyresampled($resized, $identicon, 0, 0, (imagesx($identicon) - $this->_spriteZ * 3) / 2, (imagesx($identicon) - $this->_spriteZ * 3) / 2, $this->_width, $this->_height, $this->_spriteZ * 3, $this->_spriteZ * 3);
if ($filter) {
imagefilter($resized, $filter);
}
if ($radius) {
imagearc($resized, $radius-1, $radius-1, $radius*2, $radius*2, 180, 270, $bg);
imagefilltoborder($resized, 0, 0, $bg, $bg);
imagearc($resized, $this->_width-$radius, $radius-1, $radius*2, $radius*2, 270, 0, $bg);
imagefilltoborder($resized, $this->_width-1, 0, $bg, $bg);
imagearc($resized, $radius-1, $this->_height-$radius, $radius*2, $radius*2, 90, 180, $bg);
imagefilltoborder($resized, 0, $this->_height-1, $bg, $bg);
imagearc($resized, $this->_width-$radius, $this->_height-$radius, $radius*2, $radius*2, 0, 90, $bg);
imagefilltoborder($resized, $this->_width-1, $this->_height-1, $bg, $bg);
imagecolortransparent($resized, $bg);
}
ob_start();
imagewebp($resized);
imagedestroy($resized);
return ob_get_clean();
}
private function _getSprite($shape, $R, $G, $B, $rotation) {
$sprite = imagecreatetruecolor($this->_spriteZ, $this->_spriteZ);
if (function_exists('imageantialias')) {
imageantialias($sprite, TRUE);
}
$fg = imagecolorallocate($sprite, $R, $G, $B);
$bg = imagecolorallocate($sprite, 255, 255, 255);
imagefilledrectangle($sprite, 0, 0, $this->_spriteZ, $this->_spriteZ, $bg);
switch ($shape) {
case 0: // triangle
$shape = array(0.5, 1, 1, 0, 1, 1);
break;
case 1: // parallelogram
$shape = array(0.5, 0, 1, 0, 0.5, 1, 0, 1);
break;
case 2: // mouse ears
$shape = array(0.5, 0, 1, 0, 1, 1, 0.5, 1, 1, 0.5);
break;
case 3: // ribbon
$shape = array(0, 0.5, 0.5, 0, 1, 0.5, 0.5, 1, 0.5, 0.5);
break;
case 4: // sails
$shape = array(0, 0.5, 1, 0, 1, 1, 0, 1, 1, 0.5);
break;
case 5: // fins
$shape = array(1, 0, 1, 1, 0.5, 1, 1, 0.5, 0.5, 0.5);
break;
case 6: // beak
$shape = array(0, 0, 1, 0, 1, 0.5, 0, 0, 0.5, 1, 0, 1);
break;
case 7: // chevron
$shape = array(0, 0, 0.5, 0, 1, 0.5, 0.5, 1, 0, 1, 0.5, 0.5);
break;
case 8: // fish
$shape = array(0.5, 0, 0.5, 0.5, 1, 0.5, 1, 1, 0.5, 1, 0.5, 0.5, 0, 0.5);
break;
case 9: // kite
$shape = array(0, 0, 1, 0, 0.5, 0.5, 1, 0.5, 0.5, 1, 0.5, 0.5, 0, 1);
break;
case 10: // trough
$shape = array(0, 0.5, 0.5, 1, 1, 0.5, 0.5, 0, 1, 0, 1, 1, 0, 1);
break;
case 11: // rays
$shape = array(0.5, 0, 1, 0, 1, 1, 0.5, 1, 1, 0.75, 0.5, 0.5, 1, 0.25);
break;
case 12: // double rhombus
$shape = array(0, 0.5, 0.5, 0, 0.5, 0.5, 1, 0, 1, 0.5, 0.5, 1, 0.5, 0.5, 0, 1);
break;
case 13: // crown
$shape = array(0, 0, 1, 0, 1, 1, 0, 1, 1, 0.5, 0.5, 0.25, 0.5, 0.75, 0, 0.5, 0.5, 0.25);
break;
case 14: // radioactive
$shape = array(0, 0.5, 0.5, 0.5, 0.5, 0, 1, 0, 0.5, 0.5, 1, 0.5, 0.5, 1, 0.5, 0.5, 0, 1);
break;
default: // tiles
$shape = array(0, 0, 1, 0, 0.5, 0.5, 0.5, 0, 0, 0.5, 1, 0.5, 0.5, 1, 0.5, 0.5, 0, 1);
break;
}
for ($i = 0; $i < count($shape); $i++)
$shape[$i] = $shape[$i] * $this->_spriteZ;
imagefilledpolygon($sprite, $shape, $fg);
for ($i = 0; $i < $rotation; $i++)
$sprite = imagerotate($sprite, 90, $bg);
return $sprite;
}
private function _getCenter($shape, $fR, $fG, $fB, $bR, $bG, $bB, $usebg) {
$sprite = imagecreatetruecolor($this->_spriteZ, $this->_spriteZ);
if (function_exists('imageantialias')) {
imageantialias($sprite, TRUE);
}
$fg = imagecolorallocate($sprite, $fR, $fG, $fB);
if ($usebg > 0 && (abs($fR - $bR) > 127 || abs($fG - $bG) > 127 || abs($fB - $bB) > 127))
$bg = imagecolorallocate($sprite, $bR, $bG, $bB); else
$bg = imagecolorallocate($sprite, 255, 255, 255);
imagefilledrectangle($sprite, 0, 0, $this->_spriteZ, $this->_spriteZ, $bg);
switch ($shape) {
case 0: // empty
$shape = array();
break;
case 1: // fill
$shape = array(0, 0, 1, 0, 1, 1, 0, 1);
break;
case 2: // diamond
$shape = array(0.5, 0, 1, 0.5, 0.5, 1, 0, 0.5);
break;
case 3: // reverse diamond
$shape = array(0, 0, 1, 0, 1, 1, 0, 1, 0, 0.5, 0.5, 1, 1, 0.5, 0.5, 0, 0, 0.5);
break;
case 4: // cross
$shape = array(0.25, 0, 0.75, 0, 0.5, 0.5, 1, 0.25, 1, 0.75, 0.5, 0.5, 0.75, 1, 0.25, 1, 0.5, 0.5, 0, 0.75, 0, 0.25, 0.5, 0.5);
break;
case 5: // morning star
$shape = array(0, 0, 0.5, 0.25, 1, 0, 0.75, 0.5, 1, 1, 0.5, 0.75, 0, 1, 0.25, 0.5);
break;
case 6: // small square
$shape = array(0.33, 0.33, 0.67, 0.33, 0.67, 0.67, 0.33, 0.67);
break;
case 7: // checkerboard
$shape = array(0, 0, 0.33, 0, 0.33, 0.33, 0.66, 0.33, 0.67, 0, 1, 0, 1, 0.33, 0.67, 0.33, 0.67, 0.67, 1, 0.67, 1, 1, 0.67, 1, 0.67, 0.67, 0.33, 0.67, 0.33, 1, 0, 1, 0, 0.67, 0.33, 0.67, 0.33, 0.33, 0, 0.33);
break;
}
for ($i = 0; $i < count($shape); $i++)
$shape[$i] = $shape[$i] * $this->_spriteZ;
if (count($shape) > 0)
imagefilledpolygon($sprite, $shape, $fg);
return $sprite;
}
}

4
src/public/explore.php

@ -236,7 +236,7 @@ $placeholder = Filter::plural($totalPages, [sprintf(_('Over %s page or enter the @@ -236,7 +236,7 @@ $placeholder = Filter::plural($totalPages, [sprintf(_('Over %s page or enter the
<?php } ?>
<?php } ?>
<a href="<?php echo urldecode($host->url . $hostPage->uri) ?>">
<img src="<?php echo WEBSITE_DOMAIN; ?>/file.php?type=identicon&query=<?php echo urlencode($host->name) ?>" alt="identicon" width="16" height="16" class="icon" />
<img src="<?php echo WEBSITE_DOMAIN; ?>/file.php?type=identicon&query=<?php echo urlencode(str_replace(['[',']'], false, $host->name)) ?>" alt="identicon" width="16" height="16" class="icon" />
<?php echo htmlentities(urldecode($host->url . $hostPage->uri)) ?>
</a>
</div>
@ -284,7 +284,7 @@ $placeholder = Filter::plural($totalPages, [sprintf(_('Over %s page or enter the @@ -284,7 +284,7 @@ $placeholder = Filter::plural($totalPages, [sprintf(_('Over %s page or enter the
<?php if ($host = $db->getHost($hostPage->hostId)) { ?>
<p>
<a href="<?php echo urldecode($host->url . $hostPage->uri) ?>">
<img src="<?php echo WEBSITE_DOMAIN; ?>/file.php?type=identicon&query=<?php echo urlencode($host->name) ?>" alt="identicon" width="16" height="16" class="icon" />
<img src="<?php echo WEBSITE_DOMAIN; ?>/file.php?type=identicon&query=<?php echo urlencode(str_replace(['[',']'], false, $host->name)) ?>" alt="identicon" width="16" height="16" class="icon" />
<?php echo htmlentities(urldecode($host->url) . (mb_strlen(urldecode($hostPage->uri)) > 28 ? '...' . mb_substr(urldecode($hostPage->uri), -28) : urldecode($hostPage->uri))) ?>
</a>
|

38
src/public/file.php

@ -1,9 +1,9 @@ @@ -1,9 +1,9 @@
<?php
require_once(__DIR__ . '/../config/app.php');
require_once(__DIR__ . '/../library/icon.php');
require_once(__DIR__ . '/../library/mysql.php');
require_once(__DIR__ . '/../library/ftp.php');
require_once(__DIR__ . '/../../vendor/autoload.php');
$type = !empty($_GET['type']) ? $_GET['type'] : false;
@ -11,33 +11,19 @@ switch ($type) { @@ -11,33 +11,19 @@ switch ($type) {
case 'identicon':
$query = md5($_GET['query']);
if (!empty($_GET['query']))
{
$icon = new Jdenticon\Identicon();
$width = isset($_GET['width']) ? (int) $_GET['width'] : 16;
$height = isset($_GET['height']) ? (int) $_GET['height'] : 16;
$icon->setValue(urldecode($_GET['query']));
$icon->setSize(16);
$icon->setStyle(
[
'backgroundColor' => 'rgba(255, 255, 255, 0)',
]
);
$radius = isset($_GET['radius']) ? (int) $_GET['radius'] : 0;
header('Content-Type: image/webp');
if (WEBSITE_IDENTICON_IMAGE_CACHE) {
$filename = __DIR__ . '/../storage/cache/' . $query . '.webp';
if (!file_exists($filename)) {
$icon = new Icon();
file_put_contents($filename, $icon->generateImageResource($query, $width, $height, false, $radius));
}
echo file_get_contents($filename);
} else {
$icon = new Icon();
echo $icon->generateImageResource($query, $width, $height, false, $radius);
$icon->displayImage('webp');
}
break;

4
src/public/search.php

@ -6,7 +6,7 @@ require_once(__DIR__ . '/../library/filter.php'); @@ -6,7 +6,7 @@ require_once(__DIR__ . '/../library/filter.php');
require_once(__DIR__ . '/../library/mysql.php');
require_once(__DIR__ . '/../library/helper.php');
require_once(__DIR__ . '/../library/sphinxql.php');
require_once __DIR__ . '/../../vendor/autoload.php';
require_once(__DIR__ . '/../../vendor/autoload.php');
// Connect Sphinx search server
try {
@ -327,7 +327,7 @@ if ($queueTotal = $memory->getByMethodCallback( @@ -327,7 +327,7 @@ if ($queueTotal = $memory->getByMethodCallback(
<?php } ?>
<?php } ?>
<a href="<?php echo urldecode($host->url . $hostPage->uri) ?>">
<img src="<?php echo WEBSITE_DOMAIN; ?>/file.php?type=identicon&query=<?php echo urlencode($host->name) ?>" alt="identicon" width="16" height="16" class="icon" />
<img src="<?php echo WEBSITE_DOMAIN; ?>/file.php?type=identicon&query=<?php echo urlencode(str_replace(['[',']'], false, $host->name)) ?>" alt="identicon" width="16" height="16" class="icon" />
<?php echo htmlentities(urldecode($host->url) . (mb_strlen(urldecode($hostPage->uri)) > 28 ? '...' . mb_substr(urldecode($hostPage->uri), -28) : urldecode($hostPage->uri))) ?>
</a>
|

2
src/public/top.php

@ -271,7 +271,7 @@ $placeholder = Filter::plural($totalPages, [sprintf(_('Over %s page or enter the @@ -271,7 +271,7 @@ $placeholder = Filter::plural($totalPages, [sprintf(_('Over %s page or enter the
<?php } ?>
<?php } ?>
<a href="<?php echo $topHostPage->hostPageURL ?>"title="<?php echo trim($title) ?>">
<img src="<?php echo WEBSITE_DOMAIN; ?>/file.php?type=identicon&query=<?php echo urlencode($topHostPage->name) ?>" alt="identicon" width="16" height="16" class="icon" />
<img src="<?php echo WEBSITE_DOMAIN; ?>/file.php?type=identicon&query=<?php echo urlencode(str_replace(['[',']'], false, $topHostPage->name)) ?>" alt="identicon" width="16" height="16" class="icon" />
<?php echo htmlentities(urldecode($topHostPage->hostURL) . (mb_strlen(urldecode($topHostPage->uri)) > 16 ? '...' . mb_substr(urldecode($topHostPage->uri), -16) : urldecode($topHostPage->uri))) ?>
</a>
</td>

Loading…
Cancel
Save