2016-08-10 21:14:50 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Identicon\Generator;
|
|
|
|
|
2019-06-16 13:10:24 +08:00
|
|
|
use Exception;
|
2016-08-10 21:14:50 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Benjamin Laugueux <benjamin@yzalis.com>
|
|
|
|
*/
|
|
|
|
class GdGenerator extends BaseGenerator implements GeneratorInterface
|
|
|
|
{
|
2019-06-16 13:10:24 +08:00
|
|
|
/**
|
|
|
|
* GdGenerator constructor.
|
|
|
|
*/
|
2016-08-10 21:14:50 +08:00
|
|
|
public function __construct()
|
|
|
|
{
|
2019-06-16 13:10:24 +08:00
|
|
|
if (!extension_loaded('gd') && !extension_loaded('ext-gd')) {
|
|
|
|
throw new Exception('GD does not appear to be available in your PHP installation. Please try another generator');
|
2016-08-10 21:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 13:10:24 +08:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getMimeType()
|
|
|
|
{
|
|
|
|
return 'image/png';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2016-08-10 21:14:50 +08:00
|
|
|
private function generateImage()
|
|
|
|
{
|
|
|
|
// prepare image
|
|
|
|
$this->generatedImage = imagecreatetruecolor($this->getPixelRatio() * 5, $this->getPixelRatio() * 5);
|
|
|
|
|
|
|
|
$rgbBackgroundColor = $this->getBackgroundColor();
|
|
|
|
if (null === $rgbBackgroundColor) {
|
|
|
|
$background = imagecolorallocate($this->generatedImage, 0, 0, 0);
|
|
|
|
imagecolortransparent($this->generatedImage, $background);
|
|
|
|
} else {
|
|
|
|
$background = imagecolorallocate($this->generatedImage, $rgbBackgroundColor[0], $rgbBackgroundColor[1], $rgbBackgroundColor[2]);
|
|
|
|
imagefill($this->generatedImage, 0, 0, $background);
|
|
|
|
}
|
|
|
|
|
2019-06-16 13:10:24 +08:00
|
|
|
// prepare color
|
2016-08-10 21:14:50 +08:00
|
|
|
$rgbColor = $this->getColor();
|
|
|
|
$gdColor = imagecolorallocate($this->generatedImage, $rgbColor[0], $rgbColor[1], $rgbColor[2]);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-16 13:10:24 +08:00
|
|
|
* {@inheritdoc}
|
2016-08-10 21:14:50 +08:00
|
|
|
*/
|
|
|
|
public function getImageBinaryData($string, $size = null, $color = null, $backgroundColor = null)
|
|
|
|
{
|
|
|
|
ob_start();
|
|
|
|
imagepng($this->getImageResource($string, $size, $color, $backgroundColor));
|
|
|
|
$imageData = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
|
|
|
|
return $imageData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-16 13:10:24 +08:00
|
|
|
* {@inheritdoc}
|
2016-08-10 21:14:50 +08:00
|
|
|
*/
|
|
|
|
public function getImageResource($string, $size = null, $color = null, $backgroundColor = null)
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setString($string)
|
|
|
|
->setSize($size)
|
|
|
|
->setColor($color)
|
|
|
|
->setBackgroundColor($backgroundColor)
|
|
|
|
->generateImage();
|
|
|
|
|
|
|
|
return $this->generatedImage;
|
|
|
|
}
|
|
|
|
}
|