upgraded PHP libraries

This commit is contained in:
El RIDO
2019-06-16 07:10:24 +02:00
parent 362045c664
commit db4ae09ee3
24 changed files with 469 additions and 331 deletions

View File

@@ -2,7 +2,7 @@
namespace Identicon\Generator;
use Identicon\Generator\GeneratorInterface;
use Exception;
/**
* @author Benjamin Laugueux <benjamin@yzalis.com>
@@ -15,22 +15,22 @@ class BaseGenerator
protected $generatedImage;
/**
* @var integer
* @var array
*/
protected $color;
/**
* @var integer
* @var array
*/
protected $backgroundColor;
/**
* @var integer
* @var int
*/
protected $size;
/**
* @var integer
* @var int
*/
protected $pixelRatio;
@@ -42,14 +42,14 @@ class BaseGenerator
/**
* @var array
*/
private $arrayOfSquare = array();
private $arrayOfSquare = [];
/**
* Set the image color
* Set the image color.
*
* @param string|array $color The color in hexa (6 chars) or rgb array
* @param string|array $color The color in hexa (3 or 6 chars) or rgb array
*
* @return this
* @return $this
*/
public function setColor($color)
{
@@ -63,11 +63,11 @@ class BaseGenerator
}
/**
* Set the image background color
* Set the image background color.
*
* @param string|array $backgroundColor The color in hexa (6 chars) or rgb array
* @param string|array $backgroundColor The color in hexa (3 or 6 chars) or rgb array
*
* @return this
* @return $this
*/
public function setBackgroundColor($backgroundColor)
{
@@ -80,27 +80,32 @@ class BaseGenerator
return $this;
}
/**
* @param array|string $color
*
* @return array
*/
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 $color;
}
return $convertedColor;
if (preg_match('/^#?([a-z\d])([a-z\d])([a-z\d])$/i', $color, $matches)) {
$color = $matches[1].$matches[1];
$color .= $matches[2].$matches[2];
$color .= $matches[3].$matches[3];
}
preg_match('/#?([a-z\d]{2})([a-z\d]{2})([a-z\d]{2})$/i', $color, $matches);
return array_map(function ($value) {
return hexdec($value);
}, array_slice($matches, 1, 3));
}
/**
* Get the color
* Get the color.
*
* @return array
*/
@@ -109,9 +114,8 @@ class BaseGenerator
return $this->color;
}
/**
* Get the background color
* Get the background color.
*
* @return array
*/
@@ -121,48 +125,51 @@ class BaseGenerator
}
/**
* Convert the hash into an multidimensionnal array of boolean
* Convert the hash into an multidimensional array of boolean.
*
* @return this
* @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);
$index = (int) ($i / 3);
$data = $this->convertHexaToBoolean($char);
$items = [
0 => [0, 4],
1 => [1, 3],
2 => [2],
];
foreach ($items[$i % 3] as $item) {
$this->arrayOfSquare[$index][$item] = $data;
}
ksort($this->arrayOfSquare[$i/3]);
ksort($this->arrayOfSquare[$index]);
}
$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;
$this->color = array_map(function ($data) {
return hexdec($data) * 16;
}, array_reverse($chars[1]));
return $this;
}
/**
* Convert an heaxecimal number into a boolean
* Convert an hexadecimal number into a boolean.
*
* @param string $hexa
*
* @return boolean
* @return bool
*/
private function convertHexaToBoolean($hexa)
{
return (bool) intval(round(hexdec($hexa)/10));
return (bool) round(hexdec($hexa) / 10);
}
/**
*
*
* @return array
*/
public function getArrayOfSquare()
@@ -171,7 +178,7 @@ class BaseGenerator
}
/**
* Get the identicon string hash
* Get the identicon string hash.
*
* @return string
*/
@@ -181,16 +188,18 @@ class BaseGenerator
}
/**
* Generate a hash fron the original string
* Generate a hash from the original string.
*
* @param string $string
*
* @return this
* @throws \Exception
*
* @return $this
*/
public function setString($string)
{
if (null === $string) {
throw new \Exception('The string cannot be null.');
throw new Exception('The string cannot be null.');
}
$this->hash = md5($string);
@@ -201,11 +210,11 @@ class BaseGenerator
}
/**
* Set the image size
* Set the image size.
*
* @param integer $size
* @param int $size
*
* @return this
* @return $this
*/
public function setSize($size)
{
@@ -214,15 +223,15 @@ class BaseGenerator
}
$this->size = $size;
$this->pixelRatio = round($size / 5);
$this->pixelRatio = (int) round($size / 5);
return $this;
}
/**
* Get the image size
* Get the image size.
*
* @return integer
* @return int
*/
public function getSize()
{
@@ -230,9 +239,9 @@ class BaseGenerator
}
/**
* Get the pixel ratio
* Get the pixel ratio.
*
* @return array
* @return int
*/
public function getPixelRatio()
{

View File

@@ -2,20 +2,34 @@
namespace Identicon\Generator;
use Identicon\Generator\GeneratorInterface;
use Exception;
/**
* @author Benjamin Laugueux <benjamin@yzalis.com>
*/
class GdGenerator extends BaseGenerator implements GeneratorInterface
{
/**
* GdGenerator constructor.
*/
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');
if (!extension_loaded('gd') && !extension_loaded('ext-gd')) {
throw new Exception('GD does not appear to be available in your PHP installation. Please try another generator');
}
}
/**
* @return string
*/
public function getMimeType()
{
return 'image/png';
}
/**
* @return $this
*/
private function generateImage()
{
// prepare image
@@ -30,7 +44,7 @@ class GdGenerator extends BaseGenerator implements GeneratorInterface
imagefill($this->generatedImage, 0, 0, $background);
}
// prepage color
// prepare color
$rgbColor = $this->getColor();
$gdColor = imagecolorallocate($this->generatedImage, $rgbColor[0], $rgbColor[1], $rgbColor[2]);
@@ -47,7 +61,7 @@ class GdGenerator extends BaseGenerator implements GeneratorInterface
}
/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function getImageBinaryData($string, $size = null, $color = null, $backgroundColor = null)
{
@@ -60,7 +74,7 @@ class GdGenerator extends BaseGenerator implements GeneratorInterface
}
/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function getImageResource($string, $size = null, $color = null, $backgroundColor = null)
{

View File

@@ -8,26 +8,36 @@ namespace Identicon\Generator;
interface GeneratorInterface
{
/**
*
*
* @param string $string
* @param integer $size
* @param array|string $color
* @param array|string $backgroundColor
* @param string $string
* @param int $size
* @param array|string $color
* @param array|string $backgroundColor
*
* @return mixed
*/
function getImageBinaryData($string, $size = null, $color = null, $backgroundColor = null);
public function getImageBinaryData($string, $size = null, $color = null, $backgroundColor = null);
/**
*
*
* @param string $string
* @param integer $size
* @param array|string $color
* @param array|string $backgroundColor
* @param string $string
* @param int $size
* @param array|string $color
* @param array|string $backgroundColor
*
* @return string
*/
function getImageResource($string, $size = null, $color = null, $backgroundColor = null);
public function getImageResource($string, $size = null, $color = null, $backgroundColor = null);
/**
* Return the mime-type of this identicon.
*
* @return string
*/
public function getMimeType();
/**
* Return the color of the created identicon.
*
* @return array
*/
public function getColor();
}

View File

@@ -2,20 +2,38 @@
namespace Identicon\Generator;
use Identicon\Generator\GeneratorInterface;
use Exception;
use ImagickDraw;
use ImagickPixel;
/**
* @author Francis Chuang <francis.chuang@gmail.com>
*/
class ImageMagickGenerator extends BaseGenerator implements GeneratorInterface
{
/**
* ImageMagickGenerator constructor.
*
* @throws \Exception
*/
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');
throw new Exception('ImageMagick does not appear to be avaliable in your PHP installation. Please try another generator');
}
}
/**
* @return string
*/
public function getMimeType()
{
return 'image/png';
}
/**
* @return $this
*/
private function generateImage()
{
$this->generatedImage = new \Imagick();
@@ -24,23 +42,23 @@ class ImageMagickGenerator extends BaseGenerator implements GeneratorInterface
if (null === $rgbBackgroundColor) {
$background = 'none';
} else {
$background = new \ImagickPixel("rgb($rgbBackgroundColor[0],$rgbBackgroundColor[1],$rgbBackgroundColor[2])");
$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])");
$color = new ImagickPixel("rgb($rgbColor[0],$rgbColor[1],$rgbColor[2])");
$draw = new \ImagickDraw();
$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);
$draw->rectangle($colKey * $this->pixelRatio, $lineKey * $this->pixelRatio, ($colKey + 1) * $this->pixelRatio, ($lineKey + 1) * $this->pixelRatio);
}
}
}
@@ -51,7 +69,7 @@ class ImageMagickGenerator extends BaseGenerator implements GeneratorInterface
}
/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function getImageBinaryData($string, $size = null, $color = null, $backgroundColor = null)
{
@@ -64,7 +82,7 @@ class ImageMagickGenerator extends BaseGenerator implements GeneratorInterface
}
/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function getImageResource($string, $size = null, $color = null, $backgroundColor = null)
{

View File

@@ -0,0 +1,88 @@
<?php
namespace Identicon\Generator;
/**
* @author Grummfy <grummfy@gmail.com>
*/
class SvgGenerator extends BaseGenerator implements GeneratorInterface
{
/**
* {@inheritdoc}
*/
public function getMimeType()
{
return 'image/svg+xml';
}
/**
* {@inheritdoc}
*/
public function getImageBinaryData($string, $size = null, $color = null, $backgroundColor = null)
{
return $this->getImageResource($string, $size, $color, $backgroundColor);
}
/**
* {@inheritdoc}
*/
public function getImageResource($string, $size = null, $color = null, $backgroundColor = null)
{
$this
->setString($string)
->setSize($size)
->setColor($color)
->setBackgroundColor($backgroundColor)
->_generateImage();
return $this->generatedImage;
}
/**
* @return $this
*/
protected function _generateImage()
{
// prepare image
$w = $this->getPixelRatio() * 5;
$h = $this->getPixelRatio() * 5;
$svg = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="'.$w.'" height="'.$h.'">';
$backgroundColor = '#FFFFFF';
$rgbBackgroundColor = $this->getBackgroundColor();
if (!is_null($rgbBackgroundColor)) {
$backgroundColor = $this->_toUnderstandableColor($rgbBackgroundColor);
}
$svg .= '<rect width="'.$w.'" height="'.$h.'" style="fill:'.$backgroundColor.';stroke-width:1;stroke:'.$backgroundColor.'"/>';
$rgbColor = $this->_toUnderstandableColor($this->getColor());
// draw content
foreach ($this->getArrayOfSquare() as $lineKey => $lineValue) {
foreach ($lineValue as $colKey => $colValue) {
if (true === $colValue) {
$svg .= '<rect x="'.$colKey * $this->getPixelRatio().'" y="'.$lineKey * $this->getPixelRatio().'" width="'.($this->getPixelRatio()).'" height="'.$this->getPixelRatio().'" style="fill:'.$rgbColor.';stroke-width:0;"/>';
}
}
}
$svg .= '</svg>';
$this->generatedImage = $svg;
return $this;
}
/**
* @param array|string $color
*
* @return string
*/
protected function _toUnderstandableColor($color)
{
if (is_array($color)) {
return 'rgb('.implode(', ', $color).')';
}
return $color;
}
}

View File

@@ -11,10 +11,15 @@ use Identicon\Generator\GeneratorInterface;
class Identicon
{
/**
* @var GeneratorInterface
* @var \Identicon\Generator\GeneratorInterface
*/
private $generator;
/**
* Identicon constructor.
*
* @param \Identicon\Generator\GeneratorInterface|null $generator
*/
public function __construct($generator = null)
{
if (null === $generator) {
@@ -22,15 +27,14 @@ class Identicon
} else {
$this->generator = $generator;
}
}
/**
* Set the image generetor
* Set the image generator.
*
* @param GeneratorInterface $generator
* @param \Identicon\Generator\GeneratorInterface $generator
*
* @throws \Exception
* @return $this
*/
public function setGenerator(GeneratorInterface $generator)
{
@@ -40,26 +44,26 @@ class Identicon
}
/**
* Display an Identicon image
* Display an Identicon image.
*
* @param string $string
* @param integer $size
* @param string $color
* @param string $backgroundColor
* @param string $string
* @param int $size
* @param string $color
* @param string $backgroundColor
*/
public function displayImage($string, $size = 64, $color = null, $backgroundColor = null)
{
header("Content-Type: image/png");
header('Content-Type: '.$this->generator->getMimeType());
echo $this->getImageData($string, $size, $color, $backgroundColor);
}
/**
* Get an Identicon PNG image data
* Get an Identicon PNG image data.
*
* @param string $string
* @param integer $size
* @param string $color
* @param string $backgroundColor
* @param string $string
* @param int $size
* @param string $color
* @param string $backgroundColor
*
* @return string
*/
@@ -69,12 +73,12 @@ class Identicon
}
/**
* Get an Identicon PNG image resource
* Get an Identicon PNG image resource.
*
* @param string $string
* @param integer $size
* @param string $color
* @param string $backgroundColor
* @param string $string
* @param int $size
* @param string $color
* @param string $backgroundColor
*
* @return string
*/
@@ -84,17 +88,36 @@ class Identicon
}
/**
* Get an Identicon PNG image data as base 64 encoded
* Get an Identicon PNG image data as base 64 encoded.
*
* @param string $string
* @param integer $size
* @param string $color
* @param string $backgroundColor
* @param string $string
* @param int $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)));
return sprintf('data:%s;base64,%s', $this->generator->getMimeType(), base64_encode($this->getImageData($string, $size, $color, $backgroundColor)));
}
/**
* Get the color of the Identicon
*
* Returns an array with RGB values of the Identicon's color. Colors may be NULL if no image has been generated
* so far (e.g., when calling the method on a new Identicon()).
*
* @return array
*/
public function getColor()
{
$colors = $this->generator->getColor();
return [
"r" => $colors[0],
"g" => $colors[1],
"b" => $colors[2]
];
}
}

View File

@@ -1,30 +0,0 @@
<?php
/**
* Simple autoloader that follow the PHP Standards Recommendation #0 (PSR-0)
* @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md for more informations.
*
* Code inspired from the SplClassLoader RFC
* @see https://wiki.php.net/rfc/splclassloader#example_implementation
*/
spl_autoload_register(function($className) {
$className = ltrim($className, '\\');
if (0 != strpos($className, 'Identicon')) {
return false;
}
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName = __DIR__ . DIRECTORY_SEPARATOR . $fileName . $className . '.php';
if (is_file($fileName)) {
require $fileName;
return true;
}
return false;
});