Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<img src="<?php echo $imageDataUri; ?>" alt="bar Identicon" />
```

Expand All @@ -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
Expand All @@ -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
<img src="<?= $imageDataUri; ?>" alt="bar Identicon" />
```

### 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:

![Identicon example #6](doc/benjaminAtYzalisDotCom_with_margin.png)&nbsp;&nbsp;
![Identicon example #7](doc/Benjamin_with_margin.png)&nbsp;&nbsp;
![Identicon example #8](doc/8.8.8.8_with_margin.png)&nbsp;&nbsp;
![Identicon example #9](doc/8.8.4.4_with_margin.png)&nbsp;&nbsp;
![Identicon example #10](doc/yzalis_with_margin.png)

```php
$identicon->displayImage('foo', 100, null, '#f0f0f0', 10);
```

## Unit Tests

Expand Down
144 changes: 144 additions & 0 deletions README.zh_cn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# Identicon 生成器 PHP 版

[![Build Status](https://secure.travis-ci.org/yzalis/Identicon.png)](http://travis-ci.org/yzalis/Identicon)
[![codecov.io](https://codecov.io/github/yzalis/Identicon/coverage.svg?branch=master)](https://codecov.io/github/yzalis/Identicon?branch=master)

**Identicon** 是一个基于字符串生成一个 [identicon](http://en.wikipedia.org/wiki/Identicon) 图像的库。

以下是一些精彩的输出例子!

![Identicon example #1](doc/benjaminAtYzalisDotCom.png)&nbsp;&nbsp;
![Identicon example #2](doc/Benjamin.png)&nbsp;&nbsp;
![Identicon example #3](doc/8.8.8.8.png)&nbsp;&nbsp;
![Identicon example #4](doc/8.8.4.4.png)&nbsp;&nbsp;
![Identicon example #5](doc/yzalis.png)

## 安装

推荐通过 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
<img src="<?php echo $imageDataUri; ?>" alt="bar Identicon" />
```


### 修改图像大小

默认的大小是 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
<img src="<?= $imageDataUri; ?>" alt="bar Identicon" />
```

### 边距

默认是没有边距的,如果你想要带边距的图像,你可以添加第五个参数。推荐使用图像的大小的十分之一。

在这个例子中,我们设置了灰色的背景,这样你可以很明显的看到边距:

![Identicon example #6](doc/benjaminAtYzalisDotCom_with_margin.png)&nbsp;&nbsp;
![Identicon example #7](doc/Benjamin_with_margin.png)&nbsp;&nbsp;
![Identicon example #8](doc/8.8.8.8_with_margin.png)&nbsp;&nbsp;
![Identicon example #9](doc/8.8.4.4_with_margin.png)&nbsp;&nbsp;
![Identicon example #10](doc/yzalis_with_margin.png)

```php
$identicon->displayImage('foo', 100, null, '#f0f0f0', 10);
```

## 单元测试

为了运行单元测试,你需要一组依赖,它们可以通过 Composer 安装:

```
php composer.phar install
```

一旦安装,就可以使用下面的命令:

```
./vendor/bin/phpunit
```

应该一切都好了。


## 贡献者名单

* Benjamin Laugueux <benjamin@laugueux.org>
* [所有贡献者](https://github.com/yzalis/Identicon/graphs/contributors)

灵感来自于一篇关于 Identicon 的 Github [博客](https://github.com/blog/1586-identicons)。


## 证书

Identicon 是根据 MIT 许可证发布的。详细信息请参见附带的 LICENSE 文件。
Binary file added doc/8.8.4.4_with_margin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/8.8.8.8_with_margin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/Benjamin_with_margin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/benjaminAtYzalisDotCom_with_margin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/yzalis_with_margin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions src/Identicon/Generator/BaseGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class BaseGenerator
*/
protected $size;

/**
* @var int
*/
protected $maigin;

/**
* @var int
*/
Expand Down Expand Up @@ -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;
}
}
20 changes: 14 additions & 6 deletions src/Identicon/Generator/GdGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
);
}
}
}
Expand All @@ -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();

Expand All @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions src/Identicon/Generator/GeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,33 @@ 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.
*
* @return string
*/
public function getMimeType();

/**
* Return the color of the created identicon.
*
*
* @return array
*/
public function getColor();
Expand Down
17 changes: 11 additions & 6 deletions src/Identicon/Generator/ImageMagickGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
}
}
Expand All @@ -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();

Expand All @@ -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;
Expand Down
Loading