diff --git a/README.md b/README.md
index f898fbb..7818cff 100644
--- a/README.md
+++ b/README.md
@@ -54,7 +54,7 @@ or generate and get the base 64 image uri ready for integrate into an HTML img t
``` php
$imageDataUri = $identicon->getImageDataUri('bar');
```
-``` html
+``` php
```
@@ -75,6 +75,8 @@ Color can be an hexadecimal with 6 characters
``` php
$identicon->displayImage('bar', 64, 'A87EDF');
+
+$identicon->displayImage('bar', 64, '#A87EDF');
```
or an array with red, green, blue value
@@ -88,12 +90,30 @@ That's it!
### Generate an identicon on SVG format
The only thing you need to change it this one:
+
``` php
$identicon = new \Identicon\Identicon(new SvgGenerator());
$imageDataUri = $identicon->getImageDataUri('bar');
+```
+``` php
```
+### Margin
+
+There is no magin by default. If you need an image with margin, you can add a fifth parameter. One-tenth of the size is recommended.
+
+In this example, we set a gray background, and you can clearly see the margins:
+
+
+
+
+
+
+
+```php
+$identicon->displayImage('foo', 100, null, '#f0f0f0', 10);
+```
## Unit Tests
diff --git a/README.zh_cn.md b/README.zh_cn.md
new file mode 100644
index 0000000..7f4d8bd
--- /dev/null
+++ b/README.zh_cn.md
@@ -0,0 +1,144 @@
+# Identicon 生成器 PHP 版
+
+[](http://travis-ci.org/yzalis/Identicon)
+[](https://codecov.io/github/yzalis/Identicon?branch=master)
+
+**Identicon** 是一个基于字符串生成一个 [identicon](http://en.wikipedia.org/wiki/Identicon) 图像的库。
+
+以下是一些精彩的输出例子!
+
+
+
+
+
+
+
+## 安装
+
+推荐通过 composer 安装 Identicon。
+
+只需要在你的项目中引入本库:
+
+``` bash
+composer require yzalis/identicon
+```
+
+## 使用
+
+生成的图像都是透明背景的 PNG 格式。
+
+字符串可以是邮箱,IP 地址,用户名,ID 或者其他的东西。
+
+### 生成一个 identicon
+
+创建一个 ```Identicon``` 对象。
+
+``` php
+$identicon = new \Identicon\Identicon();
+```
+
+然后你可以生成或者显示一张图像
+
+``` php
+$identicon->displayImage('foo');
+```
+
+或者生成并获取图像的信息
+
+``` php
+$imageData = $identicon->getImageData('bar');
+```
+
+或者生成并获取 base 64 图片的 uri 以便整合到 HTML 的 img 标签中。
+
+``` php
+$imageDataUri = $identicon->getImageDataUri('bar');
+```
+``` php
+
+```
+
+
+### 修改图像大小
+
+默认的大小是 64 像素。如果你想改变图像大小,只需要添加第二个参数。在这个例子中是 512 x 512px。
+
+``` php
+$identicon->displayImage('foo', 512);
+```
+
+### 颜色
+
+图像颜色是由字符串的哈希值自动生成的,但是你可以添加第三个参数指定一个颜色。
+
+颜色值可以使用一个 6 字符的十六进制字符串
+
+``` php
+$identicon->displayImage('bar', 64, 'A87EDF');
+
+$identicon->displayImage('bar', 64, '#A87EDF');
+```
+
+也可以使用由红(R)、绿(G)、蓝(B)数值组成的数组
+
+``` php
+$identicon->displayImage('foo', 64, array(200, 100, 150));
+```
+
+就是这样!
+
+### 生成一个 SVG 格式的 identicon
+
+你只需要修改一个地方:
+``` php
+$identicon = new \Identicon\Identicon(new SvgGenerator());
+$imageDataUri = $identicon->getImageDataUri('bar');
+```
+``` php
+
+```
+
+### 边距
+
+默认是没有边距的,如果你想要带边距的图像,你可以添加第五个参数。推荐使用图像的大小的十分之一。
+
+在这个例子中,我们设置了灰色的背景,这样你可以很明显的看到边距:
+
+
+
+
+
+
+
+```php
+$identicon->displayImage('foo', 100, null, '#f0f0f0', 10);
+```
+
+## 单元测试
+
+为了运行单元测试,你需要一组依赖,它们可以通过 Composer 安装:
+
+```
+php composer.phar install
+```
+
+一旦安装,就可以使用下面的命令:
+
+```
+./vendor/bin/phpunit
+```
+
+应该一切都好了。
+
+
+## 贡献者名单
+
+* Benjamin Laugueux
+* [所有贡献者](https://github.com/yzalis/Identicon/graphs/contributors)
+
+灵感来自于一篇关于 Identicon 的 Github [博客](https://github.com/blog/1586-identicons)。
+
+
+## 证书
+
+Identicon 是根据 MIT 许可证发布的。详细信息请参见附带的 LICENSE 文件。
diff --git a/doc/8.8.4.4_with_margin.png b/doc/8.8.4.4_with_margin.png
new file mode 100644
index 0000000..42b3f48
Binary files /dev/null and b/doc/8.8.4.4_with_margin.png differ
diff --git a/doc/8.8.8.8_with_margin.png b/doc/8.8.8.8_with_margin.png
new file mode 100644
index 0000000..6de38eb
Binary files /dev/null and b/doc/8.8.8.8_with_margin.png differ
diff --git a/doc/Benjamin_with_margin.png b/doc/Benjamin_with_margin.png
new file mode 100644
index 0000000..13f09a1
Binary files /dev/null and b/doc/Benjamin_with_margin.png differ
diff --git a/doc/benjaminAtYzalisDotCom_with_margin.png b/doc/benjaminAtYzalisDotCom_with_margin.png
new file mode 100644
index 0000000..0108aef
Binary files /dev/null and b/doc/benjaminAtYzalisDotCom_with_margin.png differ
diff --git a/doc/yzalis_with_margin.png b/doc/yzalis_with_margin.png
new file mode 100644
index 0000000..67c3d91
Binary files /dev/null and b/doc/yzalis_with_margin.png differ
diff --git a/src/Identicon/Generator/BaseGenerator.php b/src/Identicon/Generator/BaseGenerator.php
index fa280eb..366de4a 100644
--- a/src/Identicon/Generator/BaseGenerator.php
+++ b/src/Identicon/Generator/BaseGenerator.php
@@ -29,6 +29,11 @@ class BaseGenerator
*/
protected $size;
+ /**
+ * @var int
+ */
+ protected $maigin;
+
/**
* @var int
*/
@@ -247,4 +252,32 @@ public function getPixelRatio()
{
return $this->pixelRatio;
}
+
+ /**
+ * Set the image margin.
+ *
+ * @param int $margin
+ *
+ * @return $this
+ */
+ public function setMargin($margin)
+ {
+ if (null === $margin) {
+ return $this;
+ }
+
+ $this->margin = $margin;
+
+ return $this;
+ }
+
+ /**
+ * Get the image margin.
+ *
+ * @return int
+ */
+ public function getMargin()
+ {
+ return $this->margin;
+ }
}
diff --git a/src/Identicon/Generator/GdGenerator.php b/src/Identicon/Generator/GdGenerator.php
index 21ea922..7039f68 100644
--- a/src/Identicon/Generator/GdGenerator.php
+++ b/src/Identicon/Generator/GdGenerator.php
@@ -33,7 +33,7 @@ public function getMimeType()
private function generateImage()
{
// prepare image
- $this->generatedImage = imagecreatetruecolor($this->getPixelRatio() * 5, $this->getPixelRatio() * 5);
+ $this->generatedImage = imagecreatetruecolor($this->getPixelRatio() * 5 + $this->getMargin() * 2, $this->getPixelRatio() * 5 + $this->getMargin() * 2);
$rgbBackgroundColor = $this->getBackgroundColor();
if (null === $rgbBackgroundColor) {
@@ -51,8 +51,15 @@ private function generateImage()
// 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);
+ if (true === $colValue && 5 > $lineKey) {
+ imagefilledrectangle(
+ $this->generatedImage,
+ $this->getMargin() + $colKey * $this->getPixelRatio(),
+ $this->getMargin() + $lineKey * $this->getPixelRatio(),
+ $this->getMargin() + ($colKey + 1) * $this->getPixelRatio() -1,
+ $this->getMargin() + ($lineKey + 1) * $this->getPixelRatio() -1,
+ $gdColor
+ );
}
}
}
@@ -63,10 +70,10 @@ private function generateImage()
/**
* {@inheritdoc}
*/
- public function getImageBinaryData($string, $size = null, $color = null, $backgroundColor = null)
+ public function getImageBinaryData($string, $size = null, $color = null, $backgroundColor = null, $margin = null)
{
ob_start();
- imagepng($this->getImageResource($string, $size, $color, $backgroundColor));
+ imagepng($this->getImageResource($string, $size, $color, $backgroundColor, $margin));
$imageData = ob_get_contents();
ob_end_clean();
@@ -76,13 +83,14 @@ public function getImageBinaryData($string, $size = null, $color = null, $backgr
/**
* {@inheritdoc}
*/
- public function getImageResource($string, $size = null, $color = null, $backgroundColor = null)
+ public function getImageResource($string, $size = null, $color = null, $backgroundColor = null, $margin = null)
{
$this
->setString($string)
->setSize($size)
->setColor($color)
->setBackgroundColor($backgroundColor)
+ ->setMargin($margin)
->generateImage();
return $this->generatedImage;
diff --git a/src/Identicon/Generator/GeneratorInterface.php b/src/Identicon/Generator/GeneratorInterface.php
index c6e8e0e..4ab8ef9 100644
--- a/src/Identicon/Generator/GeneratorInterface.php
+++ b/src/Identicon/Generator/GeneratorInterface.php
@@ -12,20 +12,22 @@ interface GeneratorInterface
* @param int $size
* @param array|string $color
* @param array|string $backgroundColor
+ * @param int $margin
*
* @return mixed
*/
- public function getImageBinaryData($string, $size = null, $color = null, $backgroundColor = null);
+ public function getImageBinaryData($string, $size = null, $color = null, $backgroundColor = null, $margin = null);
/**
* @param string $string
* @param int $size
* @param array|string $color
* @param array|string $backgroundColor
+ * @param int $margin
*
* @return string
*/
- public function getImageResource($string, $size = null, $color = null, $backgroundColor = null);
+ public function getImageResource($string, $size = null, $color = null, $backgroundColor = null, $margin = null);
/**
* Return the mime-type of this identicon.
@@ -33,10 +35,10 @@ public function getImageResource($string, $size = null, $color = null, $backgrou
* @return string
*/
public function getMimeType();
-
+
/**
* Return the color of the created identicon.
- *
+ *
* @return array
*/
public function getColor();
diff --git a/src/Identicon/Generator/ImageMagickGenerator.php b/src/Identicon/Generator/ImageMagickGenerator.php
index 0bcd349..09e9467 100644
--- a/src/Identicon/Generator/ImageMagickGenerator.php
+++ b/src/Identicon/Generator/ImageMagickGenerator.php
@@ -45,7 +45,7 @@ private function generateImage()
$background = new ImagickPixel("rgb($rgbBackgroundColor[0],$rgbBackgroundColor[1],$rgbBackgroundColor[2])");
}
- $this->generatedImage->newImage($this->pixelRatio * 5, $this->pixelRatio * 5, $background, 'png');
+ $this->generatedImage->newImage($this->pixelRatio * 5 + $this->getMargin() * 2, $this->pixelRatio * 5 + $this->getMargin() * 2, $background, 'png');
// prepare color
$rgbColor = $this->getColor();
@@ -57,8 +57,12 @@ private function generateImage()
// 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);
+ if (true === $colValue && 5 > $lineKey) {
+ $draw->rectangle(
+ $this->getMargin() + $colKey * $this->pixelRatio,
+ $this->getMargin() + $lineKey * $this->pixelRatio,
+ $this->getMargin() + ($colKey + 1) * $this->pixelRatio - 1,
+ $this->getMargin() + ($lineKey + 1) * $this->pixelRatio) - 1;
}
}
}
@@ -71,10 +75,10 @@ private function generateImage()
/**
* {@inheritdoc}
*/
- public function getImageBinaryData($string, $size = null, $color = null, $backgroundColor = null)
+ public function getImageBinaryData($string, $size = null, $color = null, $backgroundColor = null, $margin = null)
{
ob_start();
- echo $this->getImageResource($string, $size, $color, $backgroundColor);
+ echo $this->getImageResource($string, $size, $color, $backgroundColor, $margin);
$imageData = ob_get_contents();
ob_end_clean();
@@ -84,13 +88,14 @@ public function getImageBinaryData($string, $size = null, $color = null, $backgr
/**
* {@inheritdoc}
*/
- public function getImageResource($string, $size = null, $color = null, $backgroundColor = null)
+ public function getImageResource($string, $size = null, $color = null, $backgroundColor = null, $margin = null)
{
$this
->setString($string)
->setSize($size)
->setColor($color)
->setBackgroundColor($backgroundColor)
+ ->setMargin($margin)
->generateImage();
return $this->generatedImage;
diff --git a/src/Identicon/Generator/SvgGenerator.php b/src/Identicon/Generator/SvgGenerator.php
index 7e753e8..b619984 100644
--- a/src/Identicon/Generator/SvgGenerator.php
+++ b/src/Identicon/Generator/SvgGenerator.php
@@ -18,21 +18,22 @@ public function getMimeType()
/**
* {@inheritdoc}
*/
- public function getImageBinaryData($string, $size = null, $color = null, $backgroundColor = null)
+ public function getImageBinaryData($string, $size = null, $color = null, $backgroundColor = null, $margin = null)
{
- return $this->getImageResource($string, $size, $color, $backgroundColor);
+ return $this->getImageResource($string, $size, $color, $backgroundColor, $margin);
}
/**
* {@inheritdoc}
*/
- public function getImageResource($string, $size = null, $color = null, $backgroundColor = null)
+ public function getImageResource($string, $size = null, $color = null, $backgroundColor = null, $margin = null)
{
$this
->setString($string)
->setSize($size)
->setColor($color)
->setBackgroundColor($backgroundColor)
+ ->setMargin($margin)
->_generateImage();
return $this->generatedImage;
@@ -44,9 +45,9 @@ public function getImageResource($string, $size = null, $color = null, $backgrou
protected function _generateImage()
{
// prepare image
- $w = $this->getPixelRatio() * 5;
- $h = $this->getPixelRatio() * 5;
- $svg = '