Skip to content

Commit

Permalink
Change svg-output to path-element; making it much more compact (#75)
Browse files Browse the repository at this point in the history
This also circumvents rendering issues when scaling isn't perfect with the rects
And it also fixes a bug with the color having much more than just 3 bytes.
  • Loading branch information
blaugueux committed Oct 14, 2019
2 parents 09be27a + fd4a66d commit ff5ed09
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Identicon/Generator/SvgGenerator.php
Expand Up @@ -46,25 +46,28 @@ 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.'">';
$svg = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="'.$w.'" height="'.$h.'" viewBox="0 0 5 5">';

$backgroundColor = '#FFFFFF';
$backgroundColor = '#FFF';
$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());
$svg .= '<rect width="5" height="5" fill="'.$backgroundColor.'" stroke-width="0"/>';

$rects = [];
// 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;"/>';
$rects[] = 'M'.$colKey.','.$lineKey.'h1v1h-1v-1';
}
}
}

$rgbColor = $this->_toUnderstandableColor($this->getColor());
$svg .= '<path fill="'.$rgbColor.'" stroke-width="0" d="' . implode('', $rects) . '"/>';
$svg .= '</svg>';

$this->generatedImage = $svg;
Expand All @@ -80,7 +83,7 @@ protected function _generateImage()
protected function _toUnderstandableColor($color)
{
if (is_array($color)) {
return 'rgb('.implode(', ', $color).')';
return sprintf('#%X%X%X', $color[0], $color[1], $color[2]);
}

return $color;
Expand Down
25 changes: 25 additions & 0 deletions tests/Identicon/Tests/IdenticonTest.php
Expand Up @@ -3,6 +3,7 @@
namespace Identicon\Tests;

use Identicon\Generator\ImageMagickGenerator;
use Identicon\Generator\SvgGenerator;
use Identicon\Identicon;

/**
Expand Down Expand Up @@ -57,4 +58,28 @@ public function imageMagickResultDataProvider()
['benjaminAtYzalisDotCom', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEEAAABBAQMAAAC0OVsGAAAABlBMVEUAAABgsDAvrOz7AAAAAXRSTlMAQObYZgAAAAlwSFlzAAAASAAAAEgARslrPgAAADlJREFUKM9jYGD//4Dx/w8GIBgg1n8oaGD4/4fB/j8DP/1ZCBf8B7lqQFhQFwDDBQigIURH1oDHAgCNSlrmMVCO8AAAAABJRU5ErkJggg=='],
];
}

/**
* @dataProvider svgResultDataProvider
*/
public function testSvgResult($string, $imageData)
{
$this->identicon->setGenerator(new SvgGenerator());
$this->assertEquals($imageData, $this->identicon->getImageData($string));
}

public function svgResultDataProvider()
{
return [
['Benjamin', '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="65" height="65" viewBox="0 0 5 5"><rect width="5" height="5" fill="#FFF" stroke-width="0"/><path fill="#804080" stroke-width="0" d="M0,0h1v1h-1v-1M2,0h1v1h-1v-1M4,0h1v1h-1v-1M1,1h1v1h-1v-1M2,1h1v1h-1v-1M3,1h1v1h-1v-1M0,2h1v1h-1v-1M1,2h1v1h-1v-1M3,2h1v1h-1v-1M4,2h1v1h-1v-1M0,3h1v1h-1v-1M1,3h1v1h-1v-1M2,3h1v1h-1v-1M3,3h1v1h-1v-1M4,3h1v1h-1v-1M0,4h1v1h-1v-1M1,4h1v1h-1v-1M3,4h1v1h-1v-1M4,4h1v1h-1v-1M0,5h1v1h-1v-1M4,5h1v1h-1v-1"/></svg>'],

['8.8.8.8', '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="65" height="65" viewBox="0 0 5 5"><rect width="5" height="5" fill="#FFF" stroke-width="0"/><path fill="#B09040" stroke-width="0" d="M1,0h1v1h-1v-1M3,0h1v1h-1v-1M0,1h1v1h-1v-1M1,1h1v1h-1v-1M3,1h1v1h-1v-1M4,1h1v1h-1v-1M0,2h1v1h-1v-1M1,2h1v1h-1v-1M3,2h1v1h-1v-1M4,2h1v1h-1v-1M0,3h1v1h-1v-1M2,3h1v1h-1v-1M4,3h1v1h-1v-1M2,4h1v1h-1v-1M0,5h1v1h-1v-1M4,5h1v1h-1v-1"/></svg>'],

['8.8.4.4', '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="65" height="65" viewBox="0 0 5 5"><rect width="5" height="5" fill="#FFF" stroke-width="0"/><path fill="#60F0E0" stroke-width="0" d="M0,0h1v1h-1v-1M4,0h1v1h-1v-1M0,2h1v1h-1v-1M2,2h1v1h-1v-1M4,2h1v1h-1v-1M1,3h1v1h-1v-1M3,3h1v1h-1v-1M1,4h1v1h-1v-1M2,4h1v1h-1v-1M3,4h1v1h-1v-1M0,5h1v1h-1v-1M4,5h1v1h-1v-1"/></svg>'],

['yzalis', '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="65" height="65" viewBox="0 0 5 5"><rect width="5" height="5" fill="#FFF" stroke-width="0"/><path fill="#E0C090" stroke-width="0" d="M1,0h1v1h-1v-1M2,0h1v1h-1v-1M3,0h1v1h-1v-1M2,1h1v1h-1v-1M0,2h1v1h-1v-1M1,2h1v1h-1v-1M3,2h1v1h-1v-1M4,2h1v1h-1v-1M0,3h1v1h-1v-1M4,3h1v1h-1v-1M0,4h1v1h-1v-1M1,4h1v1h-1v-1M2,4h1v1h-1v-1M3,4h1v1h-1v-1M4,4h1v1h-1v-1M0,5h1v1h-1v-1M4,5h1v1h-1v-1"/></svg>'],

['benjaminAtYzalisDotCom', '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="65" height="65" viewBox="0 0 5 5"><rect width="5" height="5" fill="#FFF" stroke-width="0"/><path fill="#60B030" stroke-width="0" d="M1,0h1v1h-1v-1M3,0h1v1h-1v-1M0,1h1v1h-1v-1M2,1h1v1h-1v-1M4,1h1v1h-1v-1M0,2h1v1h-1v-1M1,2h1v1h-1v-1M3,2h1v1h-1v-1M4,2h1v1h-1v-1M1,3h1v1h-1v-1M2,3h1v1h-1v-1M3,3h1v1h-1v-1M0,4h1v1h-1v-1M2,4h1v1h-1v-1M4,4h1v1h-1v-1M0,5h1v1h-1v-1M4,5h1v1h-1v-1"/></svg>'],
];
}
}

0 comments on commit ff5ed09

Please sign in to comment.