2022-01-07 19:53:31 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Hummingbird Anime List Client
|
|
|
|
*
|
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
|
|
|
*
|
|
|
|
* PHP version 8
|
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
|
|
|
* @copyright 2015 - 2021 Timothy J. Warren
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @version 5.2
|
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\Ion;
|
|
|
|
|
|
|
|
use Aviat\Ion\Exception\ImageCreationException;
|
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
use GdImage;
|
|
|
|
|
2022-01-07 19:53:31 -05:00
|
|
|
/**
|
|
|
|
* A wrapper around GD functions to create images
|
|
|
|
*
|
2022-03-03 17:26:09 -05:00
|
|
|
* @property false|GdImage|null $img
|
2022-01-07 19:53:31 -05:00
|
|
|
*/
|
2022-03-03 17:26:09 -05:00
|
|
|
class ImageBuilder
|
|
|
|
{
|
|
|
|
private GDImage|FALSE|NULL $_img;
|
2022-01-07 19:53:31 -05:00
|
|
|
private int $fontSize = 10;
|
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
private function __construct(private int $width = 200, private int $height = 200)
|
2022-01-07 19:53:31 -05:00
|
|
|
{
|
|
|
|
$this->_img = imagecreatetruecolor($this->width, $this->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
$this->cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getImg(): GdImage
|
|
|
|
{
|
|
|
|
if ($this->_img instanceof GdImage)
|
|
|
|
{
|
|
|
|
return $this->_img;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ImageCreationException('Invalid GD object');
|
|
|
|
}
|
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
public static function new(int $width = 200, int $height = 200): self
|
2022-01-07 19:53:31 -05:00
|
|
|
{
|
|
|
|
$i = new self($width, $height);
|
|
|
|
if ($i->_img === FALSE)
|
|
|
|
{
|
|
|
|
throw new ImageCreationException('Could not create image object');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setFontSize(int $size): self
|
|
|
|
{
|
|
|
|
$this->fontSize = $size;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function enableAlphaBlending(bool $enable): self
|
|
|
|
{
|
|
|
|
$ab = imagealphablending($this->getImg(), $enable);
|
|
|
|
if ( ! $ab)
|
|
|
|
{
|
|
|
|
throw new ImageCreationException('Failed to toggle image alpha blending');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
public function addCenteredText(string $text, int $red, int $green, int $blue, int $alpha = -1): self
|
2022-01-07 19:53:31 -05:00
|
|
|
{
|
|
|
|
// Create the font color
|
|
|
|
$textColor = ($alpha > -1)
|
|
|
|
? imagecolorallocatealpha($this->getImg(), $red, $green, $blue, $alpha)
|
|
|
|
: imagecolorallocate($this->getImg(), $red, $green, $blue);
|
|
|
|
if ($textColor === FALSE)
|
|
|
|
{
|
|
|
|
throw new ImageCreationException('Could not create image text color');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate placeholder text
|
|
|
|
$fontWidth = imagefontwidth($this->fontSize);
|
|
|
|
$fontHeight = imagefontheight($this->fontSize);
|
|
|
|
$length = strlen($text);
|
|
|
|
$textWidth = $length * $fontWidth;
|
|
|
|
$fxPos = (int) ceil((imagesx($this->getImg()) - $textWidth) / 2);
|
|
|
|
$fyPos = (int) ceil((imagesy($this->getImg()) - $fontHeight) / 2);
|
|
|
|
|
|
|
|
// Add the image text
|
|
|
|
imagestring($this->getImg(), $this->fontSize, $fxPos, $fyPos, $text, $textColor);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
public function addBackgroundColor(int $red, int $green, int $blue, int $alpha = -1): self
|
2022-01-07 19:53:31 -05:00
|
|
|
{
|
|
|
|
$fillColor = ($alpha > -1)
|
|
|
|
? imagecolorallocatealpha($this->getImg(), $red, $green, $blue, $alpha)
|
|
|
|
: imagecolorallocate($this->getImg(), $red, $green, $blue);
|
|
|
|
|
|
|
|
if ($fillColor === FALSE)
|
|
|
|
{
|
|
|
|
throw new ImageCreationException('Failed to create image fill color');
|
|
|
|
}
|
|
|
|
|
|
|
|
$hasFilled = imagefill($this->getImg(), 0, 0, $fillColor);
|
|
|
|
if ($hasFilled === FALSE)
|
|
|
|
{
|
|
|
|
throw new ImageCreationException('Failed to add background color to image');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function savePng(string $savePath, bool $saveAlpha = TRUE): bool
|
|
|
|
{
|
|
|
|
$setAlpha = imagesavealpha($this->getImg(), $saveAlpha);
|
|
|
|
if ($setAlpha === FALSE)
|
|
|
|
{
|
|
|
|
throw new ImageCreationException('Failed to set image save alpha flag');
|
|
|
|
}
|
|
|
|
|
|
|
|
return imagepng($this->getImg(), $savePath, 9);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function saveWebp(string $savePath): bool
|
|
|
|
{
|
|
|
|
return imagewebp($this->getImg(), $savePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function saveJpg(string $savePath): bool
|
|
|
|
{
|
|
|
|
return imagejpeg($this->getImg(), $savePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function saveGif(string $savePath): bool
|
|
|
|
{
|
|
|
|
return imagegif($this->getImg(), $savePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function cleanup(): void
|
|
|
|
{
|
|
|
|
$cleaned = FALSE;
|
|
|
|
|
|
|
|
if ($this->getImg() instanceof GdImage)
|
|
|
|
{
|
|
|
|
$cleaned = imagedestroy($this->getImg());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($cleaned === FALSE)
|
|
|
|
{
|
|
|
|
throw new ImageCreationException('Failed to clean up image resource');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|