Commit 658dc7bd authored by Dominique Feyer's avatar Dominique Feyer

Merge branch 'devel'

parents 301f5dd2 0e7f089b
......@@ -12,7 +12,9 @@ namespace Ttree\Identicons\Generator;
* */
use Imagine\Filter\Basic\Rotate;
use Imagine\Image\Box;
use Imagine\Image\Color;
use Imagine\Image\ImageInterface;
use Imagine\Image\Point;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Utility\Arrays;
......@@ -111,6 +113,18 @@ abstract class AbstractGenerator implements GeneratorInterface {
return $coordinates;
}
/**
* @param ImageInterface $image
* @param int $padding
* @return \Imagine\Image\ManipulatorInterface
*/
protected function pad(ImageInterface $image, $padding) {
$size = $image->getSize();
$resized = $this->createImage($size->getWidth() + $padding, $size->getHeight() + $padding);
return $resized->paste($image, new Point($padding / 2, $padding / 2))->resize(new Box($this->getSize(), $this->getSize()));
}
}
?>
\ No newline at end of file
......@@ -39,50 +39,42 @@ class DonParkGenerator extends AbstractGenerator {
$sideSpriteRotation = hexdec(substr($hash, 4, 1)) & 3;
$centerSpriteBackground = hexdec(substr($hash, 5, 1)) % 2;
$cornerSpriteForegroundColorRed = hexdec(substr($hash, 6, 2));
$cornerSpriteForegroundColorGreen = hexdec(substr($hash, 8, 2));
$cornerSpriteForegroundColorBlue = hexdec(substr($hash, 10, 2));
$cornerSpriteForegroundColor = new Color(array(hexdec(substr($hash, 6, 2)), hexdec(substr($hash, 8, 2)), hexdec(substr($hash, 10, 2))));
$sideSpriteForegroundColorRed = hexdec(substr($hash, 12, 2));
$sideSpriteForegroundColorGreen = hexdec(substr($hash, 14, 2));
$sideSpriteForegroundColorBlue = hexdec(substr($hash, 16, 2));
$sideSpriteForegroundColor = new Color(array(hexdec(substr($hash, 12, 2)), hexdec(substr($hash, 14, 2)), hexdec(substr($hash, 16, 2))));
$backgroundColor = new Color(array(255, 255, 255));
$identicon = $this->createImage($size * 3, $size * 3, $backgroundColor);
$corner = $this->getSprite($cornerSpriteShape, $cornerSpriteForegroundColorRed, $cornerSpriteForegroundColorGreen, $cornerSpriteForegroundColorBlue, $cornerSpriteRotation);
$corner = $this->getSprite($cornerSpriteShape, $cornerSpriteForegroundColor, $cornerSpriteRotation);
$identicon->paste($corner, new Point(0, 0));
$identicon->paste($this->rotate($corner, 90), new Point(0, $size * 2));
$identicon->paste($this->rotate($corner, 90), new Point($size * 2, $size * 2));
$identicon->paste($this->rotate($corner, 90), new Point($size * 2, 0));
$side = $this->getSprite($sideSpriteShape, $sideSpriteForegroundColorRed, $sideSpriteForegroundColorGreen, $sideSpriteForegroundColorBlue, $sideSpriteRotation);
$side = $this->getSprite($sideSpriteShape, $sideSpriteForegroundColor, $sideSpriteRotation);
$identicon->paste($side, new Point($size, 0));
$identicon->paste($this->rotate($side, 90), new Point(0, $size));
$identicon->paste($this->rotate($side, 90), new Point($size, $size * 2));
$identicon->paste($this->rotate($side, 90), new Point($size * 2, $size));
$center = $this->getCenter($centerSpriteShape, $cornerSpriteForegroundColorRed, $cornerSpriteForegroundColorGreen, $cornerSpriteForegroundColorBlue, $sideSpriteForegroundColorRed, $sideSpriteForegroundColorGreen, $sideSpriteForegroundColorBlue, $centerSpriteBackground);
$center = $this->getCenter($centerSpriteShape, $cornerSpriteForegroundColor);
$identicon->paste($center, new Point($size, $size));
/* create blank image according to specified dimensions */
$identicon = $identicon->resize(new Box($size, $size));
return $identicon;
return $this->pad($identicon, $this->getSize() / 4);
}
/**
* @param int $shape
* @param int $red
* @param int $green
* @param int $blue
* @param float $rotation
* @param $shape
* @param Color $foregroundColor
* @param $rotation
* @return \Imagine\Image\ImageInterface
*/
protected function getSprite($shape, $red, $green, $blue, $rotation) {
$foregroundColor = new Color(array($red, $green, $blue));
$backgroundColor = new Color(array(255, 255, 255));
$sprite = $this->createImage($this->getSize(), $this->getSize(), $backgroundColor);
protected function getSprite($shape, Color $foregroundColor, $rotation) {
$sprite = $this->createImage($this->getSize(), $this->getSize(), $this->backgroundColor);
switch ($shape) {
case 0: // triangle
......@@ -263,24 +255,18 @@ class DonParkGenerator extends AbstractGenerator {
}
/**
* @param int $shape
* @param int $foregroundRed
* @param int $foregroundGreen
* @param int $foregroundBlue
* @param int $backgroundRed
* @param int $backgroundGreen
* @param int $backgroundBlue
* @param bool $useBackgroundColor
* @param $shape
* @param Color $foregroundColor
* @param Color $backgroundColor
* @return \Imagine\Image\ImageInterface
*/
protected function getCenter($shape, $foregroundRed, $foregroundGreen, $foregroundBlue, $backgroundRed, $backgroundGreen, $backgroundBlue, $useBackgroundColor = FALSE) {
protected function getCenter($shape, Color $foregroundColor, Color $backgroundColor = NULL) {
/* make sure there's enough contrast before we use background color of side sprite */
if ($useBackgroundColor === TRUE) {
$backgroundColor = $this->generateBackgroundColorBasedOnContrast($foregroundRed, $foregroundGreen, $foregroundBlue, $backgroundRed, $backgroundGreen, $backgroundBlue);
if ($backgroundColor !== NULL) {
$backgroundColor = $this->generateBackgroundColorBasedOnContrast($foregroundColor, $backgroundColor);
} else {
$backgroundColor = $this->backgroundColor;
}
$foregroundColor = new Color(array($foregroundRed, $foregroundGreen, $foregroundBlue));
$sprite = $this->createImage($this->getSize(), $this->getSize(), $backgroundColor);
switch ($shape) {
......@@ -389,18 +375,12 @@ class DonParkGenerator extends AbstractGenerator {
}
/**
* @param int $foregroundRed
* @param int $foregroundGreen
* @param int $foregroundBlue
* @param int $backgroundRed
* @param int $backgroundGreen
* @param int $backgroundBlue
* @param Color $foregroundColor
* @param Color $backgroundColor
* @return Color
*/
protected function generateBackgroundColorBasedOnContrast($foregroundRed, $foregroundGreen, $foregroundBlue, $backgroundRed, $backgroundGreen, $backgroundBlue) {
if (abs($foregroundRed - $backgroundRed) > 127 || abs($foregroundGreen - $backgroundGreen) > 127 || abs($foregroundBlue - $backgroundBlue) > 127) {
$backgroundColor = new Color(array($backgroundRed, $backgroundGreen, $backgroundBlue));
} else {
protected function generateBackgroundColorBasedOnContrast(Color $foregroundColor, Color $backgroundColor) {
if (!(abs($foregroundColor->getRed() - $backgroundColor->getRed()) > 127 || abs($foregroundColor->getGreen() - $backgroundColor->getGreen()) > 127 || abs($foregroundColor->getBlue() - $backgroundColor->getBlue()) > 127)) {
$backgroundColor = $this->backgroundColor;
}
......
......@@ -14,6 +14,7 @@ namespace Ttree\Identicons\Generator;
use Imagine\Filter\Basic\FlipHorizontally;
use Imagine\Image\Box;
use Imagine\Image\Color;
use Imagine\Image\ImageInterface;
use Imagine\Image\Point;
use TYPO3\Flow\Annotations as Flow;
......@@ -74,7 +75,7 @@ class GithubLikeGenerator extends AbstractGenerator {
$resized = $this->createImage($size->getWidth() + $padding, $size->getHeight() + $padding);
$resized->paste($identicon, new Point($padding / 2, $padding / 2))->resize(new Box($this->getSize(), $this->getSize()));
return $resized;
return $this->pad($identicon, $this->getSize() / 2);
}
/**
......
Ttree\Identicons\Generator\GeneratorInterface:
className: Ttree\Identicons\Generator\GithubLikeGenerator
# className: Ttree\Identicons\Generator\DonParkGenerator
scope: singleton
\ No newline at end of file
Ttree:
Identicons:
persist: FALSE
persist: TRUE
size: 420
backgroundColor: '#EEE'
\ No newline at end of file
***********************************
FLOW Package to generate Identicons
***********************************
This contains the TYPO3 Flow package "Ttree.Identicons" to generate Identicons. Currently only two generators are
available: Don Park (original version) and Github Style (inspired by the space invader style identicons provided by
github).
============
Installation
============
1. Just install and activate the package
2. Include the package subroutes in your main Routes.yaml
::
-
name: 'Identicons'
uriPattern: 'i/<IdenticonsSubroutes>'
subRoutes:
IdenticonsSubroutes:
package: Ttree.Identicons
3. Go to www.yourdomain/i/[yourhash].png (replace [yourhash] by any string)
====
Tips
====
You can write your own generator, your implement the GeneratorInterface and change the default implementation in
your Objects.yaml (check the Objects.yaml from this package for the synthax)
==================
Identicons Samples
==================
.. image:: Documentation/Sample/dfeyer-donpark.png
:alt: Don Park
.. image:: Documentation/Sample/dfeyer-githubstyle.png
:alt: Github Style
\ No newline at end of file
......@@ -2,10 +2,11 @@
"name": "ttree/identicons",
"type": "typo3-flow-package",
"description": "Flow package to generate identicons",
"version": "1.0",
"version": "1.0.0",
"license": "MIT",
"require": {
"typo3/flow": "*"
"typo3/flow": "*",
"typo3/media": "*"
},
"autoload": {
"psr-0": {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment