added identicon library, including license, regenerated autoloader
This commit is contained in:
parent
1ef28d7a5c
commit
461aed8573
27
LICENSE.md
27
LICENSE.md
|
@ -4,8 +4,9 @@ PrivateBin consists of PHP and JS code which was originally written by Sébastie
|
|||
Sauvage in 2012 and falls unter the Zlib/libpng license. Also included are
|
||||
libraries that fall under the GPLv2 (SJCL, rawinflate, rawdeflate), BSD
|
||||
2-clause (SJCL), BSD 3-clause (base64.js version 2.1.9, Showdown), MIT
|
||||
(base64.js version 1.7, Bootstrap), Apache (prettify.js) and CC-BY (favicon,
|
||||
icon, logo) licenses. All of these license terms can be found here below:
|
||||
(base64.js version 1.7, Bootstrap, Identicon), Apache (prettify.js) and CC-BY
|
||||
(favicon, icon, logo) licenses. All of these license terms can be found here
|
||||
below:
|
||||
|
||||
## Zlib/libpng license for PrivateBin
|
||||
|
||||
|
@ -440,6 +441,28 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
## MIT License for Identicon
|
||||
|
||||
Copyright © 2013 Benjamin Laugueux <benjamin@yzalis.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
## Apache License for prettify.js
|
||||
|
||||
_Version 2.0, January 2004_
|
||||
|
|
|
@ -5,4 +5,29 @@
|
|||
$vendorDir = dirname(dirname(__FILE__));
|
||||
$baseDir = dirname($vendorDir);
|
||||
|
||||
return array();
|
||||
return array(
|
||||
'Identicon\\Generator\\BaseGenerator' => $vendorDir . '/yzalis/identicon/src/Identicon/Generator/BaseGenerator.php',
|
||||
'Identicon\\Generator\\GdGenerator' => $vendorDir . '/yzalis/identicon/src/Identicon/Generator/GdGenerator.php',
|
||||
'Identicon\\Generator\\GeneratorInterface' => $vendorDir . '/yzalis/identicon/src/Identicon/Generator/GeneratorInterface.php',
|
||||
'Identicon\\Generator\\ImageMagickGenerator' => $vendorDir . '/yzalis/identicon/src/Identicon/Generator/ImageMagickGenerator.php',
|
||||
'Identicon\\Identicon' => $vendorDir . '/yzalis/identicon/src/Identicon/Identicon.php',
|
||||
'PrivateBin\\Configuration' => $baseDir . '/lib/Configuration.php',
|
||||
'PrivateBin\\Data\\AbstractData' => $baseDir . '/lib/Data/AbstractData.php',
|
||||
'PrivateBin\\Data\\Database' => $baseDir . '/lib/Data/Database.php',
|
||||
'PrivateBin\\Data\\Filesystem' => $baseDir . '/lib/Data/Filesystem.php',
|
||||
'PrivateBin\\Filter' => $baseDir . '/lib/Filter.php',
|
||||
'PrivateBin\\I18n' => $baseDir . '/lib/I18n.php',
|
||||
'PrivateBin\\Model' => $baseDir . '/lib/Model.php',
|
||||
'PrivateBin\\Model\\AbstractModel' => $baseDir . '/lib/Model/AbstractModel.php',
|
||||
'PrivateBin\\Model\\Comment' => $baseDir . '/lib/Model/Comment.php',
|
||||
'PrivateBin\\Model\\Paste' => $baseDir . '/lib/Model/Paste.php',
|
||||
'PrivateBin\\Persistence\\AbstractPersistence' => $baseDir . '/lib/Persistence/AbstractPersistence.php',
|
||||
'PrivateBin\\Persistence\\PurgeLimiter' => $baseDir . '/lib/Persistence/PurgeLimiter.php',
|
||||
'PrivateBin\\Persistence\\ServerSalt' => $baseDir . '/lib/Persistence/ServerSalt.php',
|
||||
'PrivateBin\\Persistence\\TrafficLimiter' => $baseDir . '/lib/Persistence/TrafficLimiter.php',
|
||||
'PrivateBin\\PrivateBin' => $baseDir . '/lib/PrivateBin.php',
|
||||
'PrivateBin\\Request' => $baseDir . '/lib/Request.php',
|
||||
'PrivateBin\\Sjcl' => $baseDir . '/lib/Sjcl.php',
|
||||
'PrivateBin\\View' => $baseDir . '/lib/View.php',
|
||||
'PrivateBin\\Vizhash16x16' => $baseDir . '/lib/Vizhash16x16.php',
|
||||
);
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
<?php
|
||||
|
||||
// autoload_files.php @generated by Composer
|
||||
|
||||
$vendorDir = dirname(dirname(__FILE__));
|
||||
$baseDir = dirname($vendorDir);
|
||||
|
||||
return array();
|
|
@ -6,10 +6,5 @@ $vendorDir = dirname(dirname(__FILE__));
|
|||
$baseDir = dirname($vendorDir);
|
||||
|
||||
return array(
|
||||
'Psr\\Log\\' => array($vendorDir . '/psr/log'),
|
||||
'Prophecy\\' => array($vendorDir . '/phpspec/prophecy/src'),
|
||||
'Guzzle\\Tests' => array($vendorDir . '/guzzle/guzzle/tests'),
|
||||
'Guzzle' => array($vendorDir . '/guzzle/guzzle/src'),
|
||||
'CodeClimate\\Component' => array($vendorDir . '/codeclimate/php-test-reporter/src'),
|
||||
'CodeClimate\\Bundle' => array($vendorDir . '/codeclimate/php-test-reporter/src'),
|
||||
'Identicon' => array($vendorDir . '/yzalis/identicon/src'),
|
||||
);
|
||||
|
|
|
@ -47,24 +47,6 @@ class ComposerAutoloaderInitDontChange
|
|||
|
||||
$loader->register(true);
|
||||
|
||||
if ($useStaticLoader) {
|
||||
$includeFiles = Composer\Autoload\ComposerStaticInitDontChange::$files;
|
||||
} else {
|
||||
$includeFiles = require __DIR__ . '/autoload_files.php';
|
||||
}
|
||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||
composerRequireDontChange($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
}
|
||||
}
|
||||
|
||||
function composerRequireDontChange($fileIdentifier, $file)
|
||||
{
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
require $file;
|
||||
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,8 +6,6 @@ namespace Composer\Autoload;
|
|||
|
||||
class ComposerStaticInitDontChange
|
||||
{
|
||||
public static $files = array ();
|
||||
|
||||
public static $prefixLengthsPsr4 = array (
|
||||
'P' =>
|
||||
array (
|
||||
|
@ -22,9 +20,42 @@ class ComposerStaticInitDontChange
|
|||
),
|
||||
);
|
||||
|
||||
public static $prefixesPsr0 = array ();
|
||||
public static $prefixesPsr0 = array (
|
||||
'I' =>
|
||||
array (
|
||||
'Identicon' =>
|
||||
array (
|
||||
0 => __DIR__ . '/..' . '/yzalis/identicon/src',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
public static $classMap = array ();
|
||||
public static $classMap = array (
|
||||
'Identicon\\Generator\\BaseGenerator' => __DIR__ . '/..' . '/yzalis/identicon/src/Identicon/Generator/BaseGenerator.php',
|
||||
'Identicon\\Generator\\GdGenerator' => __DIR__ . '/..' . '/yzalis/identicon/src/Identicon/Generator/GdGenerator.php',
|
||||
'Identicon\\Generator\\GeneratorInterface' => __DIR__ . '/..' . '/yzalis/identicon/src/Identicon/Generator/GeneratorInterface.php',
|
||||
'Identicon\\Generator\\ImageMagickGenerator' => __DIR__ . '/..' . '/yzalis/identicon/src/Identicon/Generator/ImageMagickGenerator.php',
|
||||
'Identicon\\Identicon' => __DIR__ . '/..' . '/yzalis/identicon/src/Identicon/Identicon.php',
|
||||
'PrivateBin\\Configuration' => __DIR__ . '/../..' . '/lib/Configuration.php',
|
||||
'PrivateBin\\Data\\AbstractData' => __DIR__ . '/../..' . '/lib/Data/AbstractData.php',
|
||||
'PrivateBin\\Data\\Database' => __DIR__ . '/../..' . '/lib/Data/Database.php',
|
||||
'PrivateBin\\Data\\Filesystem' => __DIR__ . '/../..' . '/lib/Data/Filesystem.php',
|
||||
'PrivateBin\\Filter' => __DIR__ . '/../..' . '/lib/Filter.php',
|
||||
'PrivateBin\\I18n' => __DIR__ . '/../..' . '/lib/I18n.php',
|
||||
'PrivateBin\\Model' => __DIR__ . '/../..' . '/lib/Model.php',
|
||||
'PrivateBin\\Model\\AbstractModel' => __DIR__ . '/../..' . '/lib/Model/AbstractModel.php',
|
||||
'PrivateBin\\Model\\Comment' => __DIR__ . '/../..' . '/lib/Model/Comment.php',
|
||||
'PrivateBin\\Model\\Paste' => __DIR__ . '/../..' . '/lib/Model/Paste.php',
|
||||
'PrivateBin\\Persistence\\AbstractPersistence' => __DIR__ . '/../..' . '/lib/Persistence/AbstractPersistence.php',
|
||||
'PrivateBin\\Persistence\\PurgeLimiter' => __DIR__ . '/../..' . '/lib/Persistence/PurgeLimiter.php',
|
||||
'PrivateBin\\Persistence\\ServerSalt' => __DIR__ . '/../..' . '/lib/Persistence/ServerSalt.php',
|
||||
'PrivateBin\\Persistence\\TrafficLimiter' => __DIR__ . '/../..' . '/lib/Persistence/TrafficLimiter.php',
|
||||
'PrivateBin\\PrivateBin' => __DIR__ . '/../..' . '/lib/PrivateBin.php',
|
||||
'PrivateBin\\Request' => __DIR__ . '/../..' . '/lib/Request.php',
|
||||
'PrivateBin\\Sjcl' => __DIR__ . '/../..' . '/lib/Sjcl.php',
|
||||
'PrivateBin\\View' => __DIR__ . '/../..' . '/lib/View.php',
|
||||
'PrivateBin\\Vizhash16x16' => __DIR__ . '/../..' . '/lib/Vizhash16x16.php',
|
||||
);
|
||||
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
|
|
|
@ -1 +1,54 @@
|
|||
[]
|
||||
[
|
||||
{
|
||||
"name": "yzalis/identicon",
|
||||
"version": "1.1.0",
|
||||
"version_normalized": "1.1.0.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/yzalis/Identicon.git",
|
||||
"reference": "a99fc2a3d018512f7914bc6f972952536c0f309b"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/yzalis/Identicon/zipball/a99fc2a3d018512f7914bc6f972952536c0f309b",
|
||||
"reference": "a99fc2a3d018512f7914bc6f972952536c0f309b",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.3.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"fzaninotto/faker": "1.2.*@dev"
|
||||
},
|
||||
"time": "2014-07-13 09:19:12",
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.1-dev"
|
||||
}
|
||||
},
|
||||
"installation-source": "dist",
|
||||
"autoload": {
|
||||
"psr-0": {
|
||||
"Identicon": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Benjamin Laugueux",
|
||||
"email": "benjamin@yzalis.com"
|
||||
}
|
||||
],
|
||||
"description": "Create awesome unique avatar.",
|
||||
"homepage": "http://identicon-php.org",
|
||||
"keywords": [
|
||||
"avatar",
|
||||
"identicon",
|
||||
"image"
|
||||
]
|
||||
}
|
||||
]
|
||||
|
|
|
@ -0,0 +1,241 @@
|
|||
<?php
|
||||
|
||||
namespace Identicon\Generator;
|
||||
|
||||
use Identicon\Generator\GeneratorInterface;
|
||||
|
||||
/**
|
||||
* @author Benjamin Laugueux <benjamin@yzalis.com>
|
||||
*/
|
||||
class BaseGenerator
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $generatedImage;
|
||||
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $color;
|
||||
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $backgroundColor;
|
||||
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $size;
|
||||
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $pixelRatio;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $hash;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $arrayOfSquare = array();
|
||||
|
||||
/**
|
||||
* Set the image color
|
||||
*
|
||||
* @param string|array $color The color in hexa (6 chars) or rgb array
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
public function setColor($color)
|
||||
{
|
||||
if (null === $color) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->color = $this->convertColor($color);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the image background color
|
||||
*
|
||||
* @param string|array $backgroundColor The color in hexa (6 chars) or rgb array
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
public function setBackgroundColor($backgroundColor)
|
||||
{
|
||||
if (null === $backgroundColor) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->backgroundColor = $this->convertColor($backgroundColor);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function convertColor($color)
|
||||
{
|
||||
$convertedColor = array();
|
||||
if (is_array($color)) {
|
||||
$convertedColor[0] = $color[0];
|
||||
$convertedColor[1] = $color[1];
|
||||
$convertedColor[2] = $color[2];
|
||||
} else {
|
||||
if (false !== strpos($color, '#')) {
|
||||
$color = substr($color, 1);
|
||||
}
|
||||
$convertedColor[0] = hexdec(substr($color, 0, 2));
|
||||
$convertedColor[1] = hexdec(substr($color, 2, 2));
|
||||
$convertedColor[2] = hexdec(substr($color, 4, 2));
|
||||
}
|
||||
|
||||
return $convertedColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the color
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getColor()
|
||||
{
|
||||
return $this->color;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the background color
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getBackgroundColor()
|
||||
{
|
||||
return $this->backgroundColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the hash into an multidimensionnal array of boolean
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
private function convertHashToArrayOfBoolean()
|
||||
{
|
||||
preg_match_all('/(\w)(\w)/', $this->hash, $chars);
|
||||
foreach ($chars[1] as $i => $char) {
|
||||
if ($i % 3 == 0) {
|
||||
$this->arrayOfSquare[$i/3][0] = $this->convertHexaToBoolean($char);
|
||||
$this->arrayOfSquare[$i/3][4] = $this->convertHexaToBoolean($char);
|
||||
} elseif ($i % 3 == 1) {
|
||||
$this->arrayOfSquare[$i/3][1] = $this->convertHexaToBoolean($char);
|
||||
$this->arrayOfSquare[$i/3][3] = $this->convertHexaToBoolean($char);
|
||||
} else {
|
||||
$this->arrayOfSquare[$i/3][2] = $this->convertHexaToBoolean($char);
|
||||
}
|
||||
ksort($this->arrayOfSquare[$i/3]);
|
||||
}
|
||||
|
||||
$this->color[0] = hexdec(array_pop($chars[1]))*16;
|
||||
$this->color[1] = hexdec(array_pop($chars[1]))*16;
|
||||
$this->color[2] = hexdec(array_pop($chars[1]))*16;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an heaxecimal number into a boolean
|
||||
*
|
||||
* @param string $hexa
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
private function convertHexaToBoolean($hexa)
|
||||
{
|
||||
return (bool) intval(round(hexdec($hexa)/10));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getArrayOfSquare()
|
||||
{
|
||||
return $this->arrayOfSquare;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the identicon string hash
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getHash()
|
||||
{
|
||||
return $this->hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a hash fron the original string
|
||||
*
|
||||
* @param string $string
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
public function setString($string)
|
||||
{
|
||||
if (null === $string) {
|
||||
throw new \Exception('The string cannot be null.');
|
||||
}
|
||||
|
||||
$this->hash = md5($string);
|
||||
|
||||
$this->convertHashToArrayOfBoolean();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the image size
|
||||
*
|
||||
* @param integer $size
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
public function setSize($size)
|
||||
{
|
||||
if (null === $size) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->size = $size;
|
||||
$this->pixelRatio = round($size / 5);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the image size
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getSize()
|
||||
{
|
||||
return $this->size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the pixel ratio
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getPixelRatio()
|
||||
{
|
||||
return $this->pixelRatio;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
|
||||
namespace Identicon\Generator;
|
||||
|
||||
use Identicon\Generator\GeneratorInterface;
|
||||
|
||||
/**
|
||||
* @author Benjamin Laugueux <benjamin@yzalis.com>
|
||||
*/
|
||||
class GdGenerator extends BaseGenerator implements GeneratorInterface
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
if (!extension_loaded('gd')) {
|
||||
throw new \Exception('GD does not appear to be avaliable in your PHP installation. Please try another generator');
|
||||
}
|
||||
}
|
||||
|
||||
private function generateImage()
|
||||
{
|
||||
// prepare image
|
||||
$this->generatedImage = imagecreatetruecolor($this->getPixelRatio() * 5, $this->getPixelRatio() * 5);
|
||||
|
||||
$rgbBackgroundColor = $this->getBackgroundColor();
|
||||
if (null === $rgbBackgroundColor) {
|
||||
$background = imagecolorallocate($this->generatedImage, 0, 0, 0);
|
||||
imagecolortransparent($this->generatedImage, $background);
|
||||
} else {
|
||||
$background = imagecolorallocate($this->generatedImage, $rgbBackgroundColor[0], $rgbBackgroundColor[1], $rgbBackgroundColor[2]);
|
||||
imagefill($this->generatedImage, 0, 0, $background);
|
||||
}
|
||||
|
||||
// prepage color
|
||||
$rgbColor = $this->getColor();
|
||||
$gdColor = imagecolorallocate($this->generatedImage, $rgbColor[0], $rgbColor[1], $rgbColor[2]);
|
||||
|
||||
// draw content
|
||||
foreach ($this->getArrayOfSquare() as $lineKey => $lineValue) {
|
||||
foreach ($lineValue as $colKey => $colValue) {
|
||||
if (true === $colValue) {
|
||||
imagefilledrectangle($this->generatedImage, $colKey * $this->getPixelRatio(), $lineKey * $this->getPixelRatio(), ($colKey + 1) * $this->getPixelRatio(), ($lineKey + 1) * $this->getPixelRatio(), $gdColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getImageBinaryData($string, $size = null, $color = null, $backgroundColor = null)
|
||||
{
|
||||
ob_start();
|
||||
imagepng($this->getImageResource($string, $size, $color, $backgroundColor));
|
||||
$imageData = ob_get_contents();
|
||||
ob_end_clean();
|
||||
|
||||
return $imageData;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getImageResource($string, $size = null, $color = null, $backgroundColor = null)
|
||||
{
|
||||
$this
|
||||
->setString($string)
|
||||
->setSize($size)
|
||||
->setColor($color)
|
||||
->setBackgroundColor($backgroundColor)
|
||||
->generateImage();
|
||||
|
||||
return $this->generatedImage;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace Identicon\Generator;
|
||||
|
||||
/**
|
||||
* @author Benjamin Laugueux <benjamin@yzalis.com>
|
||||
*/
|
||||
interface GeneratorInterface
|
||||
{
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param string $string
|
||||
* @param integer $size
|
||||
* @param array|string $color
|
||||
* @param array|string $backgroundColor
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
function getImageBinaryData($string, $size = null, $color = null, $backgroundColor = null);
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param string $string
|
||||
* @param integer $size
|
||||
* @param array|string $color
|
||||
* @param array|string $backgroundColor
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function getImageResource($string, $size = null, $color = null, $backgroundColor = null);
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
|
||||
namespace Identicon\Generator;
|
||||
|
||||
use Identicon\Generator\GeneratorInterface;
|
||||
|
||||
/**
|
||||
* @author Francis Chuang <francis.chuang@gmail.com>
|
||||
*/
|
||||
class ImageMagickGenerator extends BaseGenerator implements GeneratorInterface
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
if (!extension_loaded('imagick')) {
|
||||
throw new \Exception('ImageMagick does not appear to be avaliable in your PHP installation. Please try another generator');
|
||||
}
|
||||
}
|
||||
|
||||
private function generateImage()
|
||||
{
|
||||
$this->generatedImage = new \Imagick();
|
||||
$rgbBackgroundColor = $this->getBackgroundColor();
|
||||
|
||||
if (null === $rgbBackgroundColor) {
|
||||
$background = 'none';
|
||||
} else {
|
||||
$background = new \ImagickPixel("rgb($rgbBackgroundColor[0],$rgbBackgroundColor[1],$rgbBackgroundColor[2])");
|
||||
}
|
||||
|
||||
$this->generatedImage->newImage($this->pixelRatio * 5, $this->pixelRatio * 5, $background, 'png');
|
||||
|
||||
// prepare color
|
||||
$rgbColor = $this->getColor();
|
||||
$color = new \ImagickPixel("rgb($rgbColor[0],$rgbColor[1],$rgbColor[2])");
|
||||
|
||||
$draw = new \ImagickDraw();
|
||||
$draw->setFillColor($color);
|
||||
|
||||
// draw the content
|
||||
foreach ($this->getArrayOfSquare() as $lineKey => $lineValue) {
|
||||
foreach ($lineValue as $colKey => $colValue) {
|
||||
if (true === $colValue) {
|
||||
$draw->rectangle( $colKey * $this->pixelRatio, $lineKey * $this->pixelRatio, ($colKey + 1) * $this->pixelRatio, ($lineKey + 1) * $this->pixelRatio);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->generatedImage->drawImage($draw);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getImageBinaryData($string, $size = null, $color = null, $backgroundColor = null)
|
||||
{
|
||||
ob_start();
|
||||
echo $this->getImageResource($string, $size, $color, $backgroundColor);
|
||||
$imageData = ob_get_contents();
|
||||
ob_end_clean();
|
||||
|
||||
return $imageData;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getImageResource($string, $size = null, $color = null, $backgroundColor = null)
|
||||
{
|
||||
$this
|
||||
->setString($string)
|
||||
->setSize($size)
|
||||
->setColor($color)
|
||||
->setBackgroundColor($backgroundColor)
|
||||
->generateImage();
|
||||
|
||||
return $this->generatedImage;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
<?php
|
||||
|
||||
namespace Identicon;
|
||||
|
||||
use Identicon\Generator\GdGenerator;
|
||||
use Identicon\Generator\GeneratorInterface;
|
||||
|
||||
/**
|
||||
* @author Benjamin Laugueux <benjamin@yzalis.com>
|
||||
*/
|
||||
class Identicon
|
||||
{
|
||||
/**
|
||||
* @var GeneratorInterface
|
||||
*/
|
||||
private $generator;
|
||||
|
||||
public function __construct($generator = null)
|
||||
{
|
||||
if (null === $generator) {
|
||||
$this->generator = new GdGenerator();
|
||||
} else {
|
||||
$this->generator = $generator;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the image generetor
|
||||
*
|
||||
* @param GeneratorInterface $generator
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function setGenerator(GeneratorInterface $generator)
|
||||
{
|
||||
$this->generator = $generator;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display an Identicon image
|
||||
*
|
||||
* @param string $string
|
||||
* @param integer $size
|
||||
* @param string $color
|
||||
* @param string $backgroundColor
|
||||
*/
|
||||
public function displayImage($string, $size = 64, $color = null, $backgroundColor = null)
|
||||
{
|
||||
header("Content-Type: image/png");
|
||||
echo $this->getImageData($string, $size, $color, $backgroundColor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an Identicon PNG image data
|
||||
*
|
||||
* @param string $string
|
||||
* @param integer $size
|
||||
* @param string $color
|
||||
* @param string $backgroundColor
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getImageData($string, $size = 64, $color = null, $backgroundColor = null)
|
||||
{
|
||||
return $this->generator->getImageBinaryData($string, $size, $color, $backgroundColor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an Identicon PNG image resource
|
||||
*
|
||||
* @param string $string
|
||||
* @param integer $size
|
||||
* @param string $color
|
||||
* @param string $backgroundColor
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getImageResource($string, $size = 64, $color = null, $backgroundColor = null)
|
||||
{
|
||||
return $this->generator->getImageResource($string, $size, $color, $backgroundColor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an Identicon PNG image data as base 64 encoded
|
||||
*
|
||||
* @param string $string
|
||||
* @param integer $size
|
||||
* @param string $color
|
||||
* @param string $backgroundColor
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getImageDataUri($string, $size = 64, $color = null, $backgroundColor = null)
|
||||
{
|
||||
return sprintf('data:image/png;base64,%s', base64_encode($this->getImageData($string, $size, $color, $backgroundColor)));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue