paste.chapril.org-privatebin/vendor/yzalis/identicon/src/Identicon/Generator/SvgGenerator.php

92 lines
2.3 KiB
PHP
Raw Normal View History

2020-01-14 21:42:06 +01:00
<?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;
2020-04-12 16:26:05 +02:00
$svg = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="'.$w.'" height="'.$h.'" viewBox="0 0 5 5">';
2020-01-14 21:42:06 +01:00
2020-04-12 16:26:05 +02:00
$backgroundColor = '#FFF';
2020-01-14 21:42:06 +01:00
$rgbBackgroundColor = $this->getBackgroundColor();
if (!is_null($rgbBackgroundColor)) {
$backgroundColor = $this->_toUnderstandableColor($rgbBackgroundColor);
}
2020-04-12 16:26:05 +02:00
$svg .= '<rect width="5" height="5" fill="'.$backgroundColor.'" stroke-width="0"/>';
$rects = [];
2020-01-14 21:42:06 +01:00
// draw content
foreach ($this->getArrayOfSquare() as $lineKey => $lineValue) {
foreach ($lineValue as $colKey => $colValue) {
if (true === $colValue) {
2020-04-12 16:26:05 +02:00
$rects[] = 'M'.$colKey.','.$lineKey.'h1v1h-1v-1';
2020-01-14 21:42:06 +01:00
}
}
}
2020-04-12 16:26:05 +02:00
$rgbColor = $this->_toUnderstandableColor($this->getColor());
$svg .= '<path fill="'.$rgbColor.'" stroke-width="0" d="' . implode('', $rects) . '"/>';
2020-01-14 21:42:06 +01:00
$svg .= '</svg>';
$this->generatedImage = $svg;
return $this;
}
/**
* @param array|string $color
*
* @return string
*/
protected function _toUnderstandableColor($color)
{
if (is_array($color)) {
2020-04-12 16:26:05 +02:00
return sprintf('#%X%X%X', $color[0], $color[1], $color[2]);
2020-01-14 21:42:06 +01:00
}
return $color;
}
}