-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit dc52a5e
Showing
14 changed files
with
387 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 4 | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = false | ||
|
||
[*.{vue,js,scss}] | ||
charset = utf-8 | ||
indent_style = space | ||
indent_size = 2 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
* text=auto | ||
|
||
/tests export-ignore | ||
.gitattributes export-ignore | ||
.gitignore export-ignore | ||
.scrutinizer.yml export-ignore | ||
.travis.yml export-ignore | ||
phpunit.php export-ignore | ||
phpunit.xml.dist export-ignore | ||
phpunit.xml export-ignore | ||
.php_cs export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.idea | ||
*.DS_Store | ||
/vendor | ||
/coverage | ||
sftp-config.json | ||
composer.lock | ||
.subsplit | ||
.php_cs.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
$header = <<<EOF | ||
This file is part of the valiner/identicon-avatar. | ||
(c) valiner <[email protected]> | ||
This source file is subject to the MIT license that is bundled. | ||
EOF; | ||
|
||
return PhpCsFixer\Config::create() | ||
->setRiskyAllowed(true) | ||
->setRules(array( | ||
'@Symfony' => true, | ||
'header_comment' => array('header' => $header), | ||
'array_syntax' => array('syntax' => 'short'), | ||
'ordered_imports' => true, | ||
'no_useless_else' => true, | ||
'no_useless_return' => true, | ||
'php_unit_construct' => true, | ||
'php_unit_strict' => true, | ||
)) | ||
->setFinder( | ||
PhpCsFixer\Finder::create() | ||
->exclude('vendor') | ||
->in(__DIR__) | ||
) | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<h1 align="center"> identicon-avatar </h1> | ||
|
||
<p align="center"> php生成identicon头像.</p> | ||
|
||
|
||
## Installing | ||
|
||
```shell | ||
$ composer require valiner/identicon-avatar -vvv | ||
``` | ||
|
||
## Usage | ||
|
||
TODO | ||
|
||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "valiner\/identicon-avatar", | ||
"description": "php生成identicon头像", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "valiner", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"phpunit/phpunit": "^7.5" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Valiner\\IdenticonAvatar\\": "./src/" | ||
} | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Valiner\\IdenticonAvatar\\ServiceProvider" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false"> | ||
<testsuites> | ||
<testsuite name="Application Test Suite"> | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">src/</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
namespace Valiner\IdenticonAvatar\Generator; | ||
|
||
class Generator | ||
{ | ||
protected $generatedImage; | ||
|
||
public function __construct() | ||
{ | ||
if (!extension_loaded('gd')) { | ||
throw new Exception('请安装gd库'); | ||
} | ||
} | ||
|
||
/** | ||
* 获取图片资源 | ||
* @param $matrixArr | ||
* @param $size | ||
* @param $color | ||
* @return resource | ||
*/ | ||
public function getImageResource($matrixArr,$size,$color){ | ||
$this->generatedImage = imagecreatetruecolor($size, $size); | ||
$perPixel = (int)$size/5; | ||
|
||
$background = imagecolorallocate($this->generatedImage, 255, 255, 255); | ||
imagefill($this->generatedImage, 0, 0, $background); | ||
$gdColor = imagecolorallocate($this->generatedImage, $color[0], $color[1], $color[2]); | ||
|
||
foreach ($matrixArr as $lineKey => $lineValue) { | ||
foreach ($lineValue as $colKey => $colValue) { | ||
if (true === $colValue) { | ||
imagefilledrectangle($this->generatedImage, $colKey * $perPixel, $lineKey * $perPixel, ($colKey + 1) * $perPixel, ($lineKey + 1) * $perPixel, $gdColor); | ||
} | ||
} | ||
} | ||
return $this->generatedImage; | ||
} | ||
|
||
/** | ||
* 获取图片 | ||
* @param $matrixArr | ||
* @param $size | ||
* @param $color | ||
* @return false|string | ||
*/ | ||
public function getImage($matrixArr,$size,$color) | ||
{ | ||
$imageResource = $this->getImageResource($matrixArr,$size,$color); | ||
ob_start(); | ||
imagepng($imageResource); | ||
$imageData = ob_get_contents(); | ||
ob_end_clean(); | ||
return $imageData; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Valiner\IdenticonAvatar; | ||
|
||
use Valiner\IdenticonAvatar\Matrix\Matrix; | ||
use Valiner\IdenticonAvatar\Generator\Generator; | ||
|
||
class Identicon | ||
{ | ||
protected $matrix = []; | ||
protected $generator = ''; | ||
|
||
public function __construct() | ||
{ | ||
$this->matrix = new Matrix(); | ||
$this->generator = new generator(); | ||
} | ||
|
||
/** | ||
* 输出图片到浏览器 | ||
* @param $str | ||
* @param int $size | ||
*/ | ||
public function getAvatar($str, $size = 125) | ||
{ | ||
$matrixArr = $this->matrix->getMatrix($str); | ||
header('Content-Type: image/png'); | ||
echo $this->generator->getImage($matrixArr, $size, $this->matrix->getColor()); | ||
|
||
} | ||
|
||
/** | ||
* 保存图片 | ||
* @param $str | ||
* @param int $size | ||
* @param string $path | ||
* @throws \Exception | ||
*/ | ||
public function saveAvatar($str, $size = 125, $path = '') | ||
{ | ||
if (!$path) { | ||
throw new \Exception('路径不能为空'); | ||
} | ||
$matrixArr = $this->matrix->getMatrix($str); | ||
imagepng($this->generator->getImageResource($matrixArr, $size, $this->matrix->getColor()), $path); | ||
} | ||
|
||
/** | ||
* 获取base64 | ||
* @param $str | ||
* @param int $size | ||
* @return string | ||
*/ | ||
public function getAvatarDataUri($str, $size = 125) | ||
{ | ||
$matrixArr = $this->matrix->getMatrix($str); | ||
$imageData = $this->generator->getImage($matrixArr, $size, $this->matrix->getColor()); | ||
return sprintf('data:%s;base64,%s', 'image/png', base64_encode($imageData)); | ||
} | ||
} | ||
|
||
$ide = new Identicon(); | ||
$ide->getAvatarDataUri('sdp', 25); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace Valiner\IdenticonAvatar\Matrix; | ||
|
||
class Matrix | ||
{ | ||
|
||
protected $color = []; | ||
|
||
/** | ||
* 获取图片矩阵 | ||
* @param $str | ||
* @param int $size | ||
* @return array | ||
*/ | ||
public function getMatrix($str, $size = 64) | ||
{ | ||
$hex = md5($str); | ||
$hexArr = str_split($hex, 2); | ||
$this->setColor($this->converColor($hex)); | ||
foreach ($hexArr as $line => $val) { | ||
$hexArr[$line] = (bool)round(hexdec($val) / pow(2, 7)); | ||
} | ||
$matrix = $this->getMatrixArr($hexArr); | ||
return $matrix; | ||
} | ||
|
||
/** | ||
* 获取背景色 | ||
* @param $hexadecimal | ||
* @return array | ||
*/ | ||
public function converColor($hexadecimal) | ||
{ | ||
return [base_convert(substr($hexadecimal, 0, 2), 16, 10), | ||
base_convert(substr($hexadecimal, 2, 2), 16, 10), | ||
base_convert(substr($hexadecimal, 4, 2), 16, 10)]; | ||
} | ||
|
||
|
||
/** | ||
* 组装矩阵 | ||
* @param $hexArr | ||
* @return array | ||
*/ | ||
public function getMatrixArr($hexArr) | ||
{ | ||
$matrixLeft = []; | ||
$matrix = []; | ||
for ($i = 0; $i < count($hexArr) - 1; $i++) { | ||
$matrixLeft[$i / 3][$i % 3] = $hexArr[$i]; | ||
} | ||
|
||
array_unshift($matrixLeft, null); | ||
$turnMatrixLeft = array_reverse(call_user_func_array("array_map", $matrixLeft)); | ||
array_unshift($turnMatrixLeft, null); | ||
$matrixRight = call_user_func_array("array_map", $turnMatrixLeft); | ||
array_shift($matrixLeft); | ||
|
||
for ($i = 0; $i < count($matrixLeft); $i++) { | ||
for ($j = 0; $j < count($matrixLeft); $j++) { | ||
if ($j <= count($matrixLeft) / 2) { | ||
$matrix[$i][$j] = $matrixLeft[$i][$j]; | ||
} else { | ||
$matrix[$i][$j] = $matrixRight[$i][$j - count($matrixLeft[$i]) + 1]; | ||
} | ||
} | ||
} | ||
return $matrix; | ||
} | ||
|
||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getColor(): array | ||
{ | ||
return $this->color; | ||
} | ||
|
||
/** | ||
* @param array $color | ||
*/ | ||
public function setColor(array $color): void | ||
{ | ||
$this->color = $color; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Valiner\IdenticonAvatar; | ||
|
||
|
||
class ServiceProvider extends \Illuminate\Support\ServiceProvider | ||
{ | ||
protected $defer = true; | ||
|
||
public function register() | ||
{ | ||
$this->app->singleton(Identicon::class, function(){ | ||
return new Identicon(); | ||
}); | ||
|
||
$this->app->alias(Identicon::class, 'identicon'); | ||
} | ||
|
||
public function provides() | ||
{ | ||
return [Identicon::class, 'identicon']; | ||
} | ||
} |
Empty file.
Oops, something went wrong.